Add ID for injectors

This commit is contained in:
Mumfrey committed 2016-02-09 17:00:55 +00:00
1 parent cce1039dc7
commit 71b44f93f3
4 files changed
+39 -7

No files matched your search

@@ -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
@@ -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;
}
@@ -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();
*
@@ -46,8 +46,9 @@ public class CallbackInjectionInfo extends InjectionInfo {
protected Injector initInjector(AnnotationNode injectAnnotation) {
boolean cancellable = ASMHelper.<Boolean>getAnnotationValue(injectAnnotation, "cancellable", false);
LocalCapture locals = ASMHelper.<LocalCapture>getAnnotationValue(injectAnnotation, "locals", LocalCapture.class, LocalCapture.NO_CAPTURE);
String identifier = ASMHelper.<String>getAnnotationValue(injectAnnotation, "id", "");
return new CallbackInjector(this, cancellable, locals);
return new CallbackInjector(this, cancellable, locals, identifier);
}
}