From 64feb8cbfdfdb5e98bcc2326f52f4f10a57bce44 Mon Sep 17 00:00:00 2001
From: Mumfrey
This injection point searches for LDC and other constant opcodes + * matching its arguments and returns a list of injection points matching those + * instructions. When used with {@link At} it accepts the following parameters: + *
+ * + *Examples:
+ *+ *+ * // Find all integer constans with value 4 + * @At(value = "CONSTANT", args = "intValue=4")+ *
+ *+ * // Find the String literal "foo" + * @At(value = "CONSTANT", args = "stringValue=foo"+ *
+ * // Find all integer constans with value 0 and expand conditionals
+ * @At(
+ * value = "CONSTANT",
+ * args = {
+ * "intValue=0",
+ * "expandZeroConditions=LESS_THAN_ZERO,GREATER_THAN_ZERO"
+ * }
+ * )
+ *
+ *
+ *
+ * Note that like all standard injection points, this class matches the insn + * itself, putting the injection point immediately before the access in + * question. Use {@link org.spongepowered.asm.mixin.injection.At#shift shift} + * specifier to adjust the matched opcode as necessary.
+ */ +@AtCode("CONSTANT") +public class BeforeConstant extends InjectionPoint { + + private static final Logger logger = LogManager.getLogger("mixin"); + + /** + * Ordinal of the target insn + */ + private final int ordinal; + + private final boolean nullValue; + private final Integer intValue; + private final Float floatValue; + private final Long longValue; + private final Double doubleValue; + private final String stringValue; + private final Type typeValue; + + private final int[] expandOpcodes; + private final boolean expand; + + private final String matchByType; + + private final boolean log; + + public BeforeConstant(IMixinContext context, AnnotationNode node, String returnType) { + Boolean empty = Annotations.private void setElement(ElementType[] array, int index,
- * ElementType value)For fields of an array type, it is possible to redirect the call to the + * builtin pseudo-property length. To do so, specify the argument + * array=length and configure your handler signature to return int + * and consume the array as an argument:
+ * + *private int getLength(ElementType[] array)
+ *
+ *
+ * For multi-dimensional arrays, provide one int argument for each + * additional dimension of the array:
+ * + *// Multidimensional array
+ * private ElementType[][] array;
+ * // Handler signature:
+ * private int getLength(ElementType[][] array, int baseDim)
+ *
+ *
* The handler method signature must match the constructor being redirected
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 0c581934..9fb6e94f 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
@@ -96,6 +96,7 @@ public class RedirectInjector extends InvokeInjector {
private static final String KEY_NOMINATORS = "nominators";
private static final String KEY_WILD = "wildcard";
private static final String KEY_FUZZ = "fuzz";
+ private static final String KEY_OPCODE = "opcode";
/**
* Meta decoration object for redirector target nodes
@@ -169,6 +170,7 @@ public class RedirectInjector extends InvokeInjector {
InjectionNode node = target.injectionNodes.get(insn);
ConstructorRedirectData ctorData = null;
int fuzz = BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT;
+ int opcode = 0;
if (node != null ) {
Meta other = node.getDecoration(Meta.KEY);
@@ -189,7 +191,9 @@ public class RedirectInjector extends InvokeInjector {
if (ip instanceof BeforeNew && !((BeforeNew)ip).hasDescriptor()) {
ctorData = this.getCtorRedirect((BeforeNew)ip);
} else if (ip instanceof BeforeFieldAccess) {
- fuzz = ((BeforeFieldAccess)ip).getFuzzFactor();
+ BeforeFieldAccess bfa = (BeforeFieldAccess)ip;
+ fuzz = bfa.getFuzzFactor();
+ opcode = bfa.getArrayOpcode();
}
}
@@ -201,6 +205,7 @@ public class RedirectInjector extends InvokeInjector {
targetNode.decorate(ConstructorRedirectData.KEY, ctorData);
} else {
targetNode.decorate(RedirectInjector.KEY_FUZZ, Integer.valueOf(fuzz));
+ targetNode.decorate(RedirectInjector.KEY_OPCODE, Integer.valueOf(opcode));
}
myNodes.add(targetNode);
}
@@ -328,7 +333,8 @@ public class RedirectInjector extends InvokeInjector {
throw new InvalidInjectionException(this.info, "Dimensionality of handler method is greater than target array on " + this);
} else if (handlerDimensions == 0 && targetDimensions > 0) {
int fuzz = node. Matching array access: For array fields, it is possible to match field accesses followed by a
- * corresponding array element get or set operation. To enable
- * this behaviour specify the array named-argument with the desired
- * operation:
* @At(value = "FIELD", target="myIntArray:[I", args = "array=get")
@@ -86,6 +86,10 @@ import org.spongepowered.asm.util.Bytecode;
@AtCode("FIELD")
public class BeforeFieldAccess extends BeforeInvoke {
+ private static final String ARRAY_GET = "get";
+ private static final String ARRAY_SET = "set";
+ private static final String ARRAY_LENGTH = "length";
+
/**
* Default fuzz factor for searching for array access opcodes
*/
@@ -113,13 +117,26 @@ public class BeforeFieldAccess extends BeforeInvoke {
this.opcode = data.getOpcode(-1, Opcodes.GETFIELD, Opcodes.PUTFIELD, Opcodes.GETSTATIC, Opcodes.PUTSTATIC, -1);
String array = data.get("array", "");
- this.arrOpcode = "get".equalsIgnoreCase(array) ? Opcodes.IALOAD : "set".equalsIgnoreCase(array) ? Opcodes.IASTORE : 0;
+ this.arrOpcode = BeforeFieldAccess.ARRAY_GET.equalsIgnoreCase(array) ? Opcodes.IALOAD
+ : BeforeFieldAccess.ARRAY_SET.equalsIgnoreCase(array) ? Opcodes.IASTORE
+ : BeforeFieldAccess.ARRAY_LENGTH.equalsIgnoreCase(array) ? Opcodes.ARRAYLENGTH : 0;
this.fuzzFactor = Math.min(Math.max(data.get("fuzz", BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT), 1), 32);
}
public int getFuzzFactor() {
return this.fuzzFactor;
}
+
+ public int getArrayOpcode() {
+ return this.arrOpcode;
+ }
+
+ private int getArrayOpcode(String desc) {
+ if (this.arrOpcode != Opcodes.ARRAYLENGTH) {
+ return Type.getType(desc).getElementType().getOpcode(this.arrOpcode);
+ }
+ return this.arrOpcode;
+ }
@Override
protected boolean matchesInsn(AbstractInsnNode insn) {
@@ -142,7 +159,7 @@ public class BeforeFieldAccess extends BeforeInvoke {
protected boolean addInsn(InsnList insns, Collection nodes, AbstractInsnNode insn) {
if (this.arrOpcode > 0) {
FieldInsnNode fieldInsn = (FieldInsnNode)insn;
- int accOpcode = Type.getType(fieldInsn.desc).getElementType().getOpcode(this.arrOpcode);
+ int accOpcode = this.getArrayOpcode(fieldInsn.desc);
this.log("{} > > > > searching for array access opcode {} fuzz={}", this.className, Bytecode.getOpcodeName(accOpcode), this.fuzzFactor);
if (BeforeFieldAccess.findArrayNode(insns, fieldInsn, accOpcode, this.fuzzFactor) == null) {