Refactor annotation handling in the AP

This commit is contained in:
Mumfrey committed 2016-11-23 17:53:43 +00:00
1 parent 4934071ee7
commit c007eabc31
18 files changed
+378 -258

No files matched your search

@@ -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<? extends Annotation> annotationClass) {
List<? extends AnnotationMirror> 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> 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<? extends Enum>)defaultValue.getClass(), varValue.getSimpleName().toString());
}
return value != null ? (T)value.getValue() : defaultValue;
}
public static <T> T getAnnotationValue(TypeElement elem, Class<? extends Annotation> annotationClass, String key, T defaultValue) {
AnnotationMirror annotation = MirrorUtils.getAnnotation(elem, annotationClass);
return MirrorUtils.getAnnotationValue(annotation, key, defaultValue);
}
public static <T> T getAnnotationValue(AnnotationMirror annotation) {
return MirrorUtils.getAnnotationValue(annotation, "value", null);
}
public static <T> T getAnnotationValue(AnnotationMirror annotation, String key) {
return MirrorUtils.getAnnotationValue(annotation, key, null);
}
public static <T> T getAnnotationValue(TypeElement elem, Class<? extends Annotation> annotationClass) {
return MirrorUtils.getAnnotationValue(elem, annotationClass, "value");
}
public static <T> T getAnnotationValue(TypeElement elem, Class<? extends Annotation> 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 <T> List<T> unfold(List<AnnotationValue> list) {
if (list == null) {
return Collections.<T>emptyList();
}
List<T> unfolded = new ArrayList<T>(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) {
@@ -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<AnnotationValue> publicTargets = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(this.annotation, "value",
Collections.<AnnotationValue>emptyList());
for (TypeMirror target : MirrorUtils.<TypeMirror>unfold(publicTargets)) {
for (TypeMirror target : this.annotation.<TypeMirror>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<AnnotationValue> privateTargets = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(this.annotation, "targets",
Collections.<AnnotationValue>emptyList());
for (String privateTarget : MirrorUtils.<String>unfold(privateTargets)) {
for (String privateTarget : this.annotation.<String>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);
}
@@ -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<AnnotationValue> aliases = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(annotation, "aliases");
this.aliases = MirrorUtils.<String>unfold(aliases);
this.aliases = annotation.<String>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.<String>getAnnotationValue(shadow, "prefix", "shadow$");
this.prefix = shadow.<String>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<MappingField> 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<MappingMethod> 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.<String>getAnnotationValue(annotation, "constraints"));
Constraint constraint = ConstraintParser.parse(annotation.<String>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());
}
}
}
@@ -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().<String>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<MappingField> 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<MappingMethod> 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());
}
}
@@ -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<ExecutableElement> {
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.<String>getAnnotationValue(elem.getAnnotation(), "method");
String reference = elem.getAnnotation().<String>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.<String>getAnnotationValue(elem.getAt(), "value");
String target = MirrorUtils.<String>getAnnotationValue(elem.getAt(), "target");
String type = elem.getAt().<String>getValue("value");
String target = elem.getAt().<String>getValue("target");
int remapped = this.remapReference(type + ".<target>", 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<String, String> getAtArgs(AnnotationMirror at) {
static Map<String, String> getAtArgs(AnnotationHandle at) {
Map<String, String> args = new HashMap<String, String>();
List<AnnotationValue> argv = MirrorUtils.<List<AnnotationValue>>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.<String>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;
@@ -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<ExecutableElement> {
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;
@@ -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<VariableElement, MappingField> {
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<ExecutableElement, MappingMethod> {
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());
}
}
}
@@ -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<AnnotationMirror> interfaces = MirrorUtils.<List<AnnotationMirror>>getAnnotationValue(implementsAnnotation, "value",
Collections.<AnnotationMirror>emptyList());
public void process(AnnotationHandle implementsAnnotation) {
List<AnnotationHandle> 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.<Remap>getAnnotationValue(interfaceAnnotation, "remap", Remap.ALL);
for (AnnotationHandle interfaceAnnotation : interfaces) {
Remap remap = interfaceAnnotation.<Remap>getValue("remap", Remap.ALL);
if (remap == Remap.NONE) {
continue;
}
try {
TypeHandle iface = new TypeHandle(MirrorUtils.<DeclaredType>getAnnotationValue(interfaceAnnotation, "iface"));
String prefix = MirrorUtils.<String>getAnnotationValue(interfaceAnnotation, "prefix");
TypeHandle iface = new TypeHandle(interfaceAnnotation.<DeclaredType>getValue("iface"));
String prefix = interfaceAnnotation.<String>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());
}
}
}
@@ -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.<Boolean>getAnnotationValue(annotation, "remap", Boolean.TRUE).booleanValue();
public static boolean getRemapValue(AnnotationHandle annotation) {
return annotation.<Boolean>getValue("remap", Boolean.TRUE).booleanValue();
}
/**
@@ -0,0 +1,221 @@
/*
* 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;
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> 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<? extends Enum>)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> 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> 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 <T> List<T> getList() {
return this.<T>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 <T> List<T> getList(String key) {
List<AnnotationValue> list = this.<List<AnnotationValue>>getValue(key, Collections.<AnnotationValue>emptyList());
return AnnotationHandle.<T>unfold(list);
}
/**
* Retrieve an annotation key as a list of annotation handles
*
* @param key key to fetch
* @return list of annotations
*/
public List<AnnotationHandle> getAnnotationList(String key) {
List<AnnotationValue> list = this.<List<AnnotationValue>>getValue(key, null);
if (list == null) {
return Collections.<AnnotationHandle>emptyList();
}
List<AnnotationHandle> annotations = new ArrayList<AnnotationHandle>(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 <T> List<T> unfold(List<AnnotationValue> list) {
if (list == null) {
return Collections.<T>emptyList();
}
List<T> unfolded = new ArrayList<T>(list.size());
for (AnnotationValue value : list) {
unfolded.add((T)value.getValue());
}
return unfolded;
}
protected static AnnotationMirror getAnnotation(Element elem, Class<? extends Annotation> annotationClass) {
if (elem == null) {
return null;
}
List<? extends AnnotationMirror> 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<? extends Annotation> annotationClass) {
return new AnnotationHandle(AnnotationHandle.getAnnotation(elem, annotationClass));
}
}
@@ -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);
@@ -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);
@@ -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<TypeHandle> targets) {
public final boolean validate(ValidationPass pass, TypeElement mixin, AnnotationHandle annotation, Collection<TypeHandle> 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<TypeHandle> targets);
protected abstract boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection<TypeHandle> targets);
/**
* Output a compiler note
@@ -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<? extends Annotation> 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.<ExecutableElement>getEnclosedElements(ElementKind.METHOD)) {
if (method.getSimpleName().toString().equals(memberInfo.name)) {
desc = MirrorUtils.generateSignature(method);
desc = MirrorUtils.getDescriptor(method);
break;
}
}
@@ -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<TypeHandle> targets);
public abstract boolean validate(ValidationPass pass, TypeElement mixin, AnnotationHandle annotation, Collection<TypeHandle> targets);
}
@@ -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;
@@ -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<TypeHandle> targets) {
public boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection<TypeHandle> targets) {
if (mixin.getEnclosingElement().getKind() != ElementKind.PACKAGE && !mixin.getModifiers().contains(Modifier.STATIC)) {
this.error("Inner class mixin must be declared static", mixin);
}
@@ -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<TypeHandle> targets) {
public boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection<TypeHandle> 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);
}
}