diff --git a/gradle.properties b/gradle.properties
index 7ca9a069..770852b0 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -4,5 +4,5 @@ packaging=jar
description=Mixin
url=https://www.spongepowered.org
organization=SpongePowered
-buildVersion=0.7.11
+buildVersion=0.7.12
buildType=SNAPSHOT
diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
index c659d2a3..8d44117c 100644
--- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
+++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
- public static final String VERSION = "0.7.11";
+ public static final String VERSION = "0.7.12";
/**
* Log all the things
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 dfbfea97..a0a91b43 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java
@@ -130,6 +130,31 @@ public abstract class InjectionPoint {
public static final Selector DEFAULT = Selector.FIRST;
}
+
+ /**
+ * Target restriction level for different injection point types when used
+ * by restricted injectors (eg. {@link Inject}).
+ */
+ public enum RestrictTargetLevel {
+
+ /**
+ * Injection point is valid for instructions in methods only
+ */
+ METHODS_ONLY,
+
+ /**
+ * Injection point is valid for instructions in methods and in
+ * constructors but only after the delegate constructor call.
+ */
+ CONSTRUCTORS_AFTER_DELEGATE,
+
+ /**
+ * Injection point is valid for instructions in both methods and
+ * constructors, both before and after the delegate call
+ */
+ ALLOW_ALL
+
+ }
/**
* Behaviour for when the defined allowed value of {@link At#by} is exceeded
@@ -230,6 +255,18 @@ public abstract class InjectionPoint {
public boolean checkPriority(int targetPriority, int mixinPriority) {
return targetPriority < mixinPriority;
}
+
+ /**
+ * Returns the target restriction level for this injection point. This level
+ * defines whether an injection point is valid in its current state when
+ * being used by a restricted injector (currently {@link CallbackInjector}).
+ *
+ * @param context injection-specific context
+ * @return restriction level
+ */
+ public RestrictTargetLevel getTargetRestriction(IInjectionPointContext context) {
+ return RestrictTargetLevel.METHODS_ONLY;
+ }
/**
* Find injection points in the supplied insn list
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 f7ca1016..f8bb37b8 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
@@ -37,7 +37,6 @@ import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.code.Injector;
-import org.spongepowered.asm.mixin.injection.points.BeforeReturn;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
@@ -351,7 +350,7 @@ public class CallbackInjector extends Injector {
* @param localCapture Local variable capture behaviour
*/
public CallbackInjector(InjectionInfo info, boolean cancellable, LocalCapture localCapture, String identifier) {
- super(info);
+ super(info, "@Inject");
this.cancellable = cancellable;
this.localCapture = localCapture;
this.identifier = identifier;
@@ -365,19 +364,7 @@ public class CallbackInjector extends Injector {
@Override
protected void sanityCheck(Target target, List injectionPoints) {
super.sanityCheck(target, injectionPoints);
-
- if (target.isStatic != this.isStatic) {
- throw new InvalidInjectionException(this.info, "'static' modifier of callback method does not match target in " + this);
- }
-
- if (Constants.CTOR.equals(target.method.name)) {
- for (InjectionPoint injectionPoint : injectionPoints) {
- if (!injectionPoint.getClass().equals(BeforeReturn.class)) {
- throw new InvalidInjectionException(this.info, "Found injection point type " + injectionPoint.getClass().getSimpleName()
- + " targetting a ctor in " + this + ". Only RETURN allowed for a ctor target");
- }
- }
- }
+ this.checkTargetModifiers(target, true);
}
/* (non-Javadoc)
@@ -390,6 +377,13 @@ public class CallbackInjector extends Injector {
InjectionNode injectionNode = target.addInjectionNode(node);
for (InjectionPoint ip : nominators) {
+
+ try {
+ this.checkTargetForNode(target, injectionNode, ip.getTargetRestriction(this.info));
+ } catch (InvalidInjectionException ex) {
+ throw new InvalidInjectionException(this.info, String.format("%s selector %s", ip, ex.getMessage()));
+ }
+
String id = ip.getId();
if (Strings.isNullOrEmpty(id)) {
continue;
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java
index 54c5b04f..95537fb7 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/Injector.java
@@ -47,13 +47,16 @@ import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.lib.tree.TypeInsnNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
+import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
+import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.util.Bytecode;
+import org.spongepowered.asm.util.Bytecode.DelegateInitialiser;
/**
* Base class for bytecode injectors
@@ -107,6 +110,11 @@ public abstract class Injector {
*/
protected InjectionInfo info;
+ /**
+ * Annotation type, for use in informational errors
+ */
+ protected final String annotationType;
+
/**
* Class node
*/
@@ -137,20 +145,13 @@ public abstract class Injector {
*
* @param info Information about this injection
*/
- public Injector(InjectionInfo info) {
- this(info.getClassNode(), info.getMethod());
+ public Injector(InjectionInfo info, String annotationType) {
this.info = info;
- }
+ this.annotationType = annotationType;
- /**
- * Make a new CallbackInjector with the supplied args
- *
- * @param classNode Class containing callback and target methods
- * @param methodNode Callback method
- */
- private Injector(ClassNode classNode, MethodNode methodNode) {
- this.classNode = classNode;
- this.methodNode = methodNode;
+ this.classNode = info.getClassNode();
+ this.methodNode = info.getMethod();
+
this.methodArgs = Type.getArgumentTypes(this.methodNode.desc);
this.returnType = Type.getReturnType(this.methodNode.desc);
this.isStatic = Bytecode.methodIsStatic(this.methodNode);
@@ -257,6 +258,65 @@ public abstract class Injector {
}
}
+ /**
+ * Check that the static modifier of the target method matches the
+ * handler
+ *
+ * @param target Target to check
+ * @param exactMatch True if static must match, false to only check if an
+ * instance handler is targetting a static method
+ */
+ protected final void checkTargetModifiers(Target target, boolean exactMatch) {
+ if (exactMatch && target.isStatic != this.isStatic) {
+ throw new InvalidInjectionException(this.info, String.format("'static' modifier of handler method does not match target in %s", this));
+ } else if (!exactMatch && !this.isStatic && target.isStatic) {
+ throw new InvalidInjectionException(this.info,
+ String.format("non-static callback method %s targets a static method which is not supported", this));
+ }
+ }
+
+ /**
+ * The normal staticness check is not location-aware, in that it merely
+ * enforces static modifiers of handlers to match their targets. For
+ * injecting into constructors however (which are ostensibly instance
+ * methods) calls which are injected before the call to
+ * super() cannot access this and must therefore be declared
+ * as static.
+ *
+ * @param target Target method
+ * @param node Injection location
+ */
+ protected void checkTargetForNode(Target target, InjectionNode node, RestrictTargetLevel targetLevel) {
+ if (target.isCtor) {
+ if (targetLevel == RestrictTargetLevel.METHODS_ONLY) {
+ throw new InvalidInjectionException(this.info, String.format("Found %s targetting a constructor in injector %s",
+ this.annotationType, this));
+ }
+
+ DelegateInitialiser superCall = target.findDelegateInitNode();
+ if (!superCall.isPresent) {
+ throw new InjectionError(String.format("Delegate constructor lookup failed for %s target on %s", this.annotationType, this.info));
+ }
+
+ int superCallIndex = target.indexOf(superCall.insn);
+ int targetIndex = target.indexOf(node.getCurrentTarget());
+ if (targetIndex <= superCallIndex) {
+ if (targetLevel == RestrictTargetLevel.CONSTRUCTORS_AFTER_DELEGATE) {
+ throw new InvalidInjectionException(this.info, String.format("Found %s targetting a constructor before %s() in injector %s",
+ this.annotationType, superCall, this));
+ }
+
+ if (!this.isStatic) {
+ throw new InvalidInjectionException(this.info, String.format("%s handler before %s() invocation must be static in injector %s",
+ this.annotationType, superCall, this));
+ }
+ return;
+ }
+ }
+
+ this.checkTargetModifiers(target, true);
+ }
+
protected abstract void inject(Target target, InjectionNode node);
protected void postInject(Target target, InjectionNode node) {
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java
index e5995065..8e300726 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/InvokeInjector.java
@@ -43,16 +43,13 @@ import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionExceptio
* Base class for injectors which inject at method invokes
*/
public abstract class InvokeInjector extends Injector {
-
- protected final String annotationType;
/**
* @param info Information about this injection
* @param annotationType Annotation type, used for error messages
*/
public InvokeInjector(InjectionInfo info, String annotationType) {
- super(info);
- this.annotationType = annotationType;
+ super(info, annotationType);
}
/* (non-Javadoc)
@@ -75,48 +72,6 @@ public abstract class InvokeInjector extends Injector {
this.checkTargetModifiers(target, true);
}
- /**
- * Check that the static modifier of the target method matches the
- * handler
- *
- * @param target Target to check
- * @param exactMatch True if static must match, false to only check if an
- * instance handler is targetting a static method
- */
- protected final void checkTargetModifiers(Target target, boolean exactMatch) {
- if (exactMatch && target.isStatic != this.isStatic) {
- throw new InvalidInjectionException(this.info, "'static' modifier of handler method does not match target in " + this);
- } else if (!exactMatch && !this.isStatic && target.isStatic) {
- throw new InvalidInjectionException(this.info, "non-static callback method " + this + " targets a static method which is not supported");
- }
- }
-
- /**
- * The normal staticness check is not location-aware, in that it merely
- * enforces static modifiers of handlers to match their targets. For
- * injecting into constructors however (which are ostensibly instance
- * methods) calls which are injected before the call to
- * super() cannot access this and must therefore be declared
- * as static.
- *
- * @param target Target method
- * @param node Injection location
- */
- protected void checkTargetForNode(Target target, InjectionNode node) {
- if (target.isCtor) {
- MethodInsnNode superCall = target.findSuperInitNode();
- int superCallIndex = target.indexOf(superCall);
- int targetIndex = target.indexOf(node.getCurrentTarget());
- if (targetIndex <= superCallIndex) {
- if (!this.isStatic) {
- throw new InvalidInjectionException(this.info, "Pre-super " + this.annotationType + " invocation must be static in " + this);
- }
- return;
- }
- }
- this.checkTargetModifiers(target, true);
- }
-
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector
* #inject(org.spongepowered.asm.mixin.injection.callback.Target,
@@ -125,8 +80,8 @@ public abstract class InvokeInjector extends Injector {
@Override
protected void inject(Target target, InjectionNode node) {
if (!(node.getCurrentTarget() instanceof MethodInsnNode)) {
- throw new InvalidInjectionException(this.info, this.annotationType + " annotation on is targetting a non-method insn in " + target
- + " in " + this);
+ throw new InvalidInjectionException(this.info, String.format("%s annotation on is targetting a non-method insn in %s in %s",
+ this.annotationType, target, this));
}
this.injectAtInvoke(target, node);
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java
index a908983c..c404ba84 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgInjector.java
@@ -31,6 +31,7 @@ import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
+import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
@@ -96,7 +97,7 @@ public class ModifyArgInjector extends InvokeInjector {
@Override
protected void inject(Target target, InjectionNode node) {
- this.checkTargetForNode(target, node);
+ this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
super.inject(target, node);
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java
index 46b2023b..95e196d6 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/ModifyArgsInjector.java
@@ -30,6 +30,7 @@ import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.InsnNode;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.lib.tree.VarInsnNode;
+import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
import org.spongepowered.asm.mixin.injection.invoke.arg.ArgsClassGenerator;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
@@ -65,7 +66,7 @@ public class ModifyArgsInjector extends InvokeInjector {
@Override
protected void inject(Target target, InjectionNode node) {
- this.checkTargetForNode(target, node);
+ this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
super.inject(target, node);
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java
index d82b0dc9..66c7a4d4 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/invoke/RedirectInjector.java
@@ -37,6 +37,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.Redirect;
+import org.spongepowered.asm.mixin.injection.InjectionPoint.RestrictTargetLevel;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.points.BeforeFieldAccess;
import org.spongepowered.asm.mixin.injection.points.BeforeNew;
@@ -46,6 +47,7 @@ import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
+import org.spongepowered.asm.util.Constants;
import com.google.common.base.Joiner;
import com.google.common.collect.ObjectArrays;
@@ -191,6 +193,11 @@ public class RedirectInjector extends InvokeInjector {
int fuzz = BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT;
int opcode = 0;
+ if (insn instanceof MethodInsnNode && Constants.CTOR.equals(((MethodInsnNode)insn).name)) {
+ throw new InvalidInjectionException(this.info, String.format("Illegal %s of constructor specified on %s",
+ this.annotationType, this));
+ }
+
if (node != null ) {
Meta other = node.getDecoration(Meta.KEY);
@@ -249,13 +256,13 @@ public class RedirectInjector extends InvokeInjector {
}
if (node.getCurrentTarget() instanceof MethodInsnNode) {
- this.checkTargetForNode(target, node);
+ this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
this.injectAtInvoke(target, node);
return;
}
if (node.getCurrentTarget() instanceof FieldInsnNode) {
- this.checkTargetForNode(target, node);
+ this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
this.injectAtFieldAccess(target, node);
return;
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java
index 1843ba91..0f3e2529 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java
@@ -98,7 +98,7 @@ public class ModifyVariableInjector extends Injector {
* @param discriminator discriminator
*/
public ModifyVariableInjector(InjectionInfo info, LocalVariableDiscriminator discriminator) {
- super(info);
+ super(info, "@ModifyVariable");
this.discriminator = discriminator;
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeFinalReturn.java b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeFinalReturn.java
index 3d41aa4f..1f2f57cf 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeFinalReturn.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeFinalReturn.java
@@ -32,6 +32,7 @@ import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.InsnNode;
+import org.spongepowered.asm.mixin.injection.IInjectionPointContext;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InjectionPoint.AtCode;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
@@ -70,6 +71,11 @@ public class BeforeFinalReturn extends InjectionPoint {
public boolean checkPriority(int targetPriority, int ownerPriority) {
return true;
}
+
+ @Override
+ public RestrictTargetLevel getTargetRestriction(IInjectionPointContext context) {
+ return RestrictTargetLevel.ALLOW_ALL;
+ }
@Override
public boolean find(String desc, InsnList insns, Collection nodes) {
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeReturn.java b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeReturn.java
index 41c2ef44..4ecd1bf8 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeReturn.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeReturn.java
@@ -32,6 +32,7 @@ import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.InsnNode;
+import org.spongepowered.asm.mixin.injection.IInjectionPointContext;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InjectionPoint.AtCode;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
@@ -83,6 +84,11 @@ public class BeforeReturn extends InjectionPoint {
public boolean checkPriority(int targetPriority, int ownerPriority) {
return true;
}
+
+ @Override
+ public RestrictTargetLevel getTargetRestriction(IInjectionPointContext context) {
+ return RestrictTargetLevel.ALLOW_ALL;
+ }
@Override
public boolean find(String desc, InsnList insns, Collection nodes) {
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java
index 72b4f0ba..aa0d72cb 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Target.java
@@ -43,6 +43,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes.InjectionNode;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.util.Bytecode;
+import org.spongepowered.asm.util.Bytecode.DelegateInitialiser;
import org.spongepowered.asm.util.Constants;
/**
@@ -125,6 +126,11 @@ public class Target implements Comparable, Iterable {
* Labels for LVT ranges, generated as needed
*/
private LabelNode start, end;
+
+ /**
+ * Cached delegate initialiser call
+ */
+ private DelegateInitialiser delegateInitialiser;
/**
* Make a new Target for the supplied method
@@ -490,18 +496,25 @@ public class Target implements Comparable, Iterable {
}
/**
- * Find the call to super() in a constructor. This attempts to
- * locate the first call to <init> which isn't an inline call
- * to another object ctor being passed into the super invocation.
+ * Find the call to super() or this() in a constructor.
+ * This attempts to locate the first call to <init> which
+ * isn't an inline call to another object ctor being passed into the super
+ * invocation.
*
- * @return Call to super() or null if not found
+ * @return Call to super(), this() or
+ * DelegateInitialiser.NONE if not found
*/
- public MethodInsnNode findSuperInitNode() {
+ public DelegateInitialiser findDelegateInitNode() {
if (!this.isCtor) {
return null;
}
-
- return Bytecode.findSuperInit(this.method, ClassInfo.forName(this.classNode.name).getSuperName());
+
+ if (this.delegateInitialiser == null) {
+ String superName = ClassInfo.forName(this.classNode.name).getSuperName();
+ this.delegateInitialiser = Bytecode.findDelegateInit(this.method, superName, this.classNode.name);
+ }
+
+ return this.delegateInitialiser;
}
/**
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java
index aeeed529..4d949b95 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java
@@ -62,6 +62,7 @@ import org.spongepowered.asm.mixin.transformer.meta.MixinRenamed;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.util.Annotations;
import org.spongepowered.asm.util.Bytecode;
+import org.spongepowered.asm.util.Bytecode.DelegateInitialiser;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.ConstraintParser;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
@@ -677,11 +678,16 @@ class MixinApplicatorStandard {
return;
}
+ String superName = this.context.getClassInfo().getSuperName();
+
// Patch the initialiser into the target class ctors
for (MethodNode method : this.targetClass.methods) {
if (Constants.CTOR.equals(method.name)) {
- method.maxStack = Math.max(method.maxStack, ctor.maxStack);
- this.injectInitialiser(mixin, method, initialiser);
+ DelegateInitialiser superCall = Bytecode.findDelegateInit(method, superName, this.targetClass.name);
+ if (!superCall.isPresent || superCall.isSuper) {
+ method.maxStack = Math.max(method.maxStack, ctor.maxStack);
+ this.injectInitialiser(mixin, method, initialiser);
+ }
}
}
}
diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java
index 794162cb..9ccacc16 100644
--- a/src/main/java/org/spongepowered/asm/util/Bytecode.java
+++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java
@@ -99,6 +99,45 @@ public final class Bytecode {
}
}
+
+ /**
+ * Information bundle returned from {@link Bytecode#findDelegateInit}
+ */
+ public static class DelegateInitialiser {
+
+ /**
+ * No delegate initialiser found
+ */
+ public static final DelegateInitialiser NONE = new DelegateInitialiser(null, false);
+
+ /**
+ * Constructor invocation
+ */
+ public final MethodInsnNode insn;
+
+ /**
+ * True if the invocation is a super call, false if it's a call to
+ * another ctor in the same class
+ */
+ public final boolean isSuper;
+
+ /**
+ * True if the invocation is found, false if no delegate constructor
+ * was found (false for DelegateInitialiser.NONE)
+ */
+ public final boolean isPresent;
+
+ DelegateInitialiser(MethodInsnNode insn, boolean isSuper) {
+ this.insn = insn;
+ this.isSuper = isSuper;
+ this.isPresent = insn != null;
+ }
+
+ @Override
+ public String toString() {
+ return this.isSuper ? "super" : "this";
+ }
+ }
/**
* Integer constant opcodes
@@ -253,21 +292,24 @@ public final class Bytecode {
}
/**
- * Find the call to super() in a constructor. This attempts to
- * locate the first call to <init> which isn't an inline call
- * to another object ctor being passed into the super invocation.
+ * Find the call to super() or this() in a constructor.
+ * This attempts to locate the first call to <init> which
+ * isn't an inline call to another object ctor being passed into the super
+ * invocation.
*
- * @param method ctor to scan
+ * @param ctor ctor to scan
* @param superName name of superclass
- * @return Call to super() or null if not found
+ * @param ownerName name of owning class
+ * @return Call to super(), this() or
+ * DelegateInitialiser.NONE if not found
*/
- public static MethodInsnNode findSuperInit(MethodNode method, String superName) {
- if (!Constants.CTOR.equals(method.name)) {
- return null;
+ public static DelegateInitialiser findDelegateInit(MethodNode ctor, String superName, String ownerName) {
+ if (!Constants.CTOR.equals(ctor.name)) {
+ return DelegateInitialiser.NONE;
}
int news = 0;
- for (Iterator iter = method.instructions.iterator(); iter.hasNext();) {
+ for (Iterator iter = ctor.instructions.iterator(); iter.hasNext();) {
AbstractInsnNode insn = iter.next();
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) {
news++;
@@ -276,13 +318,16 @@ public final class Bytecode {
if (Constants.CTOR.equals(methodNode.name)) {
if (news > 0) {
news--;
- } else if (methodNode.owner.equals(superName)) {
- return methodNode;
+ } else {
+ boolean isSuper = methodNode.owner.equals(superName);
+ if (isSuper || methodNode.owner.equals(ownerName)) {
+ return new DelegateInitialiser(methodNode, isSuper);
+ }
}
}
}
}
- return null;
+ return DelegateInitialiser.NONE;
}
/**