From c007eabc31fdfa2d2eafdaf89bfbd6dc86307573 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 23 Nov 2016 17:53:43 +0000 Subject: [PATCH] Refactor annotation handling in the AP --- .../org/spongepowered/tools/MirrorUtils.java | 99 +------- .../tools/obfuscation/AnnotatedMixin.java | 36 ++- .../AnnotatedMixinElementHandler.java | 59 ++--- .../AnnotatedMixinElementHandlerAccessor.java | 33 ++- .../AnnotatedMixinElementHandlerInjector.java | 57 ++--- ...AnnotatedMixinElementHandlerOverwrite.java | 7 +- .../AnnotatedMixinElementHandlerShadow.java | 14 +- ...atedMixinElementHandlerSoftImplements.java | 19 +- .../tools/obfuscation/AnnotatedMixins.java | 32 ++- .../tools/obfuscation/AnnotationHandle.java | 221 ++++++++++++++++++ .../MixinObfuscationProcessorInjection.java | 3 +- .../MixinObfuscationProcessorTargets.java | 5 +- .../tools/obfuscation/MixinValidator.java | 8 +- .../tools/obfuscation/TypeHandle.java | 7 +- .../interfaces/IMixinValidator.java | 4 +- .../tools/obfuscation/struct/Message.java | 14 +- .../validation/ParentValidator.java | 7 +- .../validation/TargetValidator.java | 11 +- 18 files changed, 378 insertions(+), 258 deletions(-) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/AnnotationHandle.java diff --git a/src/ap/java/org/spongepowered/tools/MirrorUtils.java b/src/ap/java/org/spongepowered/tools/MirrorUtils.java index f0772672..23fa4ac7 100644 --- a/src/ap/java/org/spongepowered/tools/MirrorUtils.java +++ b/src/ap/java/org/spongepowered/tools/MirrorUtils.java @@ -24,14 +24,7 @@ */ package org.spongepowered.tools; -import java.lang.annotation.Annotation; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.PackageElement; @@ -60,91 +53,6 @@ public abstract class MirrorUtils { // No instances for you private MirrorUtils() {} - public static AnnotationMirror getAnnotation(Element elem, Class annotationClass) { - List annotations = elem.getAnnotationMirrors(); - - if (annotations == null) { - return null; - } - - for (AnnotationMirror annotation : annotations) { - Element element = annotation.getAnnotationType().asElement(); - if (!(element instanceof TypeElement)) { - continue; - } - TypeElement annotationElement = (TypeElement)element; - if (annotationElement.getQualifiedName().contentEquals(annotationClass.getName())) { - return annotation; - } - } - - return null; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static T getAnnotationValue(AnnotationMirror annotation, String key, T defaultValue) { - if (annotation == null) { - return defaultValue; - } - - AnnotationValue value = MirrorUtils.getAnnotationValue0(annotation, key); - if (defaultValue instanceof Enum && value != null) { - VariableElement varValue = (VariableElement)value.getValue(); - if (varValue == null) { - return defaultValue; - } - return (T)Enum.valueOf((Class)defaultValue.getClass(), varValue.getSimpleName().toString()); - } - - return value != null ? (T)value.getValue() : defaultValue; - } - - public static T getAnnotationValue(TypeElement elem, Class annotationClass, String key, T defaultValue) { - AnnotationMirror annotation = MirrorUtils.getAnnotation(elem, annotationClass); - return MirrorUtils.getAnnotationValue(annotation, key, defaultValue); - } - - public static T getAnnotationValue(AnnotationMirror annotation) { - return MirrorUtils.getAnnotationValue(annotation, "value", null); - } - - public static T getAnnotationValue(AnnotationMirror annotation, String key) { - return MirrorUtils.getAnnotationValue(annotation, key, null); - } - - public static T getAnnotationValue(TypeElement elem, Class annotationClass) { - return MirrorUtils.getAnnotationValue(elem, annotationClass, "value"); - } - - public static T getAnnotationValue(TypeElement elem, Class annotationClass, String key) { - AnnotationMirror annotation = MirrorUtils.getAnnotation(elem, annotationClass); - return MirrorUtils.getAnnotationValue(annotation, key, null); - } - - private static AnnotationValue getAnnotationValue0(AnnotationMirror annotation, String key) { - for (ExecutableElement elem : annotation.getElementValues().keySet()) { - if (elem.getSimpleName().contentEquals(key)) { - return annotation.getElementValues().get(elem); - } - } - - return null; - } - - @SuppressWarnings("unchecked") - public static List unfold(List list) { - if (list == null) { - return Collections.emptyList(); - } - - List unfolded = new ArrayList(list.size()); - for (AnnotationValue value : list) { - unfolded.add((T)value.getValue()); - } - - return unfolded; - } - public static PackageElement getPackage(TypeElement elem) { Element parent = elem.getEnclosingElement(); while (parent != null && !(parent instanceof PackageElement)) { @@ -205,7 +113,7 @@ public abstract class MirrorUtils { public static String getDescriptor(Element elem) { if (elem instanceof ExecutableElement) { - return MirrorUtils.generateSignature((ExecutableElement)elem); + return MirrorUtils.getDescriptor((ExecutableElement)elem); } else if (elem instanceof VariableElement) { return MirrorUtils.getInternalName((VariableElement)elem); } @@ -234,14 +142,15 @@ public abstract class MirrorUtils { return MirrorUtils.getInternalName(elem).replace('/', '.'); } - public static String generateSignature(ExecutableElement method) { + public static String getDescriptor(ExecutableElement method) { StringBuilder signature = new StringBuilder(); for (VariableElement var : method.getParameters()) { signature.append(MirrorUtils.getInternalName(var)); } - return String.format("(%s)%s", signature, MirrorUtils.getInternalName(method.getReturnType())); + String returnType = MirrorUtils.getInternalName(method.getReturnType()); + return String.format("(%s)%s", signature, returnType); } public static String getInternalName(VariableElement var) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 8c538940..b3c759ff 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -26,12 +26,9 @@ package org.spongepowered.tools.obfuscation; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import javax.annotation.processing.Messager; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; @@ -41,7 +38,6 @@ import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementAccessor; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerAccessor.AnnotatedElementInvoker; import org.spongepowered.tools.obfuscation.AnnotatedMixinElementHandlerInjector.AnnotatedElementInjectionPoint; @@ -63,7 +59,7 @@ class AnnotatedMixin { /** * Mixin annotation */ - private final AnnotationMirror annotation; + private final AnnotationHandle annotation; /** * Messager @@ -141,13 +137,13 @@ class AnnotatedMixin { private final AnnotatedMixinElementHandlerSoftImplements softImplements; public AnnotatedMixin(IMixinAnnotationProcessor ap, TypeElement type) { - this.annotation = MirrorUtils.getAnnotation(type, Mixin.class); this.typeProvider = ap.getTypeProvider(); this.obf = ap.getObfuscationManager(); this.mappings = this.obf.createMappingConsumer(); this.messager = ap; this.mixin = type; this.handle = new TypeHandle(type); + this.annotation = this.handle.getAnnotation(Mixin.class); this.classRef = type.getQualifiedName().toString().replace('.', '/'); this.primaryTarget = this.initTargets(); this.remap = AnnotatedMixins.getRemapValue(this.annotation) && this.targets.size() > 0; @@ -174,9 +170,7 @@ class AnnotatedMixin { // Public targets, referenced by class try { - List publicTargets = MirrorUtils.>getAnnotationValue(this.annotation, "value", - Collections.emptyList()); - for (TypeMirror target : MirrorUtils.unfold(publicTargets)) { + for (TypeMirror target : this.annotation.getList()) { TypeHandle type = new TypeHandle((DeclaredType)target); if (this.targets.contains(type)) { continue; @@ -192,9 +186,7 @@ class AnnotatedMixin { // Private targets, referenced by name try { - List privateTargets = MirrorUtils.>getAnnotationValue(this.annotation, "targets", - Collections.emptyList()); - for (String privateTarget : MirrorUtils.unfold(privateTargets)) { + for (String privateTarget : this.annotation.getList("targets")) { TypeHandle type = this.typeProvider.getTypeHandle(privateTarget); if (this.targets.contains(type)) { continue; @@ -226,7 +218,7 @@ class AnnotatedMixin { * Print a message to the AP messager */ private void printMessage(Kind kind, CharSequence msg, AnnotatedMixin mixin) { - this.messager.printMessage(kind, msg, this.mixin, this.annotation); + this.messager.printMessage(kind, msg, this.mixin, this.annotation.asMirror()); } private void addSoftTarget(TypeHandle type, String reference) { @@ -247,7 +239,7 @@ class AnnotatedMixin { return this.mixin.getSimpleName().toString(); } - public AnnotationMirror getAnnotation() { + public AnnotationHandle getAnnotation() { return this.annotation; } @@ -312,35 +304,35 @@ class AnnotatedMixin { return this.mappings; } - public void registerOverwrite(ExecutableElement method, AnnotationMirror overwrite) { + public void registerOverwrite(ExecutableElement method, AnnotationHandle overwrite) { this.overwrites.registerOverwrite(new AnnotatedElementOverwrite(method, overwrite)); } - public void registerShadow(VariableElement field, AnnotationMirror shadow, boolean shouldRemap) { + public void registerShadow(VariableElement field, AnnotationHandle shadow, boolean shouldRemap) { this.shadows.registerShadow(this.shadows.new AnnotatedElementShadowField(field, shadow, shouldRemap)); } - public void registerShadow(ExecutableElement method, AnnotationMirror shadow, boolean shouldRemap) { + public void registerShadow(ExecutableElement method, AnnotationHandle shadow, boolean shouldRemap) { this.shadows.registerShadow(this.shadows.new AnnotatedElementShadowMethod(method, shadow, shouldRemap)); } - public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean shouldRemap) { + public Message registerInjector(ExecutableElement method, AnnotationHandle inject, boolean shouldRemap) { return this.injectors.registerInjector(new AnnotatedElementInjector(method, inject, shouldRemap)); } - public int registerInjectionPoint(ExecutableElement element, AnnotationMirror inject, AnnotationMirror at) { + public int registerInjectionPoint(ExecutableElement element, AnnotationHandle inject, AnnotationHandle at) { return this.injectors.registerInjectionPoint(new AnnotatedElementInjectionPoint(element, inject, at)); } - public void registerAccessor(ExecutableElement element, AnnotationMirror accessor) { + public void registerAccessor(ExecutableElement element, AnnotationHandle accessor) { this.accessors.registerAccessor(new AnnotatedElementAccessor(element, accessor)); } - public void registerInvoker(ExecutableElement element, AnnotationMirror invoker) { + public void registerInvoker(ExecutableElement element, AnnotationHandle invoker) { this.accessors.registerAccessor(new AnnotatedElementInvoker(element, invoker)); } - public void registerSoftImplements(AnnotationMirror implementsAnnotation) { + public void registerSoftImplements(AnnotationHandle implementsAnnotation) { this.softImplements.process(implementsAnnotation); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java index 0a6b501b..ae4f2572 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java @@ -26,11 +26,11 @@ package org.spongepowered.tools.obfuscation; import java.util.List; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.AnnotationValue; +import javax.annotation.processing.Messager; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.VariableElement; +import javax.tools.Diagnostic; import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorException; @@ -63,11 +63,11 @@ abstract class AnnotatedMixinElementHandler { private final E element; - private final AnnotationMirror annotation; + private final AnnotationHandle annotation; private final String desc; - public AnnotatedElement(E element, AnnotationMirror annotation) { + public AnnotatedElement(E element, AnnotationHandle annotation) { this.element = element; this.annotation = annotation; this.desc = MirrorUtils.getDescriptor(element); @@ -77,7 +77,7 @@ abstract class AnnotatedMixinElementHandler { return this.element; } - public AnnotationMirror getAnnotation() { + public AnnotationHandle getAnnotation() { return this.annotation; } @@ -88,6 +88,10 @@ abstract class AnnotatedMixinElementHandler { public String getDesc() { return this.desc; } + + public final void printMessage(Messager messager, Diagnostic.Kind kind, CharSequence msg) { + messager.printMessage(kind, msg, this.element, this.annotation.asMirror()); + } } @@ -108,10 +112,9 @@ abstract class AnnotatedMixinElementHandler { private boolean caseSensitive; - public AliasedElementName(Element element, AnnotationMirror annotation) { + public AliasedElementName(Element element, AnnotationHandle annotation) { this.originalName = element.getSimpleName().toString(); - List aliases = MirrorUtils.>getAnnotationValue(annotation, "aliases"); - this.aliases = MirrorUtils.unfold(aliases); + this.aliases = annotation.getList("aliases"); } public AliasedElementName setCaseSensitive(boolean caseSensitive) { @@ -177,10 +180,10 @@ abstract class AnnotatedMixinElementHandler { */ private String obfuscated; - ShadowElementName(Element element, AnnotationMirror shadow) { + ShadowElementName(Element element, AnnotationHandle shadow) { super(element, shadow); - this.prefix = MirrorUtils.getAnnotationValue(shadow, "prefix", "shadow$"); + this.prefix = shadow.getValue("prefix", "shadow$"); boolean hasPrefix = false; String name = this.originalName; @@ -296,7 +299,7 @@ abstract class AnnotatedMixinElementHandler { return this.mappings; } - protected final boolean remapReference(String key, String reference, Element element, AnnotationMirror inject, AnnotationMirror at) { + protected final boolean remapReference(String key, String reference, Element element, AnnotationHandle inject, AnnotationHandle at) { if (reference == null) { return false; } @@ -305,21 +308,22 @@ abstract class AnnotatedMixinElementHandler { MemberInfo targetMember = MemberInfo.parse(reference); if (!targetMember.isFullyQualified()) { String missing = "missing " + (targetMember.owner == null ? (targetMember.desc == null ? "owner and signature" : "owner") : "signature"); - this.ap.printMessage(Kind.ERROR, annotation + " is not fully qualified, " + missing, element, inject); + this.ap.printMessage(Kind.ERROR, annotation + " is not fully qualified, " + missing, element, inject.asMirror()); return false; } try { targetMember.validate(); } catch (InvalidMemberDescriptorException ex) { - this.ap.printMessage(Kind.ERROR, ex.getMessage(), element, inject); + this.ap.printMessage(Kind.ERROR, ex.getMessage(), element, inject.asMirror()); } try { if (targetMember.isField()) { ObfuscationData obfFieldData = this.obf.getDataProvider().getObfFieldRecursive(targetMember); if (obfFieldData.isEmpty()) { - this.ap.printMessage(Kind.WARNING, "Cannot find field mapping for " + annotation + " '" + reference + "'", element, inject); + this.ap.printMessage(Kind.WARNING, "Cannot find field mapping for " + annotation + " '" + reference + "'", element, + inject.asMirror()); return false; } this.obf.getReferenceManager().addFieldMapping(this.classRef, reference, targetMember, obfFieldData); @@ -327,7 +331,8 @@ abstract class AnnotatedMixinElementHandler { ObfuscationData obfMethodData = this.obf.getDataProvider().getObfMethodRecursive(targetMember); if (obfMethodData.isEmpty()) { if (targetMember.owner == null || !targetMember.owner.startsWith("java/lang/")) { - this.ap.printMessage(Kind.WARNING, "Cannot find method mapping for " + annotation + " '" + reference + "'", element, inject); + this.ap.printMessage(Kind.WARNING, "Cannot find method mapping for " + annotation + " '" + reference + "'", element, + inject.asMirror()); return false; } } @@ -337,7 +342,7 @@ abstract class AnnotatedMixinElementHandler { // Since references are fully-qualified, it shouldn't be possible for there to be multiple mappings, however // we catch and log the error in case something weird happens in the mapping provider this.ap.printMessage(Kind.ERROR, "Unexpected reference conflict for " + annotation + ": " + reference + " -> " - + ex.getNew() + " previously defined as " + ex.getOld(), element, inject); + + ex.getNew() + " previously defined as " + ex.getOld(), element, inject.asMirror()); return false; } @@ -397,20 +402,20 @@ abstract class AnnotatedMixinElementHandler { * @param method Annotated method * @param annotation Annotation to check constraints */ - protected final void checkConstraints(ExecutableElement method, AnnotationMirror annotation) { + protected final void checkConstraints(ExecutableElement method, AnnotationHandle annotation) { try { - Constraint constraint = ConstraintParser.parse(MirrorUtils.getAnnotationValue(annotation, "constraints")); + Constraint constraint = ConstraintParser.parse(annotation.getValue("constraints")); try { constraint.check(this.ap.getTokenProvider()); } catch (ConstraintViolationException ex) { - this.ap.printMessage(Kind.ERROR, ex.getMessage(), method, annotation); + this.ap.printMessage(Kind.ERROR, ex.getMessage(), method, annotation.asMirror()); } } catch (InvalidConstraintException ex) { - this.ap.printMessage(Kind.WARNING, ex.getMessage(), method, annotation); + this.ap.printMessage(Kind.WARNING, ex.getMessage(), method, annotation.asMirror()); } } - protected final void validateTarget(Element element, AnnotationMirror annotation, AliasedElementName name, String type) { + protected final void validateTarget(Element element, AnnotationHandle annotation, AliasedElementName name, String type) { if (element instanceof ExecutableElement) { this.validateTargetMethod((ExecutableElement)element, annotation, name, type); } else if (element instanceof VariableElement) { @@ -422,7 +427,7 @@ abstract class AnnotatedMixinElementHandler { * Checks whether the specified method exists in all targets and raises * warnings where appropriate */ - protected final void validateTargetMethod(ExecutableElement method, AnnotationMirror annotation, AliasedElementName name, String type) { + protected final void validateTargetMethod(ExecutableElement method, AnnotationHandle annotation, AliasedElementName name, String type) { String signature = MirrorUtils.getJavaSignature(method); for (TypeHandle target : this.mixin.getTargets()) { @@ -452,7 +457,7 @@ abstract class AnnotatedMixinElementHandler { } if (targetMethod == null) { - this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " method in " + target, method, annotation); + this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " method in " + target, method, annotation.asMirror()); } } } @@ -461,7 +466,7 @@ abstract class AnnotatedMixinElementHandler { * Checks whether the specified field exists in all targets and raises * warnings where appropriate */ - protected final void validateTargetField(VariableElement field, AnnotationMirror annotation, AliasedElementName name, String type) { + protected final void validateTargetField(VariableElement field, AnnotationHandle annotation, AliasedElementName name, String type) { String fieldType = field.asType().toString(); for (TypeHandle target : this.mixin.getTargets()) { @@ -484,7 +489,7 @@ abstract class AnnotatedMixinElementHandler { } if (targetField == null) { - this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " field in " + target, field, annotation); + this.ap.printMessage(Kind.WARNING, "Cannot find target for " + type + " field in " + target, field, annotation.asMirror()); } } } @@ -493,7 +498,7 @@ abstract class AnnotatedMixinElementHandler { * Checks whether the referenced method exists in all targets and raises * warnings where appropriate */ - protected final void validateReferencedTarget(ExecutableElement method, AnnotationMirror inject, MemberInfo reference, String type) { + protected final void validateReferencedTarget(ExecutableElement method, AnnotationHandle inject, MemberInfo reference, String type) { String signature = reference.toDescriptor(); for (TypeHandle target : this.mixin.getTargets()) { @@ -503,7 +508,7 @@ abstract class AnnotatedMixinElementHandler { MethodHandle targetMethod = target.findMethod(reference.name, signature); if (targetMethod == null) { - this.ap.printMessage(Kind.WARNING, "Cannot find target method for " + type + " in " + target, method, inject); + this.ap.printMessage(Kind.WARNING, "Cannot find target method for " + type + " in " + target, method, inject.asMirror()); } } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java index 6de620c3..66538085 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java @@ -24,7 +24,6 @@ */ package org.spongepowered.tools.obfuscation; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; @@ -58,13 +57,13 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH private String targetName; - public AnnotatedElementAccessor(ExecutableElement element, AnnotationMirror annotation) { + public AnnotatedElementAccessor(ExecutableElement element, AnnotationHandle annotation) { super(element, annotation); this.returnType = this.getElement().getReturnType(); } public String getAnnotationValue() { - return MirrorUtils.getAnnotationValue(this.getAnnotation()); + return this.getAnnotation().getValue(); } public TypeMirror getTargetType() { @@ -113,13 +112,13 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH */ static class AnnotatedElementInvoker extends AnnotatedElementAccessor { - public AnnotatedElementInvoker(ExecutableElement element, AnnotationMirror annotation) { + public AnnotatedElementInvoker(ExecutableElement element, AnnotationHandle annotation) { super(element, annotation); } @Override public String getAccessorDesc() { - return MirrorUtils.generateSignature(this.getElement()); + return MirrorUtils.getDescriptor(this.getElement()); } @Override @@ -155,13 +154,13 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH public void registerAccessor(AnnotatedElementAccessor elem) { if (elem.getAccessorType() == null) { - this.ap.printMessage(Kind.WARNING, "Unsupported accessor type", elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.WARNING, "Unsupported accessor type"); return; } String targetName = this.getAccessorTargetName(elem); if (targetName == null) { - this.ap.printMessage(Kind.WARNING, "Cannot inflect accessor target name", elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.WARNING, "Cannot inflect accessor target name"); return; } elem.setTargetName(targetName); @@ -178,16 +177,14 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH private void registerAccessorForTarget(AnnotatedElementAccessor elem, TypeHandle target) { FieldHandle targetField = target.findField(elem.getTargetName(), elem.getTargetTypeName(), false); if (targetField == null) { - this.ap.printMessage(Kind.ERROR, "Could not locate @Accessor target " + elem + " in target " + target, elem.getElement(), - elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, "Could not locate @Accessor target " + elem + " in target " + target); return; } ObfuscationData obfData = this.obf.getDataProvider().getObfField(targetField.asMapping().move(target.getName())); if (obfData.isEmpty()) { String info = this.mixin.isMultiTarget() ? " in target " + target : ""; - this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Accessor target " + elem, - elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Accessor target " + elem); return; } @@ -196,24 +193,22 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH try { this.obf.getReferenceManager().addFieldMapping(this.mixin.getClassRef(), elem.getTargetName(), elem.getContext(), obfData); } catch (ReferenceConflictException ex) { - this.ap.printMessage(Kind.ERROR, "Mapping conflict for @Accessor target " + elem + ": " + ex.getNew() + " for target " - + target + " conflicts with existing mapping " + ex.getOld(), elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, "Mapping conflict for @Accessor target " + elem + ": " + ex.getNew() + " for target " + + target + " conflicts with existing mapping " + ex.getOld()); } } private void registerInvokerForTarget(AnnotatedElementInvoker elem, TypeHandle target) { MethodHandle targetMethod = target.findMethod(elem.getTargetName(), elem.getTargetTypeName(), false); if (targetMethod == null) { - this.ap.printMessage(Kind.ERROR, "Could not locate @Invoker target " + elem + " in target " + target, elem.getElement(), - elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, "Could not locate @Invoker target " + elem + " in target " + target); return; } ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(targetMethod.asMapping().move(target.getName())); if (obfData.isEmpty()) { String info = this.mixin.isMultiTarget() ? " in target " + target : ""; - this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Accessor target " + elem, - elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Accessor target " + elem); return; } @@ -222,8 +217,8 @@ public class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementH try { this.obf.getReferenceManager().addMethodMapping(this.mixin.getClassRef(), elem.getTargetName(), elem.getContext(), obfData); } catch (ReferenceConflictException ex) { - this.ap.printMessage(Kind.ERROR, "Mapping conflict for @Invoker target " + elem + ": " + ex.getNew() + " for target " - + target + " conflicts with existing mapping " + ex.getOld(), elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, "Mapping conflict for @Invoker target " + elem + ": " + ex.getNew() + " for target " + + target + " conflicts with existing mapping " + ex.getOld()); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java index 9811a354..3dbc6029 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java @@ -25,11 +25,8 @@ package org.spongepowered.tools.obfuscation; import java.util.HashMap; -import java.util.List; import java.util.Map; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic.Kind; @@ -37,7 +34,6 @@ import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorExcep import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.Constants; -import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.ReferenceManager.ReferenceConflictException; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.struct.Message; @@ -54,7 +50,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler private final boolean shouldRemap; - public AnnotatedElementInjector(ExecutableElement element, AnnotationMirror annotation, boolean shouldRemap) { + public AnnotatedElementInjector(ExecutableElement element, AnnotationHandle annotation, boolean shouldRemap) { super(element, annotation); this.shouldRemap = shouldRemap; } @@ -65,7 +61,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler @Override public String toString() { - return "@" + this.getAnnotation().getAnnotationType().asElement().getSimpleName(); + return this.getAnnotation().toString(); } } @@ -75,14 +71,14 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler */ static class AnnotatedElementInjectionPoint extends AnnotatedElement { - private final AnnotationMirror at; + private final AnnotationHandle at; - public AnnotatedElementInjectionPoint(ExecutableElement element, AnnotationMirror inject, AnnotationMirror at) { + public AnnotatedElementInjectionPoint(ExecutableElement element, AnnotationHandle inject, AnnotationHandle at) { super(element, inject); this.at = at; } - public AnnotationMirror getAt() { + public AnnotationHandle getAt() { return this.at; } @@ -97,7 +93,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement()); } - String reference = MirrorUtils.getAnnotationValue(elem.getAnnotation(), "method"); + String reference = elem.getAnnotation().getValue("method"); MemberInfo targetMember = MemberInfo.parse(reference); if (targetMember.name == null) { return null; @@ -106,7 +102,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler try { targetMember.validate(); } catch (InvalidMemberDescriptorException ex) { - this.ap.printMessage(Kind.ERROR, ex.getMessage(), elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, ex.getMessage()); } if (targetMember.desc != null) { @@ -131,11 +127,10 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler if (desc == null) { Kind error = this.mixin.isMultiTarget() ? Kind.ERROR : Kind.WARNING; if (target.isImaginary()) { - this.ap.printMessage(error, elem + " target requires method signature because enclosing type information for " - + target + " is unavailable", elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, error, elem + " target requires method signature because enclosing type information for " + + target + " is unavailable"); } else if (!Constants.CTOR.equals(targetMember.name)) { - this.ap.printMessage(error, "Unable to determine signature for " + elem + " target method", - elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, error, "Unable to determine signature for " + elem + " target method"); } return null; } @@ -156,8 +151,8 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler this.obf.getReferenceManager().addMethodMapping(this.classRef, reference, obfData); } catch (ReferenceConflictException ex) { String conflictType = this.mixin.isMultiTarget() ? "Multi-target" : "Target"; - this.ap.printMessage(Kind.ERROR, conflictType + " reference conflict for " + targetName + ": " + reference + " -> " - + ex.getNew() + " previously defined as " + ex.getOld(), elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, conflictType + " reference conflict for " + targetName + ": " + reference + " -> " + + ex.getNew() + " previously defined as " + ex.getOld()); } return null; @@ -176,8 +171,8 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler return 0; } - String type = MirrorUtils.getAnnotationValue(elem.getAt(), "value"); - String target = MirrorUtils.getAnnotationValue(elem.getAt(), "target"); + String type = elem.getAt().getValue("value"); + String target = elem.getAt().getValue("target"); int remapped = this.remapReference(type + ".", target, elem.getElement(), elem.getAnnotation(), elem.getAt()) ? 1 : 0; // Pattern for replacing references in args, not used yet @@ -189,21 +184,17 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler return remapped; } - static Map getAtArgs(AnnotationMirror at) { + static Map getAtArgs(AnnotationHandle at) { Map args = new HashMap(); - List argv = MirrorUtils.>getAnnotationValue(at, "args"); - if (argv != null) { - for (AnnotationValue av : argv) { - String arg = (String)av.getValue(); - if (arg == null) { - continue; - } - int eqPos = arg.indexOf('='); - if (eqPos > -1) { - args.put(arg.substring(0, eqPos), arg.substring(eqPos + 1)); - } else { - args.put(arg, ""); - } + for (String arg : at.getList("args")) { + if (arg == null) { + continue; + } + int eqPos = arg.indexOf('='); + if (eqPos > -1) { + args.put(arg.substring(0, eqPos), arg.substring(eqPos + 1)); + } else { + args.put(arg, ""); } } return args; diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java index 6c037bf7..ced47a78 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java @@ -26,7 +26,6 @@ package org.spongepowered.tools.obfuscation; import java.lang.reflect.Method; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic.Kind; @@ -44,7 +43,7 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler */ static class AnnotatedElementOverwrite extends AnnotatedElement { - public AnnotatedElementOverwrite(ExecutableElement element, AnnotationMirror annotation) { + public AnnotatedElementOverwrite(ExecutableElement element, AnnotationHandle annotation) { super(element, annotation); } @@ -113,8 +112,8 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler try { this.addMethodMappings(elem.getSimpleName(), elem.getDesc(), obfData); } catch (MappingConflictException ex) { - this.ap.printMessage(Kind.ERROR, "Mapping conflict for @Overwrite method: " + ex.getNew().getSimpleName() + " for target " + target - + " conflicts with existing mapping " + ex.getOld().getSimpleName(), elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, "Mapping conflict for @Overwrite method: " + ex.getNew().getSimpleName() + " for target " + target + + " conflicts with existing mapping " + ex.getOld().getSimpleName()); return false; } return true; diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java index 89f4c67c..5fba1e47 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerShadow.java @@ -24,7 +24,6 @@ */ package org.spongepowered.tools.obfuscation; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.VariableElement; @@ -57,7 +56,7 @@ class AnnotatedMixinElementHandlerShadow extends AnnotatedMixinElementHandler { private final Type type; - protected AnnotatedElementShadow(E element, AnnotationMirror annotation, boolean shouldRemap, Type type) { + protected AnnotatedElementShadow(E element, AnnotationHandle annotation, boolean shouldRemap, Type type) { super(element, annotation); this.shouldRemap = shouldRemap; this.name = new ShadowElementName(element, annotation); @@ -104,7 +103,7 @@ class AnnotatedMixinElementHandlerShadow extends AnnotatedMixinElementHandler { */ class AnnotatedElementShadowField extends AnnotatedElementShadow { - public AnnotatedElementShadowField(VariableElement element, AnnotationMirror annotation, boolean shouldRemap) { + public AnnotatedElementShadowField(VariableElement element, AnnotationHandle annotation, boolean shouldRemap) { super(element, annotation, shouldRemap, Type.FIELD); } @@ -125,7 +124,7 @@ class AnnotatedMixinElementHandlerShadow extends AnnotatedMixinElementHandler { */ class AnnotatedElementShadowMethod extends AnnotatedElementShadow { - public AnnotatedElementShadowMethod(ExecutableElement element, AnnotationMirror annotation, boolean shouldRemap) { + public AnnotatedElementShadowMethod(ExecutableElement element, AnnotationHandle annotation, boolean shouldRemap) { super(element, annotation, shouldRemap, Type.METHOD); } @@ -165,8 +164,7 @@ class AnnotatedMixinElementHandlerShadow extends AnnotatedMixinElementHandler { if (obfData.isEmpty()) { String info = this.mixin.isMultiTarget() ? " in target " + target : ""; - this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Shadow " + elem, - elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.WARNING, "Unable to locate obfuscation mapping" + info + " for @Shadow " + elem); return; } @@ -174,8 +172,8 @@ class AnnotatedMixinElementHandlerShadow extends AnnotatedMixinElementHandler { try { elem.addMapping(type, obfData.get(type)); } catch (MappingConflictException ex) { - this.ap.printMessage(Kind.ERROR, "Mapping conflict for @Shadow " + elem + ": " + ex.getNew().getSimpleName() + " for target " - + target + " conflicts with existing mapping " + ex.getOld().getSimpleName(), elem.getElement(), elem.getAnnotation()); + elem.printMessage(this.ap, Kind.ERROR, "Mapping conflict for @Shadow " + elem + ": " + ex.getNew().getSimpleName() + " for target " + + target + " conflicts with existing mapping " + ex.getOld().getSimpleName()); } } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java index ec91016f..ad83108f 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java @@ -24,10 +24,8 @@ */ package org.spongepowered.tools.obfuscation; -import java.util.Collections; import java.util.List; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.DeclaredType; @@ -56,29 +54,28 @@ public class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinEl * * @param implementsAnnotation */ - public void process(AnnotationMirror implementsAnnotation) { - List interfaces = MirrorUtils.>getAnnotationValue(implementsAnnotation, "value", - Collections.emptyList()); + public void process(AnnotationHandle implementsAnnotation) { + List interfaces = implementsAnnotation.getAnnotationList("value"); // Derp? if (interfaces.size() < 1) { - this.ap.printMessage(Kind.WARNING, "Empty @Implements annotation", this.mixin.getMixin(), implementsAnnotation); + this.ap.printMessage(Kind.WARNING, "Empty @Implements annotation", this.mixin.getMixin(), implementsAnnotation.asMirror()); return; } - for (AnnotationMirror interfaceAnnotation : interfaces) { - Remap remap = MirrorUtils.getAnnotationValue(interfaceAnnotation, "remap", Remap.ALL); + for (AnnotationHandle interfaceAnnotation : interfaces) { + Remap remap = interfaceAnnotation.getValue("remap", Remap.ALL); if (remap == Remap.NONE) { continue; } try { - TypeHandle iface = new TypeHandle(MirrorUtils.getAnnotationValue(interfaceAnnotation, "iface")); - String prefix = MirrorUtils.getAnnotationValue(interfaceAnnotation, "prefix"); + TypeHandle iface = new TypeHandle(interfaceAnnotation.getValue("iface")); + String prefix = interfaceAnnotation.getValue("prefix"); this.processSoftImplements(remap, iface, prefix); } catch (Exception ex) { this.ap.printMessage(Kind.ERROR, "Unexpected error: " + ex.getClass().getName() + ": " + ex.getMessage(), this.mixin.getMixin(), - interfaceAnnotation); + interfaceAnnotation.asMirror()); } } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 3476e279..76ba538b 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -58,7 +58,6 @@ import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.util.ITokenProvider; -import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.interfaces.IJavadocProvider; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator; @@ -357,7 +356,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp return; } - mixinClass.registerAccessor(method, MirrorUtils.getAnnotation(method, Accessor.class)); + mixinClass.registerAccessor(method, AnnotationHandle.of(method, Accessor.class)); } /** @@ -373,7 +372,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp return; } - mixinClass.registerInvoker(method, MirrorUtils.getAnnotation(method, Invoker.class)); + mixinClass.registerInvoker(method, AnnotationHandle.of(method, Invoker.class)); } /** @@ -389,7 +388,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp return; } - mixinClass.registerOverwrite(method, MirrorUtils.getAnnotation(method, Overwrite.class)); + mixinClass.registerOverwrite(method, AnnotationHandle.of(method, Overwrite.class)); } /** @@ -399,7 +398,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp * @param field Shadow field * @param shadow {@link org.spongepowered.asm.mixin.Shadow} annotation */ - public void registerShadow(TypeElement mixinType, VariableElement field, AnnotationMirror shadow) { + public void registerShadow(TypeElement mixinType, VariableElement field, AnnotationHandle shadow) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { this.printMessage(Kind.ERROR, "Found @Shadow annotation on a non-mixin field", field); @@ -416,7 +415,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp * @param method Shadow method * @param shadow {@link org.spongepowered.asm.mixin.Shadow} annotation */ - public void registerShadow(TypeElement mixinType, ExecutableElement method, AnnotationMirror shadow) { + public void registerShadow(TypeElement mixinType, ExecutableElement method, AnnotationHandle shadow) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { this.printMessage(Kind.ERROR, "Found @Shadow annotation on a non-mixin method", method); @@ -434,11 +433,10 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp * @param inject {@link org.spongepowered.asm.mixin.injection.Inject} * annotation */ - public void registerInjector(TypeElement mixinType, ExecutableElement method, AnnotationMirror inject) { + public void registerInjector(TypeElement mixinType, ExecutableElement method, AnnotationHandle inject) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { - String type = "@" + inject.getAnnotationType().asElement().getSimpleName() + ""; - this.printMessage(Kind.ERROR, "Found " + type + " annotation on a non-mixin method", method); + this.printMessage(Kind.ERROR, "Found " + inject + " annotation on a non-mixin method", method); return; } @@ -446,7 +444,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp Message errorMessage = mixinClass.registerInjector(method, inject, remap); int remappedAts = 0; if (remap) { - Object ats = MirrorUtils.getAnnotationValue(inject, "at"); + Object ats = inject.getValue("at"); if (ats instanceof List) { List annotationList = (List)ats; @@ -456,18 +454,18 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp at = ((AnnotationValue)at).getValue(); } if (at instanceof AnnotationMirror) { - remappedAts += mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)at); + remappedAts += mixinClass.registerInjectionPoint(method, inject, new AnnotationHandle((AnnotationMirror)at)); } else { this.printMessage(Kind.WARNING, "No annotation mirror on " + at.getClass().getName()); } } } else if (ats instanceof AnnotationMirror) { - remappedAts += mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)ats); + remappedAts += mixinClass.registerInjectionPoint(method, inject, new AnnotationHandle((AnnotationMirror)ats)); } else if (ats instanceof AnnotationValue) { // Fix for JDT Object mirror = ((AnnotationValue)ats).getValue(); if (mirror instanceof AnnotationMirror) { - remappedAts += mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)mirror); + remappedAts += mixinClass.registerInjectionPoint(method, inject, new AnnotationHandle((AnnotationMirror)mirror)); } } } @@ -488,7 +486,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp * @param implementsAnnotation * {@link org.spongepowered.asm.mixin.Implements} annotation */ - public void registerSoftImplements(TypeElement mixin, AnnotationMirror implementsAnnotation) { + public void registerSoftImplements(TypeElement mixin, AnnotationHandle implementsAnnotation) { AnnotatedMixin mixinClass = this.getMixin(mixin); if (mixinClass == null) { this.printMessage(Kind.ERROR, "Found @Implements annotation on a non-mixin class"); @@ -519,15 +517,15 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp } } - private boolean shouldRemap(AnnotatedMixin mixinClass, AnnotationMirror annotation) { + private boolean shouldRemap(AnnotatedMixin mixinClass, AnnotationHandle annotation) { return mixinClass.remap() && AnnotatedMixins.getRemapValue(annotation); } /** * Check whether we should remap the annotated member or skip it */ - public static boolean getRemapValue(AnnotationMirror annotation) { - return MirrorUtils.getAnnotationValue(annotation, "remap", Boolean.TRUE).booleanValue(); + public static boolean getRemapValue(AnnotationHandle annotation) { + return annotation.getValue("remap", Boolean.TRUE).booleanValue(); } /** diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotationHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotationHandle.java new file mode 100644 index 00000000..3d82c25c --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotationHandle.java @@ -0,0 +1,221 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; + +/** + * A wrapper for {@link AnnotationMirror} which provides a more convenient way + * to access annotation values. + */ +public class AnnotationHandle { + + /** + * Annotation being wrapped + */ + private final AnnotationMirror annotation; + + /** + * ctor + * + * @param annotation annotation to wrap + */ + AnnotationHandle(AnnotationMirror annotation) { + this.annotation = annotation; + } + + /** + * Return the wrapped mirror, only used to pass to Messager methods + * + * @return + */ + public AnnotationMirror asMirror() { + return this.annotation; + } + + /** + * Get whether the annotation mirror actually exists, if the mirror is null + * returns false + * + * @return true if the annotation exists + */ + public boolean exists() { + return this.annotation != null; + } + + @Override + public String toString() { + return "@" + this.annotation.getAnnotationType().asElement().getSimpleName(); + } + + /** + * Get a value with the specified key from this annotation, return the + * specified default value if the key is not set or is not present + * + * @param key key + * @param defaultValue value to return if the key is not set or not present + * @return value or default if not set + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + public T getValue(String key, T defaultValue) { + if (this.annotation == null) { + return defaultValue; + } + + AnnotationValue value = this.getAnnotationValue0(key); + if (defaultValue instanceof Enum && value != null) { + VariableElement varValue = (VariableElement)value.getValue(); + if (varValue == null) { + return defaultValue; + } + return (T)Enum.valueOf((Class)defaultValue.getClass(), varValue.getSimpleName().toString()); + } + + return value != null ? (T)value.getValue() : defaultValue; + } + + /** + * Get the annotation value or return null if not present or not set + * + * @return value or null if not present or not set + */ + public T getValue() { + return this.getValue("value", null); + } + + /** + * Get the annotation value with the specified key or return null if not + * present or not set + * + * @return value or null if not present or not set + */ + public T getValue(String key) { + return this.getValue(key, null); + } + + /** + * Retrieve the annotation value as a list with values of the specified + * type. Returns an empty list if the value is not present or not set. + * + * @return list of values + */ + public List getList() { + return this.getList("value"); + } + + /** + * Retrieve the annotation value with the specified key as a list with + * values of the specified type. Returns an empty list if the value is not + * present or not set. + * + * @return list of values + */ + public List getList(String key) { + List list = this.>getValue(key, Collections.emptyList()); + return AnnotationHandle.unfold(list); + } + + /** + * Retrieve an annotation key as a list of annotation handles + * + * @param key key to fetch + * @return list of annotations + */ + public List getAnnotationList(String key) { + List list = this.>getValue(key, null); + if (list == null) { + return Collections.emptyList(); + } + + List annotations = new ArrayList(list.size()); + for (AnnotationValue value : list) { + annotations.add(new AnnotationHandle((AnnotationMirror)value.getValue())); + } + return annotations; + } + + protected AnnotationValue getAnnotationValue0(String key) { + for (ExecutableElement elem : this.annotation.getElementValues().keySet()) { + if (elem.getSimpleName().contentEquals(key)) { + return this.annotation.getElementValues().get(elem); + } + } + + return null; + } + + @SuppressWarnings("unchecked") + protected static List unfold(List list) { + if (list == null) { + return Collections.emptyList(); + } + + List unfolded = new ArrayList(list.size()); + for (AnnotationValue value : list) { + unfolded.add((T)value.getValue()); + } + + return unfolded; + } + + protected static AnnotationMirror getAnnotation(Element elem, Class annotationClass) { + if (elem == null) { + return null; + } + + List annotations = elem.getAnnotationMirrors(); + + if (annotations == null) { + return null; + } + + for (AnnotationMirror annotation : annotations) { + Element element = annotation.getAnnotationType().asElement(); + if (!(element instanceof TypeElement)) { + continue; + } + TypeElement annotationElement = (TypeElement)element; + if (annotationElement.getQualifiedName().contentEquals(annotationClass.getName())) { + return annotation; + } + } + + return null; + } + + public static AnnotationHandle of(Element elem, Class annotationClass) { + return new AnnotationHandle(AnnotationHandle.getAnnotation(elem, annotationClass)); + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorInjection.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorInjection.java index ccc05416..933ba617 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorInjection.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorInjection.java @@ -29,7 +29,6 @@ import java.util.Set; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; @@ -101,7 +100,7 @@ public class MixinObfuscationProcessorInjection extends MixinObfuscationProcesso + MirrorUtils.getElementType(parent)); } - AnnotationMirror inject = MirrorUtils.getAnnotation(elem, injectorClass); + AnnotationHandle inject = AnnotationHandle.of(elem, injectorClass); if (elem.getKind() == ElementKind.METHOD) { this.mixins.registerInjector((TypeElement)parent, (ExecutableElement)elem, inject); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java index af9bf312..34fe5ee1 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinObfuscationProcessorTargets.java @@ -28,7 +28,6 @@ import java.util.Set; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; @@ -102,7 +101,7 @@ public class MixinObfuscationProcessorTargets extends MixinObfuscationProcessor continue; } - AnnotationMirror shadow = MirrorUtils.getAnnotation(elem, Shadow.class); + AnnotationHandle shadow = AnnotationHandle.of(elem, Shadow.class); if (elem.getKind() == ElementKind.FIELD) { this.mixins.registerShadow((TypeElement)parent, (VariableElement)elem, shadow); @@ -181,7 +180,7 @@ public class MixinObfuscationProcessorTargets extends MixinObfuscationProcessor private void processImplements(RoundEnvironment roundEnv) { for (Element elem : roundEnv.getElementsAnnotatedWith(Implements.class)) { if (elem.getKind() == ElementKind.CLASS || elem.getKind() == ElementKind.INTERFACE) { - AnnotationMirror implementsAnnotation = MirrorUtils.getAnnotation(elem, Implements.class); + AnnotationHandle implementsAnnotation = AnnotationHandle.of(elem, Implements.class); this.mixins.registerSoftImplements((TypeElement)elem, implementsAnnotation); } else { this.mixins.printMessage(Kind.ERROR, "Found an @Implements annotation on an element which is not a class or interface", elem); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java index 3dbab5c1..21a65218 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java @@ -28,7 +28,6 @@ import java.util.Collection; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; @@ -81,10 +80,11 @@ public abstract class MixinValidator implements IMixinValidator { * @see org.spongepowered.tools.obfuscation.IMixinValidator#validate( * org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass, * javax.lang.model.element.TypeElement, - * javax.lang.model.element.AnnotationMirror, java.util.Collection) + * org.spongepowered.tools.obfuscation.AnnotationHandle, + * java.util.Collection) */ @Override - public final boolean validate(ValidationPass pass, TypeElement mixin, AnnotationMirror annotation, Collection targets) { + public final boolean validate(ValidationPass pass, TypeElement mixin, AnnotationHandle annotation, Collection targets) { if (pass != this.pass) { return true; } @@ -92,7 +92,7 @@ public abstract class MixinValidator implements IMixinValidator { return this.validate(mixin, annotation, targets); } - protected abstract boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection targets); + protected abstract boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection targets); /** * Output a compiler note diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java index 63dfd818..e8c7dc3f 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TypeHandle.java @@ -24,6 +24,7 @@ */ package org.spongepowered.tools.obfuscation; +import java.lang.annotation.Annotation; import java.util.Collections; import java.util.List; @@ -134,6 +135,10 @@ public class TypeHandle { return this.element; } + public AnnotationHandle getAnnotation(Class annotationClass) { + return AnnotationHandle.of(this.element, annotationClass); + } + /** * Returns enclosed elements (methods, fields, etc.) */ @@ -233,7 +238,7 @@ public class TypeHandle { if (desc == null) { for (ExecutableElement method : this.getEnclosedElements(ElementKind.METHOD)) { if (method.getSimpleName().toString().equals(memberInfo.name)) { - desc = MirrorUtils.generateSignature(method); + desc = MirrorUtils.getDescriptor(method); break; } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java index 3b5488df..365e520f 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java @@ -26,9 +26,9 @@ package org.spongepowered.tools.obfuscation.interfaces; import java.util.Collection; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.TypeElement; +import org.spongepowered.tools.obfuscation.AnnotationHandle; import org.spongepowered.tools.obfuscation.TypeHandle; /** @@ -55,5 +55,5 @@ public interface IMixinValidator { * * @return False to halt processing of further validators */ - public abstract boolean validate(ValidationPass pass, TypeElement mixin, AnnotationMirror annotation, Collection targets); + public abstract boolean validate(ValidationPass pass, TypeElement mixin, AnnotationHandle annotation, Collection targets); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/struct/Message.java b/src/ap/java/org/spongepowered/tools/obfuscation/struct/Message.java index c18cb8fc..35702e89 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/struct/Message.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/struct/Message.java @@ -30,6 +30,8 @@ import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.tools.Diagnostic; +import org.spongepowered.tools.obfuscation.AnnotationHandle; + /** * Wrapper for Annotation Processor messages, used to enable messages to be * easily queued and manipulated @@ -43,17 +45,25 @@ public class Message { private final AnnotationValue value; public Message(Diagnostic.Kind kind, CharSequence msg) { - this(kind, msg, null, null, null); + this(kind, msg, null, (AnnotationMirror)null, null); } public Message(Diagnostic.Kind kind, CharSequence msg, Element element) { - this(kind, msg, element, null, null); + this(kind, msg, element, (AnnotationMirror)null, null); + } + + public Message(Diagnostic.Kind kind, CharSequence msg, Element element, AnnotationHandle annotation) { + this(kind, msg, element, annotation.asMirror(), null); } public Message(Diagnostic.Kind kind, CharSequence msg, Element element, AnnotationMirror annotation) { this(kind, msg, element, annotation, null); } + public Message(Diagnostic.Kind kind, CharSequence msg, Element element, AnnotationHandle annotation, AnnotationValue value) { + this(kind, msg, element, annotation.asMirror(), value); + } + public Message(Diagnostic.Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, AnnotationValue value) { this.kind = kind; this.msg = msg; diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java index a2ebd5be..498a3fa6 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java @@ -26,11 +26,11 @@ package org.spongepowered.tools.obfuscation.validation; import java.util.Collection; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ElementKind; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; +import org.spongepowered.tools.obfuscation.AnnotationHandle; import org.spongepowered.tools.obfuscation.MixinValidator; import org.spongepowered.tools.obfuscation.TypeHandle; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; @@ -52,10 +52,11 @@ public class ParentValidator extends MixinValidator { /* (non-Javadoc) * @see org.spongepowered.tools.obfuscation.MixinValidator * #validate(javax.lang.model.element.TypeElement, - * javax.lang.model.element.AnnotationMirror, java.util.Collection) + * org.spongepowered.tools.obfuscation.AnnotationHandle, + * java.util.Collection) */ @Override - public boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection targets) { + public boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection targets) { if (mixin.getEnclosingElement().getKind() != ElementKind.PACKAGE && !mixin.getModifiers().contains(Modifier.STATIC)) { this.error("Inner class mixin must be declared static", mixin); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java index b853e90d..484549fd 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -26,7 +26,6 @@ package org.spongepowered.tools.obfuscation.validation; import java.util.Collection; -import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; @@ -37,6 +36,7 @@ import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.tools.MirrorUtils; +import org.spongepowered.tools.obfuscation.AnnotationHandle; import org.spongepowered.tools.obfuscation.MixinValidator; import org.spongepowered.tools.obfuscation.SupportedOptions; import org.spongepowered.tools.obfuscation.TypeHandle; @@ -59,10 +59,11 @@ public class TargetValidator extends MixinValidator { /* (non-Javadoc) * @see org.spongepowered.tools.obfuscation.MixinValidator * #validate(javax.lang.model.element.TypeElement, - * javax.lang.model.element.AnnotationMirror, java.util.Collection) + * org.spongepowered.tools.obfuscation.AnnotationHandle, + * java.util.Collection) */ @Override - public boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection targets) { + public boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection targets) { if ("true".equalsIgnoreCase(this.options.getOption(SupportedOptions.DISABLE_TARGET_VALIDATOR))) { return true; } @@ -80,8 +81,8 @@ public class TargetValidator extends MixinValidator { boolean containsNonAccessorMethod = false; for (Element element : mixin.getEnclosedElements()) { if (element.getKind() == ElementKind.METHOD) { - boolean isAccessor = MirrorUtils.getAnnotation(element, Accessor.class) != null; - boolean isInvoker = MirrorUtils.getAnnotation(element, Invoker.class) != null; + boolean isAccessor = AnnotationHandle.of(element, Accessor.class).exists(); + boolean isInvoker = AnnotationHandle.of(element, Invoker.class).exists(); containsNonAccessorMethod |= (!isAccessor && !isInvoker); } }