From 41e53c49425c854ff8d44104a18246d9b9e672c0 Mon Sep 17 00:00:00 2001
From: Mumfrey
Date: Wed, 5 Jul 2017 09:15:03 +0100
Subject: [PATCH] Add support for Overwrite annotation on non-obfuscated
methods
---
.../tools/obfuscation/AnnotatedMixin.java | 4 +--
...AnnotatedMixinElementHandlerOverwrite.java | 21 +++++++++------
.../tools/obfuscation/AnnotatedMixins.java | 3 ++-
.../spongepowered/asm/mixin/Overwrite.java | 27 ++++++++++++++-----
.../org/spongepowered/asm/mixin/Unique.java | 2 +-
5 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java
index 4552e48a..479327dc 100644
--- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java
+++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java
@@ -340,9 +340,9 @@ class AnnotatedMixin {
}
}
- public void registerOverwrite(ExecutableElement method, AnnotationHandle overwrite) {
+ public void registerOverwrite(ExecutableElement method, AnnotationHandle overwrite, boolean shouldRemap) {
this.methods.remove(method);
- this.overwrites.registerOverwrite(new AnnotatedElementOverwrite(method, overwrite));
+ this.overwrites.registerOverwrite(new AnnotatedElementOverwrite(method, overwrite, shouldRemap));
}
public void registerShadow(VariableElement field, AnnotationHandle shadow, boolean shouldRemap) {
diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java
index 4a463fe4..abfdc7df 100644
--- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java
+++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java
@@ -45,8 +45,15 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler
*/
static class AnnotatedElementOverwrite extends AnnotatedElement {
- public AnnotatedElementOverwrite(ExecutableElement element, AnnotationHandle annotation) {
+ private final boolean shouldRemap;
+
+ public AnnotatedElementOverwrite(ExecutableElement element, AnnotationHandle annotation, boolean shouldRemap) {
super(element, annotation);
+ this.shouldRemap = shouldRemap;
+ }
+
+ public boolean shouldRemap() {
+ return this.shouldRemap;
}
}
@@ -64,13 +71,11 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler
this.validateTargetMethod(elem.getElement(), elem.getAnnotation(), name, "@Overwrite", true, false);
this.checkConstraints(elem.getElement(), elem.getAnnotation());
- if (!this.mixin.remap()) {
- return;
- }
-
- for (TypeHandle target : this.mixin.getTargets()) {
- if (!this.registerOverwriteForTarget(elem, target)) {
- return;
+ if (elem.shouldRemap()) {
+ for (TypeHandle target : this.mixin.getTargets()) {
+ if (!this.registerOverwriteForTarget(elem, target)) {
+ return;
+ }
}
}
diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java
index ae21b5a9..e5737a28 100644
--- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java
+++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java
@@ -387,7 +387,8 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
return;
}
- mixinClass.registerOverwrite(method, AnnotationHandle.of(method, Overwrite.class));
+ AnnotationHandle overwrite = AnnotationHandle.of(method, Overwrite.class);
+ mixinClass.registerOverwrite(method, overwrite, this.shouldRemap(mixinClass, overwrite));
}
/**
diff --git a/src/main/java/org/spongepowered/asm/mixin/Overwrite.java b/src/main/java/org/spongepowered/asm/mixin/Overwrite.java
index 152c98c4..4894c1fd 100644
--- a/src/main/java/org/spongepowered/asm/mixin/Overwrite.java
+++ b/src/main/java/org/spongepowered/asm/mixin/Overwrite.java
@@ -32,8 +32,8 @@ import java.lang.annotation.Target;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
/**
- * Annotation used to indicate a mixin class member which must overwrite an
- * obfuscated method in the target class.
+ * Annotation used to indicate a mixin class member which must overwrite a
+ * method in the target class.
*
* The default behaviour of mixin classes when merging mixin methods is to
* replace methods in the target class which already exist, and simply add any
@@ -46,11 +46,8 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
* traverse the obfuscation boundary, this association with the target method is
* lost because the method name will change. The {@link Overwrite} annotation is
* used to indicate to the annotation processor that this method is intended to
- * overwrite a member in the target class, and should therefore be added to the
- * obfuscation table.
- *
- * Adding this annotation to a non-obfuscated method will cause the
- * annotation processor to raise an error at compile time.
+ * overwrite a member in the target class, and should be added to the
+ * obfuscation table if {@link #remap} is true.
*/
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@@ -80,4 +77,20 @@ public @interface Overwrite {
*/
public String[] aliases() default { };
+ /**
+ * By default, the annotation processor will attempt to locate an
+ * obfuscation mapping for all {@link Overwrite} methods since it is
+ * anticipated that in general the target of a {@link Overwrite} annotation
+ * will be an obfuscated method in the target class. However since it is
+ * possible to also overwrite methods in non-obfuscated targets 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.
+ *
+ * @return True to instruct the annotation processor to search for
+ * obfuscation mappings for this method
+ */
+ public boolean remap() default true;
+
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/Unique.java b/src/main/java/org/spongepowered/asm/mixin/Unique.java
index c23503fe..e5ed1fa4 100644
--- a/src/main/java/org/spongepowered/asm/mixin/Unique.java
+++ b/src/main/java/org/spongepowered/asm/mixin/Unique.java
@@ -31,7 +31,7 @@ import java.lang.annotation.Target;
/**
* This annotation, when applied to a member method or field in a mixin,
- * indicates that the member should never overwrite a matching membver in
+ * indicates that the member should never overwrite a matching member in
* the target class. This indicates that the member differs from the normal
* "overlay-like" behaviour of mixins in general, and should only ever be
* added to the target. For public fields, the annotation has no