From fb64777a419df68d3bf8ff7c53903ee4c5bab76e Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 5 Jan 2015 16:11:17 +0000 Subject: [PATCH] Added "remap" arg to let reobf processing be skipped per-member basis --- .../tools/obfuscation/AnnotatedMixin.java | 17 +++++++++- .../tools/obfuscation/AnnotatedMixins.java | 34 +++++++++++++++---- .../org/spongepowered/asm/mixin/Mixin.java | 11 ++++++ .../org/spongepowered/asm/mixin/Shadow.java | 8 +++++ .../asm/mixin/injection/Inject.java | 10 ++++++ .../asm/mixin/injection/InjectionPoint.java | 3 +- 6 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 5e5d7539..8a151cdf 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -141,6 +141,11 @@ class AnnotatedMixin { */ private final String classRef; + /** + * True if we will actually process remappings for this mixin + */ + private final boolean remap; + /** * Stored (ordered) field mappings */ @@ -152,13 +157,16 @@ class AnnotatedMixin { private final Set methodMappings = new LinkedHashSet(); public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type) { + AnnotationMirror annotation = MirrorUtils.getAnnotation(type, Mixin.class); + this.mixins = mixins; this.mixin = type; this.classRef = type.getQualifiedName().toString().replace('.', '/'); + this.remap = AnnotatedMixins.getRemapValue(annotation); String targetRef = null; TypeElement targetType = null; - List targetList = MirrorUtils.>getAnnotationValue(type, Mixin.class); + List targetList = MirrorUtils.>getAnnotationValue(annotation); for (TypeMirror target : MirrorUtils.unfold(targetList)) { Element element = ((DeclaredType)target).asElement(); String targetName = ((TypeElement)element).getQualifiedName().toString(); @@ -187,6 +195,13 @@ class AnnotatedMixin { return this.targets; } + /** + * Get whether to remap annotations in this mixin + */ + public boolean remap() { + return this.remap; + } + /** * Get stored field mappings */ diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 3de82d85..e7acbeda 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -245,7 +245,10 @@ class AnnotatedMixins { "Found @Shadow annotation on a non-mixin field " + field + " in " + mixinType); return; } - mixinClass.registerShadow(field, shadow); + + if (this.shouldRemap(mixinClass, shadow)) { + mixinClass.registerShadow(field, shadow); + } } /** @@ -262,7 +265,10 @@ class AnnotatedMixins { "Found @Shadow annotation on a non-mixin method " + method + " in " + mixinType); return; } - mixinClass.registerShadow(method, shadow); + + if (this.shouldRemap(mixinClass, shadow)) { + mixinClass.registerShadow(method, shadow); + } } /** @@ -279,14 +285,28 @@ class AnnotatedMixins { "Found @Inject annotation on a non-mixin method " + method + " in " + mixinType); return; } - mixinClass.registerInjector(method, inject); - - List annotationValue = MirrorUtils.>getAnnotationValue(inject, "at"); - for (AnnotationMirror at : annotationValue) { - mixinClass.registerInjectionPoint(at); + + if (this.shouldRemap(mixinClass, inject)) { + mixinClass.registerInjector(method, inject); + + List annotationValue = MirrorUtils.>getAnnotationValue(inject, "at"); + for (AnnotationMirror at : annotationValue) { + mixinClass.registerInjectionPoint(at); + } } } + private boolean shouldRemap(AnnotatedMixin mixinClass, AnnotationMirror 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(); + } + /** * Print a message to the AP messager */ diff --git a/src/main/java/org/spongepowered/asm/mixin/Mixin.java b/src/main/java/org/spongepowered/asm/mixin/Mixin.java index 2e67724a..e50bda70 100644 --- a/src/main/java/org/spongepowered/asm/mixin/Mixin.java +++ b/src/main/java/org/spongepowered/asm/mixin/Mixin.java @@ -45,4 +45,15 @@ public @interface Mixin { * Priority for the mixin, relative to other mixins targetting the same classes */ public int priority() default 1000; + + /** + * By default, the annotation processor will attempt to locate an obfuscation mapping for all {@link Shadow} and + * {@link org.spongepowered.asm.mixin.injection.Inject} annotated members since it is anticipated that in general the target of a {@link Mixin} + * will be an obfuscated class and all annotated members will need to be added to the obfuscation table. However since it is possible to also + * apply mixins to non-obfuscated targets it may be desirable to suppress the compiler warnings which would otherwise be generated. This can be + * done on an individual member basis by setting remap to false on the individual annotations, or disabled for the entire + * mixin by setting the value here to false. Doing so will cause the annotation processor to skip all annotations in this mixin when + * building the obfuscation table. + */ + public boolean remap() default true; } diff --git a/src/main/java/org/spongepowered/asm/mixin/Shadow.java b/src/main/java/org/spongepowered/asm/mixin/Shadow.java index c411e205..7694fa42 100644 --- a/src/main/java/org/spongepowered/asm/mixin/Shadow.java +++ b/src/main/java/org/spongepowered/asm/mixin/Shadow.java @@ -65,4 +65,12 @@ public @interface Shadow { */ public String prefix() default "shadow$"; + /** + * By default, the annotation processor will attempt to locate an obfuscation mapping for all {@link Shadow} methods since it is anticipated that + * in general the target of a {@link Shadow} annotation will be an obfuscated field or method in the target class. However since it is possible + * to also apply mixins to non-obfuscated targets (or non-obfuscated methods in obfuscated targets, such as methods added by Forge) it may be + * desirable to suppress the compiler warning which would otherwise be generated. Setting this value to false will cause the annotation + * processor to skip this annotation when attempting to build the obfuscation table for the mixin. + */ + public boolean remap() default true; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java index bea1643b..f4925168 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java @@ -58,4 +58,14 @@ public @interface Inject { * Set to true to allow local variables to be captured from the target method as well as method parameters. */ // public boolean captureLocals() default false; + + + /** + * By default, the annotation processor will attempt to locate an obfuscation mapping for all {@link Inject} methods since it is anticipated that + * in general the target of a {@link Inject} annotation will be an obfuscated method in the target class. However since it is possible to also + * apply mixins to non-obfuscated targets (or non-obfuscated methods in obfuscated targets, such as methods added by Forge) it may be necessary + * to suppress the compiler error which would otherwise be generated. Setting this value to false will cause the annotation processor + * to skip this annotation when attempting to build the obfuscation table for the mixin. + */ + public boolean remap() default true; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 43de439c..1dc3ca49 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -118,7 +118,8 @@ public abstract class InjectionPoint { public boolean find(String desc, InsnList insns, Collection nodes) { boolean found = false; - @SuppressWarnings("unchecked") ArrayList[] allNodes = new ArrayList[this.components.length]; + @SuppressWarnings({ "unchecked", "rawtypes" }) + ArrayList[] allNodes = new ArrayList[this.components.length]; for (int i = 0; i < this.components.length; i++) { allNodes[i] = new ArrayList();