diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/IInsnListEx.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/IInsnListEx.java
index 58426920..73979e59 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/code/IInsnListEx.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/IInsnListEx.java
@@ -48,7 +48,14 @@ public interface IInsnListEx {
/**
* The location for injected initialisers in a constructor
*/
- INITIALISER_INJECTION_POINT
+ INITIALISER_INJECTION_POINT,
+
+ /**
+ * The location after field initialisers but before the first
+ * constructor body instruction, requires line numbers to be present in
+ * the target class
+ */
+ CTOR_BODY
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListEx.java b/src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListEx.java
index 97802871..0a6a26a3 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListEx.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/code/InsnListEx.java
@@ -142,6 +142,14 @@ public class InsnListEx extends InsnListReadOnly implements IInsnListEx {
}
return null;
+ case CTOR_BODY:
+ if (this.target instanceof Constructor) {
+ AbstractInsnNode beforeBody = ((Constructor)this.target).findFirstBodyInsn();
+ if (this.contains(beforeBody)) {
+ return beforeBody;
+ }
+ }
+ return null;
default:
return null;
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/points/ConstructorHead.java b/src/main/java/org/spongepowered/asm/mixin/injection/points/ConstructorHead.java
index 21edce2b..804f7369 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/points/ConstructorHead.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/points/ConstructorHead.java
@@ -37,7 +37,6 @@ import org.spongepowered.asm.mixin.injection.code.IInsnListEx.SpecialNodeType;
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionPointException;
import org.spongepowered.asm.service.MixinService;
-import org.spongepowered.asm.util.asm.MarkerNode;
/**
*
Like {@link MethodHead HEAD}, this injection point can be used to specify
@@ -56,9 +55,15 @@ import org.spongepowered.asm.util.asm.MarkerNode;
*
* - enforce=POST_DELEGATE
* - Select the instruction immediately after the delegate constructor
- * - enforce=POST_INIT
- * - Select the instruction immediately after field initialisers, this
- * condition can fail if no initialisers are found.
+ * - enforce=POST_MIXIN
+ * - Select the instruction immediately after all mixin-initialised field
+ * initialisers, this is similar to POST_DELEGATE if no applied mixins have
+ * initialisers for target class fields, except that the injection point
+ * will be after any mixin-supplied initialisers.
+ * - enforce=PRE_BODY
+ * - Selects the first instruction in the target method body, as determined
+ * by the line numbers. If the target method does not have line numbers
+ * available, the result is equivalent to POST_DELEGATE.
*
*
* Example default behaviour:
@@ -81,7 +86,7 @@ public class ConstructorHead extends MethodHead {
static enum Enforce {
/**
- * Use default behaviour
+ * Use default behaviour (POST_INIT)
*/
DEFAULT,
@@ -93,7 +98,12 @@ public class ConstructorHead extends MethodHead {
/**
* Enforce selection of post-initialiser insn
*/
- POST_INIT;
+ POST_INIT,
+
+ /**
+ * Enforce selection of the first body insn
+ */
+ PRE_BODY;
}
@@ -149,30 +159,10 @@ public class ConstructorHead extends MethodHead {
return true;
}
- AbstractInsnNode postInit = xinsns.getSpecialNode(SpecialNodeType.INITIALISER_INJECTION_POINT);
+ SpecialNodeType type = this.enforce == Enforce.PRE_BODY ? SpecialNodeType.CTOR_BODY : SpecialNodeType.INITIALISER_INJECTION_POINT;
+ AbstractInsnNode postInit = xinsns.getSpecialNode(type);
- if (postInit instanceof MarkerNode) {
- AbstractInsnNode postPostInit = postInit.getNext();
- if (postDelegate == postInit || postDelegate == postPostInit) {
- // If the marker is immedately after the delegate call, then we haven't actually
- // found any initialiser insns. This is fine as long as we're not Enforce.POST_INIT
- // which we assume will only be set by the user when they want to be sure that the
- // initialisers are properly detected. Set this null to trigger the enforcement or
- // fallback behaviour below.
- postInit = null;
- } else {
- postInit = postPostInit;
- }
- }
-
- if (this.enforce == Enforce.POST_INIT || postInit != null) {
- if (postInit == null) {
- if (this.verbose) {
- this.logger.warn("@At(\"{}\") on {}{} targetting {} failed for enforce=POST_INIT because no initialiser was found",
- this.getAtCode(), this.method.name, this.method.desc, xinsns);
- }
- return false;
- }
+ if (postInit != null) {
nodes.add(postInit);
return true;
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Constructor.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Constructor.java
index 52fdab74..943edadf 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/Constructor.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/Constructor.java
@@ -24,18 +24,23 @@
*/
package org.spongepowered.asm.mixin.injection.struct;
+import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
+import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
+import org.objectweb.asm.tree.LabelNode;
+import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.mixin.transformer.struct.Initialiser;
+import org.spongepowered.asm.mixin.transformer.struct.InsnRange;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.asm.MarkerNode;
@@ -52,9 +57,18 @@ public class Constructor extends Target {
private DelegateInitialiser delegateInitialiser;
private MarkerNode initialiserInjectionPoint;
+ private MarkerNode bodyStart;
+
+ private final String targetName;
+ private final String targetSuperName;
+
+ private Set mixinInitialisedFields = new HashSet();
public Constructor(ClassInfo classInfo, ClassNode classNode, MethodNode method) {
super(classInfo, classNode, method);
+
+ this.targetName = this.classInfo.getName();
+ this.targetSuperName = this.classInfo.getSuperName();
}
/**
@@ -73,6 +87,40 @@ public class Constructor extends Target {
return this.delegateInitialiser;
}
+
+ /**
+ * Get whether this constructor should have mixin initialisers injected into
+ * it based on whether a delegate initialiser call is absent or is a call to
+ * super()
+ */
+ public boolean isInjectable() {
+ DelegateInitialiser delegateInit = this.findDelegateInitNode();
+ return !delegateInit.isPresent || delegateInit.isSuper;
+ }
+
+ /**
+ * Inspect an incoming initialiser to determine which fields are touched by
+ * the mixin. This is then used in {@link #findInitialiserInjectionPoint} to
+ * determine the instruction after which the mixin initialisers will be
+ * placed
+ *
+ * @param initialiser the initialiser to inspect
+ */
+ public void inspect(Initialiser initialiser) {
+ if (this.initialiserInjectionPoint != null) {
+ throw new IllegalStateException("Attempted to inspect an incoming initialiser after the injection point was already determined");
+ }
+
+ for (AbstractInsnNode initialiserInsn : initialiser.getInsns()) {
+ if (initialiserInsn.getOpcode() == Opcodes.PUTFIELD) {
+ FieldInsnNode fieldInsn = (FieldInsnNode)initialiserInsn;
+ if (!fieldInsn.owner.equals(this.targetName)) {
+ continue;
+ }
+ this.mixinInitialisedFields.add(Constructor.fieldKey((FieldInsnNode)initialiserInsn));
+ }
+ }
+ }
/**
* Find the injection point for injected initialiser insns in the target
@@ -90,26 +138,12 @@ public class Constructor extends Target {
return this.initialiserInjectionPoint;
}
- String targetName = this.classInfo.getName();
- String targetSuperName = this.classInfo.getSuperName();
-
- Set initialisedFields = new HashSet();
- for (AbstractInsnNode initialiserInsn : this.insns) {
- if (initialiserInsn.getOpcode() == Opcodes.PUTFIELD) {
- FieldInsnNode fieldInsn = (FieldInsnNode)initialiserInsn;
- if (!fieldInsn.owner.equals(targetName)) {
- continue;
- }
- initialisedFields.add(Constructor.fieldKey((FieldInsnNode)initialiserInsn));
- }
- }
-
AbstractInsnNode lastInsn = null;
for (Iterator iter = this.insns.iterator(); iter.hasNext();) {
AbstractInsnNode insn = iter.next();
if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.CTOR.equals(((MethodInsnNode)insn).name)) {
String owner = ((MethodInsnNode)insn).owner;
- if (owner.equals(targetName) || owner.equals(targetSuperName)) {
+ if (owner.equals(this.targetName) || owner.equals(targetSuperName)) {
lastInsn = insn;
if (mode == Initialiser.InjectionMode.SAFE) {
break;
@@ -117,7 +151,7 @@ public class Constructor extends Target {
}
} else if (insn.getOpcode() == Opcodes.PUTFIELD && mode == Initialiser.InjectionMode.DEFAULT) {
String key = Constructor.fieldKey((FieldInsnNode)insn);
- if (initialisedFields.contains(key)) {
+ if (this.mixinInitialisedFields.contains(key)) {
lastInsn = insn;
}
}
@@ -132,8 +166,70 @@ public class Constructor extends Target {
return this.initialiserInjectionPoint;
}
+ /**
+ * Find the injection point
+ */
+ public AbstractInsnNode findFirstBodyInsn() {
+ if (this.bodyStart == null) {
+ this.bodyStart = new MarkerNode(MarkerNode.BODY_START);
+ InsnRange range = Constructor.getRange(this.method);
+ if (range.isValid()) {
+ Deque body = range.apply(this.insns, true);
+ this.insertBefore(body.pop(), this.bodyStart);
+ } else if (range.marker > -1) {
+ this.insert(this.insns.get(range.marker), this.bodyStart);
+ } else {
+ this.bodyStart = null;
+ }
+ }
+ return this.bodyStart;
+ }
+
private static String fieldKey(FieldInsnNode fieldNode) {
return String.format("%s:%s", fieldNode.desc, fieldNode.name);
}
+ /**
+ * Identifies line numbers in the supplied ctor which correspond to the
+ * start and end of the method body.
+ *
+ * @param ctor constructor to scan
+ * @return range indicating the line numbers of the specified constructor
+ * and the position of the superclass ctor invocation
+ */
+ public static InsnRange getRange(MethodNode ctor) {
+ boolean lineNumberIsValid = false;
+ AbstractInsnNode endReturn = null;
+
+ int line = 0, start = 0, end = 0, delegateIndex = -1;
+ for (Iterator iter = ctor.instructions.iterator(); iter.hasNext();) {
+ AbstractInsnNode insn = iter.next();
+ if (insn instanceof LineNumberNode) {
+ line = ((LineNumberNode)insn).line;
+ lineNumberIsValid = true;
+ } else if (insn instanceof MethodInsnNode) {
+ if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.CTOR.equals(((MethodInsnNode)insn).name) && delegateIndex == -1) {
+ delegateIndex = ctor.instructions.indexOf(insn);
+ start = line;
+ }
+ } else if (insn.getOpcode() == Opcodes.PUTFIELD) {
+ lineNumberIsValid = false;
+ } else if (insn.getOpcode() == Opcodes.RETURN) {
+ if (lineNumberIsValid) {
+ end = line;
+ } else {
+ end = start;
+ endReturn = insn;
+ }
+ }
+ }
+
+ if (endReturn != null) {
+ LabelNode label = new LabelNode(new Label());
+ ctor.instructions.insertBefore(endReturn, label);
+ ctor.instructions.insertBefore(endReturn, new LineNumberNode(start, label));
+ }
+
+ return new InsnRange(start, end, delegateIndex);
+ }
}
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 2b9eb3e8..d5350c29 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java
@@ -61,7 +61,6 @@ import org.spongepowered.asm.service.IMixinAuditTrail;
import org.spongepowered.asm.service.MixinService;
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;
@@ -675,8 +674,7 @@ class MixinApplicatorStandard {
// Patch the initialiser into the target class ctors
for (Constructor ctor : this.context.getConstructors()) {
- DelegateInitialiser superCall = ctor.findDelegateInitNode();
- if (!superCall.isPresent || superCall.isSuper) {
+ if (ctor.isInjectable()) {
int extraStack = initialiser.getMaxStack() - ctor.getMaxStack();
if (extraStack > 0) {
ctor.extendStack().add(extraStack);
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
index f5879cba..83a1b622 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java
@@ -32,7 +32,6 @@ import org.spongepowered.asm.logging.Level;
import org.spongepowered.asm.logging.ILogger;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
-import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;
@@ -45,6 +44,7 @@ import org.spongepowered.asm.mixin.extensibility.IActivityContext.IActivity;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.gen.AccessorInfo;
import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.struct.Constructor;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.InjectorGroupInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
@@ -62,7 +62,7 @@ import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal;
import org.spongepowered.asm.mixin.transformer.ext.Extensions;
import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
import org.spongepowered.asm.mixin.transformer.struct.Initialiser;
-import org.spongepowered.asm.mixin.transformer.struct.Range;
+import org.spongepowered.asm.mixin.transformer.struct.InsnRange;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError;
import org.spongepowered.asm.obfuscation.RemapperChain;
@@ -178,6 +178,11 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
* upgraded if the version is below this value
*/
private int minRequiredClassVersion = CompatibilityLevel.JAVA_6.getClassVersion();
+
+ /**
+ * The mixin's initialiser
+ */
+ private Initialiser initialiser;
/**
* ctor
@@ -199,6 +204,8 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
InnerClassGenerator icg = context.getExtensions().getGenerator(InnerClassGenerator.class);
this.innerClasses = icg.getInnerClasses(this.mixin, this.getTargetClassRef());
+
+ this.initialiser = this.findInitialiser();
}
/**
@@ -1012,6 +1019,10 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
* null if the constructor range could not be parsed
*/
final Initialiser getInitialiser() {
+ return this.initialiser;
+ }
+
+ private Initialiser findInitialiser() {
// Try to find a suitable constructor, we need a constructor with line numbers in order to extract the initialiser
MethodNode ctor = this.getConstructor();
if (ctor == null) {
@@ -1019,12 +1030,18 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
}
// Find the range of line numbers which corresponds to the constructor body
- Range init = this.getConstructorRange(ctor);
+ InsnRange init = Constructor.getRange(ctor);
if (!init.isValid()) {
return null;
}
- return new Initialiser(this, ctor, init);
+ Initialiser initialiser = new Initialiser(this, ctor, init);
+ for (Constructor targetCtor : this.getTarget().getConstructors()) {
+ if (targetCtor.isInjectable()) {
+ targetCtor.inspect(initialiser);
+ }
+ }
+ return initialiser;
}
/**
@@ -1049,50 +1066,6 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
return ctor;
}
- /**
- * Identifies line numbers in the supplied ctor which correspond to the
- * start and end of the method body.
- *
- * @param ctor constructor to scan
- * @return range indicating the line numbers of the specified constructor
- * and the position of the superclass ctor invocation
- */
- private Range getConstructorRange(MethodNode ctor) {
- boolean lineNumberIsValid = false;
- AbstractInsnNode endReturn = null;
-
- int line = 0, start = 0, end = 0, superIndex = -1;
- for (Iterator iter = ctor.instructions.iterator(); iter.hasNext();) {
- AbstractInsnNode insn = iter.next();
- if (insn instanceof LineNumberNode) {
- line = ((LineNumberNode)insn).line;
- lineNumberIsValid = true;
- } else if (insn instanceof MethodInsnNode) {
- if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.CTOR.equals(((MethodInsnNode)insn).name) && superIndex == -1) {
- superIndex = ctor.instructions.indexOf(insn);
- start = line;
- }
- } else if (insn.getOpcode() == Opcodes.PUTFIELD) {
- lineNumberIsValid = false;
- } else if (insn.getOpcode() == Opcodes.RETURN) {
- if (lineNumberIsValid) {
- end = line;
- } else {
- end = start;
- endReturn = insn;
- }
- }
- }
-
- if (endReturn != null) {
- LabelNode label = new LabelNode(new Label());
- ctor.instructions.insertBefore(endReturn, label);
- ctor.instructions.insertBefore(endReturn, new LineNumberNode(start, label));
- }
-
- return new Range(start, end, superIndex);
- }
-
/**
* Get a target method handle from the target class
*
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/struct/Initialiser.java b/src/main/java/org/spongepowered/asm/mixin/transformer/struct/Initialiser.java
index ddd7650d..78ea3540 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/struct/Initialiser.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/struct/Initialiser.java
@@ -24,9 +24,7 @@
*/
package org.spongepowered.asm.mixin.transformer.struct;
-import java.util.ArrayDeque;
import java.util.Deque;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
@@ -34,7 +32,6 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
-import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.mixin.MixinEnvironment;
@@ -118,59 +115,28 @@ public class Initialiser {
private final MethodNode ctor;
/**
- * Extracted instructions
+ * Filtered instructions
*/
- private final Deque insns = new ArrayDeque();
+ private Deque insns;
- public Initialiser(MixinTargetContext mixin, MethodNode ctor, Range range) {
+ public Initialiser(MixinTargetContext mixin, MethodNode ctor, InsnRange range) {
this.mixin = mixin;
this.ctor = ctor;
this.initInstructions(range);
}
- private void initInstructions(Range range) {
+ private void initInstructions(InsnRange range) {
// Now we know where the constructor is, look for insns which lie OUTSIDE the method body
- int line = 0;
+ this.insns = range.apply(this.ctor.instructions, false);
- boolean gatherNodes = false;
- int trimAtOpcode = -1;
- LabelNode optionalInsn = null;
- for (Iterator iter = ctor.instructions.iterator(range.marker); iter.hasNext();) {
- AbstractInsnNode insn = iter.next();
- if (insn instanceof LineNumberNode) {
- line = ((LineNumberNode)insn).line;
- AbstractInsnNode next = ctor.instructions.get(ctor.instructions.indexOf(insn) + 1);
- if (line == range.end && next.getOpcode() != Opcodes.RETURN) {
- gatherNodes = true;
- trimAtOpcode = Opcodes.RETURN;
- } else {
- gatherNodes = range.excludes(line);
- trimAtOpcode = -1;
- }
- } else if (gatherNodes) {
- if (optionalInsn != null) {
- this.insns.add(optionalInsn);
- optionalInsn = null;
- }
-
- if (insn instanceof LabelNode) {
- optionalInsn = (LabelNode)insn;
- } else {
- int opcode = insn.getOpcode();
- if (opcode == trimAtOpcode) {
- trimAtOpcode = -1;
- continue;
- }
- for (int ivalidOp : Initialiser.OPCODE_BLACKLIST) {
- if (opcode == ivalidOp) {
- // At the moment I don't handle any transient locals because I haven't seen any in the wild, but let's avoid writing
- // code which will likely break things and fix it if a real test case ever appears
- throw new InvalidMixinException(this.mixin, "Cannot handle " + Bytecode.getOpcodeName(opcode) + " opcode (0x"
- + Integer.toHexString(opcode).toUpperCase(Locale.ROOT) + ") in class initialiser");
- }
- }
-
- this.insns.add(insn);
+ for (AbstractInsnNode insn : this.insns) {
+ int opcode = insn.getOpcode();
+ for (int ivalidOp : Initialiser.OPCODE_BLACKLIST) {
+ if (opcode == ivalidOp) {
+ // At the moment I don't handle any transient locals because I haven't seen any in the wild, but let's avoid writing
+ // code which will likely break things and fix it if a real test case ever appears
+ throw new InvalidMixinException(this.mixin, "Cannot handle " + Bytecode.getOpcodeName(opcode) + " opcode (0x"
+ + Integer.toHexString(opcode).toUpperCase(Locale.ROOT) + ") in class initialiser");
}
}
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/struct/Range.java b/src/main/java/org/spongepowered/asm/mixin/transformer/struct/InsnRange.java
similarity index 53%
rename from src/main/java/org/spongepowered/asm/mixin/transformer/struct/Range.java
rename to src/main/java/org/spongepowered/asm/mixin/transformer/struct/InsnRange.java
index eb00121d..86ed33f3 100644
--- a/src/main/java/org/spongepowered/asm/mixin/transformer/struct/Range.java
+++ b/src/main/java/org/spongepowered/asm/mixin/transformer/struct/InsnRange.java
@@ -24,23 +24,33 @@
*/
package org.spongepowered.asm.mixin.transformer.struct;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Iterator;
+
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.tree.AbstractInsnNode;
+import org.objectweb.asm.tree.InsnList;
+import org.objectweb.asm.tree.LabelNode;
+import org.objectweb.asm.tree.LineNumberNode;
+
/**
- * Struct for representing a range
+ * Struct for representing a range of instructions
*/
-public class Range {
+public class InsnRange {
/**
- * Start of the range
+ * Start of the range (line number)
*/
public final int start;
/**
- * End of the range
+ * End of the range (line number)
*/
public final int end;
/**
- * Range marker
+ * Range marker (index of insn)
*/
public final int marker;
@@ -51,7 +61,7 @@ public class Range {
* @param end End of the range
* @param marker Arbitrary marker value
*/
- public Range(int start, int end, int marker) {
+ public InsnRange(int start, int end, int marker) {
this.start = start;
this.end = end;
this.marker = marker;
@@ -94,4 +104,53 @@ public class Range {
return String.format("Range[%d-%d,%d,valid=%s)", this.start, this.end, this.marker, this.isValid());
}
+ /**
+ * Apply this range to the specified insn list
+ *
+ * @param insns insn list to filter
+ * @param inclusive whether to include or exclude instructions
+ * @return filtered list
+ */
+ public Deque apply(InsnList insns, boolean inclusive) {
+ Deque filtered = new ArrayDeque();
+ int line = 0;
+
+ boolean gatherNodes = false;
+ int trimAtOpcode = -1;
+ LabelNode optionalInsn = null;
+ for (Iterator iter = insns.iterator(this.marker); iter.hasNext();) {
+ AbstractInsnNode insn = iter.next();
+ if (insn instanceof LineNumberNode) {
+ line = ((LineNumberNode)insn).line;
+ AbstractInsnNode next = insns.get(insns.indexOf(insn) + 1);
+ if (line == this.end && next.getOpcode() != Opcodes.RETURN) {
+ gatherNodes = !inclusive;
+ trimAtOpcode = Opcodes.RETURN;
+ } else {
+ gatherNodes = inclusive ? this.contains(line) : this.excludes(line);
+ trimAtOpcode = -1;
+ }
+ } else if (gatherNodes) {
+ if (optionalInsn != null) {
+ filtered.add(optionalInsn);
+ optionalInsn = null;
+ }
+
+ if (insn instanceof LabelNode) {
+ optionalInsn = (LabelNode)insn;
+ } else {
+ int opcode = insn.getOpcode();
+ if (opcode == trimAtOpcode) {
+ trimAtOpcode = -1;
+ continue;
+ }
+
+ filtered.add(insn);
+ }
+ }
+ }
+
+ return filtered;
+ }
+
}
diff --git a/src/main/java/org/spongepowered/asm/util/asm/MarkerNode.java b/src/main/java/org/spongepowered/asm/util/asm/MarkerNode.java
index 228e3e98..64321baf 100644
--- a/src/main/java/org/spongepowered/asm/util/asm/MarkerNode.java
+++ b/src/main/java/org/spongepowered/asm/util/asm/MarkerNode.java
@@ -38,6 +38,11 @@ public class MarkerNode extends LabelNode {
*/
public static final int INITIALISER_TAIL = 1;
+ /**
+ * Marks the start of the body in a constructor
+ */
+ public static final int BODY_START = 2;
+
/**
* The type for this marker
*/