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 5f065f74..fcbfdfeb 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java @@ -42,6 +42,15 @@ import org.spongepowered.asm.mixin.injection.callback.LocalCapture; @Retention(RetentionPolicy.RUNTIME) public @interface Inject { + /** + * The identifier for this injector, can be retrieved via the + * {@link CallbackInfo#getId} accessor. If not specified, the ID defaults to + * the target method name. + * + * @return the injector id to use + */ + public String id() default ""; + /** * String representation of a * {@link org.spongepowered.asm.mixin.injection.struct.MemberInfo @@ -112,7 +121,6 @@ public @interface Inject { */ public LocalCapture locals() default LocalCapture.NO_CAPTURE; - /** * By default, the annotation processor will attempt to locate an * obfuscation mapping for all {@link Inject} methods since it is diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInfo.java index 5c954ce7..e3a6c7b3 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInfo.java @@ -25,6 +25,7 @@ package org.spongepowered.asm.mixin.injection.callback; import org.spongepowered.asm.lib.Type; +import org.spongepowered.asm.mixin.injection.Inject; /** * CallbackInfo instances are passed to callbacks in order to provide @@ -65,11 +66,13 @@ public class CallbackInfo implements Cancellable { } /** - * Get the method name where this callback originated + * Get the ID of the injector which defined this callback. This defaults to + * the method name but can be overridden by specifying the {@link Inject#id} + * parameter on the injector * - * @return the name of the calling method + * @return the injector ID */ - public String getName() { + public String getId() { return this.name; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 7a6549ec..ff0f77f1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -46,6 +46,8 @@ import org.spongepowered.asm.util.Locals; import org.spongepowered.asm.util.PrettyPrinter; import org.spongepowered.asm.util.SignaturePrinter; +import com.google.common.base.Strings; + /** * This class is responsible for generating the bytecode for injected callbacks */ @@ -286,6 +288,11 @@ public class CallbackInjector extends Injector { */ private final LocalCapture localCapture; + /** + * ID to return from callbackinfo + */ + private final String identifier; + /** * Make a new CallbackInjector with the supplied args * @@ -294,10 +301,11 @@ public class CallbackInjector extends Injector { * be cancellable * @param localCapture Local variable capture behaviour */ - public CallbackInjector(InjectionInfo info, boolean cancellable, LocalCapture localCapture) { + public CallbackInjector(InjectionInfo info, boolean cancellable, LocalCapture localCapture, String identifier) { super(info); this.cancellable = cancellable; this.localCapture = localCapture; + this.identifier = identifier; } /* (non-Javadoc) @@ -543,7 +551,7 @@ public class CallbackInjector extends Injector { * invoke */ protected void invokeCallbackInfoCtor(final Callback callback, boolean store) { - callback.add(new LdcInsnNode(callback.target.method.name), true, !store); + callback.add(new LdcInsnNode(this.getIdentifier(callback)), true, !store); callback.add(new InsnNode(this.cancellable ? Opcodes.ICONST_1 : Opcodes.ICONST_0), true, !store); if (callback.isAtReturn) { @@ -582,6 +590,18 @@ public class CallbackInjector extends Injector { this.invokeHandler(callback, callbackMethod); } + /** + * Get the identifier to use for the specified callback. If an id was + * specified by the end user on the annotation then use the value specified, + * otherwise defaults to the target method name. + * + * @param callback Callback being injected + * @return Identifier to use + */ + private String getIdentifier(Callback callback) { + return Strings.isNullOrEmpty(this.identifier) ? callback.target.method.name : this.identifier; + } + /** * if (e.isCancelled()) return e.getReturnValue(); * diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java index 4e671147..da9871e2 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/CallbackInjectionInfo.java @@ -46,8 +46,9 @@ public class CallbackInjectionInfo extends InjectionInfo { protected Injector initInjector(AnnotationNode injectAnnotation) { boolean cancellable = ASMHelper.getAnnotationValue(injectAnnotation, "cancellable", false); LocalCapture locals = ASMHelper.getAnnotationValue(injectAnnotation, "locals", LocalCapture.class, LocalCapture.NO_CAPTURE); + String identifier = ASMHelper.getAnnotationValue(injectAnnotation, "id", ""); - return new CallbackInjector(this, cancellable, locals); + return new CallbackInjector(this, cancellable, locals, identifier); } }