diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java
index cb2d9055..d9cb436e 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/BeforeLoadLocal.java
@@ -74,12 +74,35 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
*/
public class BeforeLoadLocal extends ContextualInjectionPoint {
+ /**
+ * Keeps track of state within {@link #find}
+ */
static class SearchState {
+ /**
+ * The target ordinal from the injection point
+ */
private final int targetOrdinal;
+
+ /**
+ * The current ordinal
+ */
private int ordinal = 0;
+
+ /**
+ * Flag to defer a {@link check} to the next opcode, to honour the after
+ * semantics of {@link AfterStoreLocal}.
+ */
private boolean pendingCheck = false;
+
+ /**
+ * True if one or more opcodes was matched
+ */
private boolean found = false;
+
+ /**
+ * Var node, captured for when deferring processing to the next opcode
+ */
private VarInsnNode varNode;
SearchState(int targetOrdinal) {
@@ -121,14 +144,31 @@ public class BeforeLoadLocal extends ContextualInjectionPoint {
public static final String CODE = "LOAD";
+ /**
+ * Return type of the handler, also the type of the local variable we're
+ * interested in
+ */
private final Type returnType;
+ /**
+ * Discriminator, parsed from parent annotation
+ */
private final LocalVariableDiscriminator discriminator;
+ /**
+ * Target opcode, inflected from return type
+ */
private final int opcode;
+ /**
+ * Target ordinal
+ */
private final int ordinal;
+ /**
+ * True if this injection point should capture the opcode after a matching
+ * opcode, used by {@link AfterStoreLocal}.
+ */
private boolean opcodeAfter;
public BeforeLoadLocal(MixinTargetContext mixin, Type returnType, LocalVariableDiscriminator discriminator, InjectionPointData data) {
diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java
index f54bb9f0..6d08083d 100644
--- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java
+++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java
@@ -45,14 +45,38 @@ import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.asm.util.PrettyPrinter.IPrettyPrintable;
import org.spongepowered.asm.util.SignaturePrinter;
+/**
+ * Encapsulates logic for identifying a local variable in a target method using
+ * 3 criteria: ordinal, index and name. This is used
+ * by the {@link ModifyVariableInjector} and its associated injection points.
+ */
public class LocalVariableDiscriminator {
+ /**
+ * Discriminator context information, wraps all relevant information about
+ * a target location for use when performing discrimination
+ */
public static class Context implements IPrettyPrintable {
+ /**
+ * Information about a local variable in the LVT, used during
+ * discrimination
+ */
public class Local {
+ /**
+ * Ordinal value of this local variable type
+ */
int ord = 0;
+
+ /**
+ * Local variable name
+ */
String name;
+
+ /**
+ * Local variable type
+ */
Type type;
public Local(String name, Type type) {
@@ -67,12 +91,36 @@ public class LocalVariableDiscriminator {
}
+ /**
+ * Target method for this context
+ */
final Target target;
+
+ /**
+ * The return type of the handler in question, also the type of the
+ * local variable that we care about
+ */
final Type returnType;
+
+ /**
+ * Injection point
+ */
final AbstractInsnNode node;
+
+ /**
+ * Base argument index, for static methods this is 0, for instance
+ * methods this is 1
+ */
final int baseArgIndex;
+
+ /**
+ * Enumerated locals in this context
+ */
final Local[] locals;
+ /**
+ * True if the handler (and target) are static
+ */
private final boolean isStatic;
public Context(MixinTargetContext mixin, Type returnType, boolean argsOnly, Target target, AbstractInsnNode node) {
@@ -181,22 +229,38 @@ public class LocalVariableDiscriminator {
this.names = Collections.unmodifiableSet(names);
}
+ /**
+ * True if this discriminator will examine only the target method args and
+ * won't consider the rest of the LVT at the target location
+ */
public boolean isArgsOnly() {
return this.argsOnly;
}
+ /**
+ * Get the local variable ordinal (nth variable of type)
+ */
public int getOrdinal() {
return this.ordinal;
}
+ /**
+ * Get the local variable absolute index
+ */
public int getIndex() {
return this.index;
}
+ /**
+ * Get valid names for consideration
+ */
public Set getNames() {
return this.names;
}
+ /**
+ * Returns true if names is not empty
+ */
public boolean hasNames() {
return !this.names.isEmpty();
}
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 7868baae..0a2d60bc 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
@@ -36,6 +36,7 @@ import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.lib.tree.VarInsnNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
import org.spongepowered.asm.mixin.injection.InvalidInjectionException;
+import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.code.Injector;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
@@ -45,12 +46,20 @@ import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.asm.util.SignaturePrinter;
/**
- * A bytecode injector which allows a single argument of a chosen method call to
- * be altered.
+ * A bytecode injector which allows a single local variable in the target method
+ * to be captured and altered. See also {@link LocalVariableDiscriminator} and
+ * {@link ModifyVariable}.
*/
public class ModifyVariableInjector extends Injector {
+ /**
+ * Target context information
+ */
static class Context extends LocalVariableDiscriminator.Context {
+
+ /**
+ * Instructions to inject
+ */
final InsnList insns = new InsnList();
public Context(ClassNode targetClass, Type returnType, boolean argsOnly, Target target, AbstractInsnNode node) {
@@ -59,6 +68,9 @@ public class ModifyVariableInjector extends Injector {
}
+ /**
+ * Specialised injection point which uses a target-aware search pattern
+ */
abstract static class ContextualInjectionPoint extends InjectionPoint {
protected final MixinTargetContext mixin;
@@ -173,6 +185,12 @@ public class ModifyVariableInjector extends Injector {
printer.print(System.err);
}
+ /**
+ * Perform the injection
+ *
+ * @param context target context
+ * @param local local variable to capture
+ */
private void inject(final Context context, final int local) {
if (!this.isStatic) {
context.insns.add(new VarInsnNode(Opcodes.ALOAD, 0));