From 314db1f25b46e200180ff603efbffa0349ddf2a0 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 16 Jan 2015 15:46:34 +0000 Subject: [PATCH] AP improvements, allow usage in eclipse, log actual position of warnings --- .../org/spongepowered/tools/MirrorUtils.java | 5 +- .../tools/obfuscation/AnnotatedMixin.java | 61 ++++++++------ .../tools/obfuscation/AnnotatedMixins.java | 84 ++++++++++++++----- 3 files changed, 102 insertions(+), 48 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/MirrorUtils.java b/src/ap/java/org/spongepowered/tools/MirrorUtils.java index c9070117..06c841c3 100644 --- a/src/ap/java/org/spongepowered/tools/MirrorUtils.java +++ b/src/ap/java/org/spongepowered/tools/MirrorUtils.java @@ -186,12 +186,15 @@ public abstract class MirrorUtils { return "S"; case DECLARED: return "L" + MirrorUtils.getInternalName((DeclaredType)type) + ";"; + case ERROR: + // TODO figure out a better way to not crash when we get here + return "Ljava/lang/Object;"; case VOID: return "V"; default: } - throw new IllegalArgumentException("Unable to parse type symbol " + type + " to equivalent bytecode type"); + throw new IllegalArgumentException("Unable to parse type symbol " + type + " with " + type.getKind() + " to equivalent bytecode type"); } public static String getInternalName(DeclaredType type) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index d4ffd77d..40a70226 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -112,6 +112,8 @@ class AnnotatedMixin { } } + private static final String CTOR = ""; + /** * Manager */ @@ -167,15 +169,19 @@ class AnnotatedMixin { String targetRef = null; TypeElement targetType = null; - List targetList = MirrorUtils.>getAnnotationValue(annotation); - for (TypeMirror target : MirrorUtils.unfold(targetList)) { - Element element = ((DeclaredType)target).asElement(); - String targetName = ((TypeElement)element).getQualifiedName().toString(); - this.targets.add(targetName); - if (targetRef == null) { - targetRef = MirrorUtils.getInternalName((DeclaredType)target); - targetType = (TypeElement) ((DeclaredType)target).asElement(); + try { + List targetList = MirrorUtils.>getAnnotationValue(annotation); + for (TypeMirror target : MirrorUtils.unfold(targetList)) { + Element element = ((DeclaredType)target).asElement(); + String targetName = ((TypeElement)element).getQualifiedName().toString(); + this.targets.add(targetName); + if (targetRef == null) { + targetRef = MirrorUtils.getInternalName((DeclaredType)target); + targetType = (TypeElement) ((DeclaredType)target).asElement(); + } } + } catch (Exception ex) { + this.mixins.printMessage(Kind.WARNING, "Error processing target list: " + ex.getClass().getSimpleName() + ": "+ ex.getMessage(), type); } this.targetRef = targetRef; @@ -229,9 +235,9 @@ class AnnotatedMixin { * Validate method for {@link org.spongepowered.asm.mixin.Shadow} and {@link org.spongepowered.asm.mixin.Overwrite} registrations to check that * only a single target is registered. Mixins containing annotated methods with these annotations cannot be multi-targetted. */ - private boolean validateSingleTarget(String annotation) { + private boolean validateSingleTarget(String annotation, Element element) { if (this.targetRef == null) { - this.mixins.printMessage(Kind.ERROR, "Mixin class " + this.mixin + " has multiple targets. " + annotation + " is not supported!"); + this.mixins.printMessage(Kind.ERROR, "Mixin has multiple targets. " + annotation + " is not supported!", element); return false; } return true; @@ -241,7 +247,7 @@ class AnnotatedMixin { * Register an {@link org.spongepowered.asm.mixin.Overwrite} method */ public void registerOverwrite(ExecutableElement method) { - if (!this.validateSingleTarget("@Overwrite")) { + if (!this.validateSingleTarget("@Overwrite", method)) { return; } @@ -262,7 +268,7 @@ class AnnotatedMixin { // well, we tried } - this.mixins.printMessage(error, "No obfuscation mapping for @Overwrite method " + mcpName + " in " + this.mixin); + this.mixins.printMessage(error, "No obfuscation mapping for @Overwrite method", method); return; } @@ -273,7 +279,7 @@ class AnnotatedMixin { * Register a {@link org.spongepowered.asm.mixin.Shadow} field */ public void registerShadow(VariableElement field, AnnotationMirror shadow) { - if (!this.validateSingleTarget("@Shadow")) { + if (!this.validateSingleTarget("@Shadow", field)) { return; } @@ -281,7 +287,7 @@ class AnnotatedMixin { String obfField = this.mixins.getObfField(this.targetRef + "/" + name); if (obfField == null) { - this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow field " + name + " in " + this.mixin); + this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow field", field); return; } @@ -292,7 +298,7 @@ class AnnotatedMixin { * Register a {@link org.spongepowered.asm.mixin.Shadow} method */ public void registerShadow(ExecutableElement method, AnnotationMirror shadow) { - if (!this.validateSingleTarget("@Shadow")) { + if (!this.validateSingleTarget("@Shadow", method)) { return; } @@ -301,7 +307,7 @@ class AnnotatedMixin { MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + name, mcpSignature)); if (obfMethod == null) { - this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow method " + name + " in " + this.mixin); + this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow method", method); return; } @@ -312,7 +318,7 @@ class AnnotatedMixin { * Register a {@link org.spongepowered.asm.mixin.injection.Inject} method */ public void registerInjector(ExecutableElement method, AnnotationMirror inject) { - if (!this.validateSingleTarget("@Inject")) { + if (!this.validateSingleTarget("@Inject", method)) { return; } @@ -322,15 +328,16 @@ class AnnotatedMixin { return; } - String desc = AnnotatedMixin.findDescriptor(this.targetType, targetMember); + String desc = this.findDescriptor(this.targetType, targetMember); if (desc == null) { - this.mixins.printMessage(Kind.WARNING, "Unable to parse signature for @Inject target " + targetMember.name + " in " + this.mixin); + this.mixins.printMessage(Kind.WARNING, "Unable to determine signature for @Inject target method", method); return; } MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + targetMember.name, desc)); if (obfMethod == null) { - this.mixins.printMessage(Kind.ERROR, "No obfuscation mapping for @Inject target " + targetMember.name + " in " + this.mixin); + Kind error = AnnotatedMixin.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; + this.mixins.printMessage(error, "No obfuscation mapping for @Inject target " + targetMember.name, method); return; } @@ -343,10 +350,10 @@ class AnnotatedMixin { /** * Register a {@link org.spongepowered.asm.mixin.injection.At} annotation and process the references */ - public void registerInjectionPoint(AnnotationMirror at) { + public void registerInjectionPoint(AnnotationMirror at, Element element) { String type = MirrorUtils.getAnnotationValue(at, "value"); String target = MirrorUtils.getAnnotationValue(at, "target"); - this.remapReference(type + ".", target); + this.remapReference(type + ".", target, element); // Pattern for replacing references in args, not used yet // if ("SOMETYPE".equals(type)) { @@ -355,7 +362,7 @@ class AnnotatedMixin { // } } - private void remapReference(String key, String target) { + private void remapReference(String key, String target, Element element) { if (target == null) { return; } @@ -363,7 +370,7 @@ class AnnotatedMixin { MemberInfo targetMember = MemberInfo.parse(target); if (!targetMember.isFullyQualified()) { String missing = "missing " + (targetMember.owner == null ? (targetMember.desc == null ? "owner and signature" : "owner") : "signature"); - this.mixins.printMessage(Kind.ERROR, "@At(" + key + ") is not fully qualified, " + missing + " in \"" + target + "\" in " + this.mixin); + this.mixins.printMessage(Kind.ERROR, "@At(" + key + ") is not fully qualified, " + missing, element); return; } @@ -371,14 +378,14 @@ class AnnotatedMixin { if (targetMember.isField()) { String obfField = this.mixins.getObfField(targetMember.toSrg()); if (obfField == null) { - this.mixins.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") \"" + target + "\" in " + this.mixin); + this.mixins.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target, element); return; } remappedReference = MemberInfo.fromSrgField(obfField, targetMember.desc); } else { MethodData obfMethod = this.mixins.getObfMethod(targetMember.asMethodData()); if (obfMethod == null) { - this.mixins.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") \"" + target + "\" in " + this.mixin); + this.mixins.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target, element); return; } remappedReference = MemberInfo.fromSrgMethod(obfMethod); @@ -410,7 +417,7 @@ class AnnotatedMixin { this.methodMappings.add(mapping); } - static String findDescriptor(TypeElement targetType, MemberInfo memberInfo) { + private String findDescriptor(TypeElement targetType, MemberInfo memberInfo) { String desc = memberInfo.desc; if (desc == null) { for (Element child : targetType.getEnclosedElements()) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index bcf8deaa..4741aa1c 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -36,6 +36,8 @@ import java.util.Set; import javax.annotation.processing.Filer; 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.TypeElement; import javax.lang.model.element.VariableElement; @@ -56,11 +58,25 @@ import net.minecraftforge.srg2source.rangeapplier.SrgContainer; */ class AnnotatedMixins { + static enum CompilerEnvironment { + /** + * Default environment + */ + JAVAC, + + /** + * Eclipse + */ + JDT + } + /** * Singleton instances for each ProcessingEnvironment */ private static Map instances = new HashMap(); + private final CompilerEnvironment env; + /** * Local processing environment */ @@ -105,6 +121,9 @@ class AnnotatedMixins { * Private constructor, get instances using {@link #getMixinsForEnvironment} */ private AnnotatedMixins(ProcessingEnvironment processingEnv) { + this.env = this.detectEnvironment(processingEnv); + this.processingEnv = processingEnv; + String outSrgFileName = processingEnv.getOptions().get("outSrgFile"); this.outSrgFileName = (outSrgFileName != null) ? outSrgFileName : "mixins.srg"; @@ -113,13 +132,20 @@ class AnnotatedMixins { String reobfSrgFileName = processingEnv.getOptions().get("reobfSrgFile"); if (reobfSrgFileName == null) { - processingEnv.getMessager().printMessage(Kind.ERROR, "The reobfSrgFile argument was not supplied, processing cannot continue"); + this.printMessage(Kind.ERROR, "The reobfSrgFile argument was not supplied, processing cannot continue"); } - this.processingEnv = processingEnv; this.reobfSrgFile = new File(reobfSrgFileName); } + private CompilerEnvironment detectEnvironment(ProcessingEnvironment processingEnv) { + if (processingEnv.getClass().getName().contains("jdt")) { + return CompilerEnvironment.JDT; + } + + return CompilerEnvironment.JAVAC; + } + /** * Lazy initialisation for srgs, so that we only initialise the srgs if they're actually required. */ @@ -128,11 +154,11 @@ class AnnotatedMixins { this.initDone = true; try { - this.processingEnv.getMessager().printMessage(Kind.NOTE, "Loading SRGs from " + this.reobfSrgFile.getAbsolutePath()); + this.printMessage(Kind.NOTE, "Loading SRGs from " + this.reobfSrgFile.getAbsolutePath()); this.srgs = new SrgContainer().readSrg(this.reobfSrgFile); } catch (Exception ex) { ex.printStackTrace(); - this.processingEnv.getMessager().printMessage(Kind.ERROR, "The specified SRG file could not be read, processing cannot continue"); + this.printMessage(Kind.ERROR, "The specified SRG file could not be read, processing cannot continue"); this.srgs = null; } } @@ -182,13 +208,13 @@ class AnnotatedMixins { if (this.outSrgFileName.matches("^.*[\\\\/:].*$")) { File outSrgFile = new File(this.outSrgFileName); outSrgFile.getParentFile().mkdirs(); - this.processingEnv.getMessager().printMessage(Kind.NOTE, "Writing output SRGs to " + outSrgFile.getAbsolutePath()); + this.printMessage(Kind.NOTE, "Writing output SRGs to " + outSrgFile.getAbsolutePath()); return new PrintWriter(outSrgFile); } Filer filer = this.processingEnv.getFiler(); FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", this.outSrgFileName); - this.processingEnv.getMessager().printMessage(Kind.NOTE, "Writing output SRGs to " + new File(outSrg.toUri()).getAbsolutePath()); + this.printMessage(Kind.NOTE, "Writing output SRGs to " + new File(outSrg.toUri()).getAbsolutePath()); return new PrintWriter(outSrg.openWriter()); } @@ -265,8 +291,7 @@ class AnnotatedMixins { public void registerOverwrite(TypeElement mixinType, ExecutableElement method) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { - this.processingEnv.getMessager().printMessage(Kind.ERROR, - "Found @Overwrite annotation on a non-mixin method " + method + " in " + mixinType); + this.printMessage(Kind.ERROR, "Found @Overwrite annotation on a non-mixin method " + method + " in " + mixinType, method); return; } @@ -285,8 +310,7 @@ class AnnotatedMixins { public void registerShadow(TypeElement mixinType, VariableElement field, AnnotationMirror shadow) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { - this.processingEnv.getMessager().printMessage(Kind.ERROR, - "Found @Shadow annotation on a non-mixin field " + field + " in " + mixinType); + this.printMessage(Kind.ERROR, "Found @Shadow annotation on a non-mixin field " + field + " in " + mixinType, field); return; } @@ -305,8 +329,7 @@ class AnnotatedMixins { public void registerShadow(TypeElement mixinType, ExecutableElement method, AnnotationMirror shadow) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { - this.processingEnv.getMessager().printMessage(Kind.ERROR, - "Found @Shadow annotation on a non-mixin method " + method + " in " + mixinType); + this.printMessage(Kind.ERROR, "Found @Shadow annotation on a non-mixin method " + method + " in " + mixinType, method); return; } @@ -325,8 +348,7 @@ class AnnotatedMixins { public void registerInjector(TypeElement mixinType, ExecutableElement method, AnnotationMirror inject) { AnnotatedMixin mixinClass = this.getMixin(mixinType); if (mixinClass == null) { - this.processingEnv.getMessager().printMessage(Kind.ERROR, - "Found @Inject annotation on a non-mixin method " + method + " in " + mixinType); + this.printMessage(Kind.ERROR, "Found @Inject annotation on a non-mixin method " + method + " in " + mixinType, method); return; } @@ -336,13 +358,26 @@ class AnnotatedMixins { Object ats = MirrorUtils.getAnnotationValue(inject, "at"); if (ats instanceof List) { - @SuppressWarnings("unchecked") - List annotationValue = (List)ats; - for (AnnotationMirror at : annotationValue) { - mixinClass.registerInjectionPoint(at); + List annotationList = (List)ats; + for (Object at : annotationList) { + // Fix for JDT + if (at instanceof AnnotationValue) { + at = ((AnnotationValue)at).getValue(); + } + if (at instanceof AnnotationMirror) { + mixinClass.registerInjectionPoint((AnnotationMirror)at, method); + } else { + this.printMessage(Kind.WARNING, "No annotation mirror on " + at.getClass().getName()); + } } } else if (ats instanceof AnnotationMirror) { - mixinClass.registerInjectionPoint((AnnotationMirror)ats); + mixinClass.registerInjectionPoint((AnnotationMirror)ats, method); + } else if (ats instanceof AnnotationValue) { + // Fix for JDT + Object mirror = ((AnnotationValue)ats).getValue(); + if (mirror instanceof AnnotationMirror) { + mixinClass.registerInjectionPoint((AnnotationMirror)mirror, method); + } } } } @@ -362,7 +397,16 @@ class AnnotatedMixins { * Print a message to the AP messager */ public void printMessage(Diagnostic.Kind kind, CharSequence msg) { - this.processingEnv.getMessager().printMessage(kind, msg); + if (this.env == CompilerEnvironment.JAVAC) { + this.processingEnv.getMessager().printMessage(kind, msg); + } + } + + /** + * Print a message to the AP messager + */ + public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element element) { + this.processingEnv.getMessager().printMessage(kind, msg, element); } /**