Make AP emit similar warnings to transformer for up/downgraded methods

This commit is contained in:
Mumfrey committed 2017-06-29 15:47:10 +01:00
1 parent 366473c1af
commit faf581b977
14 files changed
+172 -24

No files matched your search

@@ -88,6 +88,11 @@ class AnnotatedMixin {
* Mixin class
*/
private final TypeElement mixin;
/**
* Methods
*/
private final List<ExecutableElement> methods;
/**
* Mixin class
@@ -146,6 +151,11 @@ class AnnotatedMixin {
* Soft implementation handler;
*/
private final AnnotatedMixinElementHandlerSoftImplements softImplements;
/**
* True once the FINAL
*/
private boolean validated = false;
public AnnotatedMixin(IMixinAnnotationProcessor ap, TypeElement type) {
this.typeProvider = ap.getTypeProvider();
@@ -154,11 +164,11 @@ class AnnotatedMixin {
this.messager = ap;
this.mixin = type;
this.handle = new TypeHandle(type);
this.methods = new ArrayList<ExecutableElement>(this.handle.<ExecutableElement>getEnclosedElements(ElementKind.METHOD));
this.virtual = this.handle.getAnnotation(Pseudo.class).exists();
this.annotation = this.handle.getAnnotation(Mixin.class);
this.classRef = type.getQualifiedName().toString().replace('.', '/');
this.primaryTarget = this.initTargets();
// this.remap = !this.virtual && this.annotation.getBoolean("remap", true) && this.targets.size() > 0;
this.remap = this.annotation.getBoolean("remap", true) && this.targets.size() > 0;
this.overwrites = new AnnotatedMixinElementHandlerOverwrite(ap, this);
@@ -174,6 +184,11 @@ class AnnotatedMixin {
break;
}
}
if (pass == ValidationPass.FINAL && !this.validated) {
this.validated = true;
this.runFinalValidation();
}
return this;
}
@@ -319,7 +334,14 @@ class AnnotatedMixin {
return this.mappings;
}
private void runFinalValidation() {
for (ExecutableElement method : this.methods) {
this.overwrites.registerMerge(method);
}
}
public void registerOverwrite(ExecutableElement method, AnnotationHandle overwrite) {
this.methods.remove(method);
this.overwrites.registerOverwrite(new AnnotatedElementOverwrite(method, overwrite));
}
@@ -328,10 +350,12 @@ class AnnotatedMixin {
}
public void registerShadow(ExecutableElement method, AnnotationHandle shadow, boolean shouldRemap) {
this.methods.remove(method);
this.shadows.registerShadow(this.shadows.new AnnotatedElementShadowMethod(method, shadow, shouldRemap));
}
public void registerInjector(ExecutableElement method, AnnotationHandle inject, InjectorRemap remap) {
this.methods.remove(method);
this.injectors.registerInjector(new AnnotatedElementInjector(method, inject, remap));
List<AnnotationHandle> ats = inject.getAnnotationList("at");
@@ -360,10 +384,12 @@ class AnnotatedMixin {
}
public void registerAccessor(ExecutableElement element, AnnotationHandle accessor, boolean shouldRemap) {
this.methods.remove(element);
this.accessors.registerAccessor(new AnnotatedElementAccessor(element, accessor, shouldRemap));
}
public void registerInvoker(ExecutableElement element, AnnotationHandle invoker, boolean shouldRemap) {
this.methods.remove(element);
this.accessors.registerAccessor(new AnnotatedElementInvoker(element, invoker, shouldRemap));
}
@@ -49,6 +49,7 @@ import org.spongepowered.tools.obfuscation.mirror.FieldHandle;
import org.spongepowered.tools.obfuscation.mirror.MethodHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
import org.spongepowered.tools.obfuscation.mirror.Visibility;
/**
* Base class for module for {@link AnnotatedMixin} which handle different
@@ -153,6 +154,10 @@ abstract class AnnotatedMixinElementHandler {
return this.originalName;
}
public boolean hasPrefix() {
return false;
}
}
/**
@@ -232,6 +237,11 @@ abstract class AnnotatedMixinElementHandler {
this.obfuscated = name;
return this;
}
@Override
public boolean hasPrefix() {
return this.hasPrefix;
}
/**
* Get the prefix (if set), does not return the expected prefix
@@ -369,7 +379,7 @@ abstract class AnnotatedMixinElementHandler {
protected final void validateTarget(Element element, AnnotationHandle annotation, AliasedElementName name, String type) {
if (element instanceof ExecutableElement) {
this.validateTargetMethod((ExecutableElement)element, annotation, name, type);
this.validateTargetMethod((ExecutableElement)element, annotation, name, type, false, false);
} else if (element instanceof VariableElement) {
this.validateTargetField((VariableElement)element, annotation, name, type);
}
@@ -379,7 +389,8 @@ abstract class AnnotatedMixinElementHandler {
* Checks whether the specified method exists in all targets and raises
* warnings where appropriate
*/
protected final void validateTargetMethod(ExecutableElement method, AnnotationHandle annotation, AliasedElementName name, String type) {
protected final void validateTargetMethod(ExecutableElement method, AnnotationHandle annotation, AliasedElementName name, String type,
boolean overwrite, boolean merge) {
String signature = TypeUtils.getJavaSignature(method);
for (TypeHandle target : this.mixin.getTargets()) {
@@ -389,31 +400,47 @@ abstract class AnnotatedMixinElementHandler {
// Find method as-is
MethodHandle targetMethod = target.findMethod(method);
if (targetMethod != null) {
continue;
}
if (!name.baseName().equals(name.elementName())) {
// Find method without prefix
// Find method without prefix
if (targetMethod == null && name.hasPrefix()) {
targetMethod = target.findMethod(name.baseName(), signature);
if (targetMethod != null) {
continue;
}
}
// Check aliases
for (String alias : name.getAliases()) {
if ((targetMethod = target.findMethod(alias, signature)) != null) {
break;
if (targetMethod == null && name.hasAliases()) {
for (String alias : name.getAliases()) {
if ((targetMethod = target.findMethod(alias, signature)) != null) {
break;
}
}
}
if (targetMethod == null) {
this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " method in " + target, method, annotation.asMirror());
if (targetMethod != null) {
if (overwrite) {
this.validateMethodVisibility(method, annotation, type, target, targetMethod);
}
} else if (!merge) {
this.printMessage(Kind.WARNING, "Cannot find target for " + type + " method in " + target, method, annotation);
}
}
}
private void validateMethodVisibility(ExecutableElement method, AnnotationHandle annotation, String type, TypeHandle target,
MethodHandle targetMethod) {
Visibility visTarget = targetMethod.getVisibility();
if (visTarget == null) {
return;
}
Visibility visMethod = TypeUtils.getVisibility(method);
String visibility = "visibility of " + visTarget + " method in " + target;
if (visTarget.ordinal() > visMethod.ordinal()) {
this.printMessage(Kind.WARNING, visMethod + " " + type + " method cannot reduce " + visibility, method, annotation);
} else if (visTarget == Visibility.PRIVATE && visMethod.ordinal() > visTarget.ordinal()) {
this.printMessage(Kind.WARNING, visMethod + " " + type + " method will upgrade " + visibility, method, annotation);
}
}
/**
* Checks whether the specified field exists in all targets and raises
* warnings where appropriate
@@ -465,6 +492,14 @@ abstract class AnnotatedMixinElementHandler {
}
}
private void printMessage(Kind kind, String msg, Element e, AnnotationHandle annotation) {
if (annotation == null) {
this.ap.printMessage(kind, msg, e);
} else {
this.ap.printMessage(kind, msg, e, annotation.asMirror());
}
}
protected static <T extends IMapping<T>> ObfuscationData<T> stripOwnerData(ObfuscationData<T> data) {
ObfuscationData<T> stripped = new ObfuscationData<T>();
for (ObfuscationType type : data) {
@@ -55,9 +55,13 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler
super(ap, mixin);
}
public void registerMerge(ExecutableElement method) {
this.validateTargetMethod(method, null, new AliasedElementName(method, AnnotationHandle.MISSING), "overwrite", true, true);
}
public void registerOverwrite(AnnotatedElementOverwrite elem) {
AliasedElementName name = new AliasedElementName(elem.getElement(), elem.getAnnotation());
this.validateTargetMethod(elem.getElement(), elem.getAnnotation(), name, "@Overwrite");
this.validateTargetMethod(elem.getElement(), elem.getAnnotation(), name, "@Overwrite", true, false);
this.checkConstraints(elem.getElement(), elem.getAnnotation());
if (!this.mixin.remap()) {
@@ -38,6 +38,7 @@ import java.util.regex.Pattern;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
@@ -472,13 +473,13 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
/**
* Called from each AP when a pass is completed
*/
public void onPassCompleted() {
public void onPassCompleted(RoundEnvironment roundEnv) {
if (!"true".equalsIgnoreCase(this.getOption(SupportedOptions.DISABLE_TARGET_EXPORT))) {
this.targets.write(true);
}
for (AnnotatedMixin mixin : this.mixinsForPass) {
mixin.runValidators(ValidationPass.LATE, this.validators);
for (AnnotatedMixin mixin : roundEnv.processingOver() ? this.mixins.values() : this.mixinsForPass) {
mixin.runValidators(roundEnv.processingOver() ? ValidationPass.FINAL : ValidationPass.LATE, this.validators);
}
}
@@ -75,7 +75,7 @@ public abstract class MixinObfuscationProcessor extends AbstractProcessor {
}
protected void postProcess(RoundEnvironment roundEnv) {
this.mixins.onPassCompleted();
this.mixins.onPassCompleted(roundEnv);
}
@Override
@@ -66,6 +66,7 @@ public class MixinObfuscationProcessorInjection extends MixinObfuscationProcesso
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
this.postProcess(roundEnv);
return true;
}
@@ -64,6 +64,7 @@ public class MixinObfuscationProcessorTargets extends MixinObfuscationProcessor
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
this.postProcess(roundEnv);
return true;
}
@@ -42,7 +42,8 @@ public interface IMixinValidator {
*/
public enum ValidationPass {
EARLY,
LATE
LATE,
FINAL
}
/**
@@ -44,6 +44,8 @@ import com.google.common.collect.ImmutableList;
*/
public final class AnnotationHandle {
public static final AnnotationHandle MISSING = new AnnotationHandle(null);
/**
* Annotation being wrapped
*/
@@ -86,6 +86,11 @@ public class FieldHandle extends MemberHandle<MappingField> {
public VariableElement getElement() {
return this.element;
}
@Override
public Visibility getVisibility() {
return TypeUtils.getVisibility(this.element);
}
/**
* Returns true if the searched type had a type specifier but the returned
@@ -57,6 +57,11 @@ public abstract class MemberHandle<T extends IMapping<T>> {
return this.desc;
}
/**
* Get the visibility level for this member
*/
public abstract Visibility getVisibility();
/**
* Return this handle as a mapping
*
@@ -46,7 +46,7 @@ public class MethodHandle extends MemberHandle<MappingMethod> {
}
public MethodHandle(String owner, ExecutableElement element) {
this(owner, TypeUtils.getName(element), TypeUtils.getDescriptor(element));
this(owner, element, TypeUtils.getName(element), TypeUtils.getDescriptor(element));
}
protected MethodHandle(String owner, String name, String desc) {
@@ -71,6 +71,11 @@ public class MethodHandle extends MemberHandle<MappingMethod> {
public ExecutableElement getElement() {
return this.element;
}
@Override
public Visibility getVisibility() {
return TypeUtils.getVisibility(this.element);
}
@Override
public MappingMethod asMapping(boolean includeOwner) {
@@ -27,6 +27,7 @@ package org.spongepowered.tools.obfuscation.mirror;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
@@ -372,4 +373,27 @@ public abstract class TypeUtils {
return processingEnv.getElementUtils().getTypeElement(((TypeElement)targetType.asElement()).getQualifiedName()).asType();
}
/**
* Get the ordinal visibility for the specified element
*
* @param element element to inspect
* @return visibility level or null if element is null
*/
public static Visibility getVisibility(Element element) {
if (element == null) {
return null;
}
for (Modifier modifier : element.getModifiers()) {
switch (modifier) {
case PUBLIC: return Visibility.PUBLIC;
case PROTECTED: return Visibility.PROTECTED;
case PRIVATE: return Visibility.PRIVATE;
default: break;
}
}
return Visibility.PACKAGE;
}
}
@@ -0,0 +1,38 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.tools.obfuscation.mirror;
/**
* Ordinal member visibility level. This is used to represent visibility of
* a member in a formal way from lowest to highest.
*/
public enum Visibility {
PRIVATE,
PROTECTED,
PACKAGE,
PUBLIC
}