List of usage examples for org.objectweb.asm Opcodes PUTFIELD
int PUTFIELD
To view the source code for org.objectweb.asm Opcodes PUTFIELD.
Click Source Link
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * @param assignment// ww w. j a v a2 s. c o m * @param context * @param retainValue whether to leave on the stack the result of evaluating the expression. This is an optimization. * The normal case for an assignment expression is with retainValue = true. However, often assignments are used in * statement form in which case the resulting value is immediately popped off the stack. Hence there is no point to * push it onto the stack in the first place. * @return the type of the result on the operand stack (assuming retainValue is true). * @throws JavaGenerationException */ private static JavaTypeName encodeAssignmentExpr(JavaExpression.Assignment assignment, GenerationContext context, boolean retainValue) throws JavaGenerationException { MethodVisitor mv = context.getMethodVisitor(); JavaExpression.Nameable lhs = assignment.getLeftHandSide(); if (lhs instanceof LocalName) { // Determine what type of creature this name represents. // Note that because of the order of lookups, this takes scoping into account. LocalName localName = (LocalName) lhs; String varName = localName.getName(); if (context.getLocalVarIndex(varName) != -1) { lhs = new LocalVariable(varName, localName.getTypeName()); } else if (context.getMethodVarIndex(varName) != -1) { lhs = new MethodVariable(varName); } else { lhs = new JavaField.Instance(null, varName, localName.getTypeName()); } } // assign the value if (lhs instanceof JavaField.Instance) { JavaField.Instance javaInstanceField = (JavaField.Instance) lhs; //push the reference to the Java field itself. We can't just call encodeJavaField because we need the type of the instance object. JavaExpression instance = javaInstanceField.getInstance(); JavaTypeName instanceType; if (instance == null) { //use 'this' as the instance expression, so if the field is 'foo', then it is 'this.foo'. instanceType = encodeThis(context); } else { instanceType = encodeExpr(instance, context); } //push the rhs expression // the type of the assignment takes on the type of the value (not the assigned variable). JavaTypeName returnType = encodeExpr(assignment.getValue(), context); if (retainValue) { //duplicate the value so that a copy will be left over after assignment //field, value ---> //value, field, value mv.visitInsn(getDupX1OpCode(returnType)); } //do the assignment mv.visitFieldInsn(Opcodes.PUTFIELD, instanceType.getJVMInternalName(), javaInstanceField.getFieldName(), javaInstanceField.getFieldType().getJVMDescriptor()); return returnType; } else if (lhs instanceof JavaField.Static) { JavaField.Static javaStaticField = (JavaField.Static) lhs; //push the rhs expression // the type of the assignment takes on the type of the value (not the assigned variable). JavaTypeName returnType = encodeExpr(assignment.getValue(), context); if (retainValue) { //duplicate the value so that a copy will be left over after assignment //value ---> //value, value mv.visitInsn(getDupOpCode(returnType)); } //do the assignment mv.visitFieldInsn(Opcodes.PUTSTATIC, javaStaticField.getInvocationClass().getJVMInternalName(), javaStaticField.getFieldName(), javaStaticField.getFieldType().getJVMDescriptor()); return returnType; } else if (lhs instanceof LocalVariable) { LocalVariable localVariable = (LocalVariable) lhs; int localVarIndex = context.getLocalVarIndex(localVariable.getName()); //push the rhs expression JavaTypeName returnType = encodeExpr(assignment.getValue(), context); if (retainValue) { //duplicate the value so that a copy will be left over after assignment //value ---> //value, value mv.visitInsn(getDupOpCode(returnType)); } //encode the store instruction mv.visitVarInsn(getStoreOpCode(localVariable.getTypeName()), localVarIndex); return returnType; } else if (lhs instanceof MethodVariable) { MethodVariable methodVariable = (MethodVariable) lhs; String varName = methodVariable.getName(); int methodVarIndex = context.getMethodVarIndex(varName); JavaTypeName methodVarType = context.getMethodVarType(varName); //push the rhs expression JavaTypeName returnType = encodeExpr(assignment.getValue(), context); if (retainValue) { //duplicate the value so that a copy will be left over after assignment //value ---> //value, value mv.visitInsn(getDupOpCode(returnType)); } //encode the store instruction mv.visitVarInsn(getStoreOpCode(methodVarType), methodVarIndex); return returnType; } else if (lhs instanceof ArrayAccess) { ArrayAccess arrayAccess = (ArrayAccess) lhs; // Add the instructions to get the array ref. encodeExpr(arrayAccess.getArrayReference(), context); // Add the instructions to evaluate the array index. encodeExpr(arrayAccess.getArrayIndex(), context); // add the instructions to evaluate the expression to assign. JavaTypeName returnType = encodeExpr(assignment.getValue(), context); if (retainValue) { //duplicate the value so that a copy will be left over after assignment //arrayRef index value ---> //value arrayRef index value mv.visitInsn(getDupX2OpCode(returnType)); } //encode the store array element instruction mv.visitInsn(getArrayStoreOpCode(returnType)); return returnType; } throw new JavaGenerationException("Cannot assign to this type of expression: " + lhs); }
From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator1.java
License:Apache License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { // GETFIELD I,F,L,D + B,S if ((opcode == Opcodes.GETFIELD)) { if (desc.equals("I")) { if (this.shouldMutate("Incremented (a++) integer field " + name)) { // Assuming we have the reference to [this] on the stack mv.visitInsn(Opcodes.DUP); // dup [this] mv.visitFieldInsn(opcode, owner, name, desc); // stack = [this] [this.field] mv.visitInsn(Opcodes.DUP_X1); // stack = [this.field] [this] [this.field] mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); // stack = [this.field] [this] [this.field +1] mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; }// w ww . j ava2 s . c o m } if (desc.equals("F")) { if (this.shouldMutate("Incremented (a++) float field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FADD); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Incremented (a++) long field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2_X1); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LADD); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Incremented (a++) double field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2_X1); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DADD); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Incremented (a++) byte field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2B); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Incremented (a++) short field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2S); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } } // GETSTATIC I,F,L,D + B,S if (opcode == Opcodes.GETSTATIC) { if (desc.equals("I")) { if (this.shouldMutate("Incremented (a++) static integer field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("F")) { if (this.shouldMutate("Incremented (a++) static float field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FADD); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Incremented (a++) static long field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LADD); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Incremented (a++) static double field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DADD); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Incremented (a++) static byte field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2B); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Incremented (a++) static short field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2S); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } } mv.visitFieldInsn(opcode, owner, name, desc); }
From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator2.java
License:Apache License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { // GETFIELD I,F,L,D + B,S if ((opcode == Opcodes.GETFIELD)) { if (desc.equals("I")) { if (this.shouldMutate("Decremented (a--) integer field " + name)) { // stack = [this] mv.visitInsn(Opcodes.DUP); // stack = [this] [this] mv.visitFieldInsn(opcode, owner, name, desc); // stack = [this] [this.field] mv.visitInsn(Opcodes.DUP_X1); // stack = [this.field] [this] [this.field] mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; }/* w w w . j a va 2s .c o m*/ } if (desc.equals("F")) { if (this.shouldMutate("Decremented (a--) float field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FSUB); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Decremented (a--) long field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2_X1); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LSUB); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Decremented (a--) double field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2_X1); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DSUB); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Decremented (a--) byte field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2B); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Decremented (a--) short field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2S); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } } // GETSTATIC I,F,L,D + B,S if (opcode == Opcodes.GETSTATIC) { if (desc.equals("I")) { if (this.shouldMutate("Decremented (a--) static integer field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); // stack = [this.sfield] mv.visitInsn(Opcodes.DUP); // stack = [this.sfield] [this.sfield] mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); // stack = [this.sfield] [this.sfield -1] mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("F")) { if (this.shouldMutate("Decremented (a--) static float field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FSUB); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Decremented (a--) static long field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LSUB); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Decremented (a--) static double field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP2); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DSUB); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Decremented (a--) static byte field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2B); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Decremented (a--) static short field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2S); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } } mv.visitFieldInsn(opcode, owner, name, desc); }
From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator3.java
License:Apache License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { // GETFIELD I,F,L,D + B,S if ((opcode == Opcodes.GETFIELD)) { if (desc.equals("I")) { if (this.shouldMutate("Incremented (++a) integer field " + name)) { // stack = [this] mv.visitInsn(Opcodes.DUP); // stack = [this] [this] mv.visitFieldInsn(opcode, owner, name, desc); // stack = [this] [this.field] mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); // stack = [this] [this.field +1] mv.visitInsn(Opcodes.DUP_X1); // stack = [this.field +1] [this] [this.field +1] mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; }/*from ww w.jav a 2 s . c o m*/ } if (desc.equals("F")) { if (this.shouldMutate("Incremented (++a) float field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FADD); mv.visitInsn(Opcodes.DUP_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Incremented (++a) long field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LADD); mv.visitInsn(Opcodes.DUP2_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Incremented (++a) double field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DADD); mv.visitInsn(Opcodes.DUP2_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Incremented (++a) byte field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2B); mv.visitInsn(Opcodes.DUP_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Incremented (++a) short field " + name)) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2S); mv.visitInsn(Opcodes.DUP_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } } // GETSTATIC I,F,L,D + B,S if (opcode == Opcodes.GETSTATIC) { if (desc.equals("I")) { if (this.shouldMutate("Incremented (++a) static integer field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("F")) { if (this.shouldMutate("Incremented (++a) static float field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FADD); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Incremented (++a) static long field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LADD); mv.visitInsn(Opcodes.DUP2); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Incremented (++a) static double field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DADD); mv.visitInsn(Opcodes.DUP2); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Incremented (++a) static byte field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2B); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Incremented (++a) static short field " + name)) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitInsn(Opcodes.I2S); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } } mv.visitFieldInsn(opcode, owner, name, desc); }
From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator4.java
License:Apache License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { // GETFIELD I,F,L,D + B,S if ((opcode == Opcodes.GETFIELD)) { if (desc.equals("I")) { if (this.shouldMutate("Decremented (--a) integer field")) { // stack = [this] mv.visitInsn(Opcodes.DUP); // stack = [this] [this] mv.visitFieldInsn(opcode, owner, name, desc); // stack = [this] [this.field] mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); // stack = [this] [this.field -1] mv.visitInsn(Opcodes.DUP_X1); // stack = [this.field -1] [this] [this.field -1] mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; }// w ww . j a va 2 s. c om } if (desc.equals("F")) { if (this.shouldMutate("Decremented (--a) float field")) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FSUB); mv.visitInsn(Opcodes.DUP_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Decremented (--a) long field")) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LSUB); mv.visitInsn(Opcodes.DUP2_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Decremented (--a) double field")) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DSUB); mv.visitInsn(Opcodes.DUP2_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Decremented (--a) double field")) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2B); mv.visitInsn(Opcodes.DUP_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Decremented (--a) short field")) { mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2S); mv.visitInsn(Opcodes.DUP_X1); mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc); return; } } } // GETSTATIC I,F,L,D + B,S if (opcode == Opcodes.GETSTATIC) { if (desc.equals("I")) { if (this.shouldMutate("Decremented (--a) static integer field")) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("F")) { if (this.shouldMutate("Decremented (--a) static float field")) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.FCONST_1); mv.visitInsn(Opcodes.FSUB); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("J")) { if (this.shouldMutate("Decremented (--a) static long field")) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.LCONST_1); mv.visitInsn(Opcodes.LSUB); mv.visitInsn(Opcodes.DUP2); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("D")) { if (this.shouldMutate("Decremented (--a) static double field")) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.DCONST_1); mv.visitInsn(Opcodes.DSUB); mv.visitInsn(Opcodes.DUP2); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("B")) { if (this.shouldMutate("Decremented (--a) static byte field")) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2B); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } if (desc.equals("S")) { if (this.shouldMutate("Decremented (--a) static short field")) { mv.visitFieldInsn(opcode, owner, name, desc); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); mv.visitInsn(Opcodes.I2S); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc); return; } } } mv.visitFieldInsn(opcode, owner, name, desc); }
From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java
License:Open Source License
@Test public void test_putfield() throws Exception { SymbolicValue objectRef = new SymbolicValue(); SymbolicValue value = new SymbolicValue(); ProgramState programState = execute(new Instruction(Opcodes.PUTFIELD), ProgramState.EMPTY_STATE.stackValue(objectRef).stackValue(value)); assertThat(programState.peekValue()).isNull(); assertThatThrownBy(/*from w w w .jav a 2 s . c o m*/ () -> execute(new Instruction(Opcodes.PUTFIELD), ProgramState.EMPTY_STATE.stackValue(value))) .hasMessage("PUTFIELD needs 2 values on stack"); }
From source file:org.spongepowered.asm.mixin.injection.points.BeforeFieldAccess.java
License:MIT License
public BeforeFieldAccess(InjectionPointData data) { super(data);/*from w w w. ja v a 2 s .co m*/ this.opcode = data.getOpcode(-1, Opcodes.GETFIELD, Opcodes.PUTFIELD, Opcodes.GETSTATIC, Opcodes.PUTSTATIC, -1); }
From source file:org.spongepowered.asm.mixin.transformer.MixinTransformer.java
License:MIT License
/** * Get insns corresponding to the instance initialiser (hopefully) from the * supplied constructor./*from w w w .j a v a 2 s . c o m*/ * * TODO Potentially rewrite this to be less horrible. * * @param mixin * @param ctor * @return initialiser bytecode extracted from the supplied constructor, or * null if the constructor range could not be parsed */ private InsnList getInitialiser(MixinTargetContext mixin, MethodNode ctor) { // Find the range of line numbers which corresponds to the constructor body Range init = this.getConstructorRange(ctor); if (!init.isValid()) { return null; } // Now we know where the constructor is, look for insns which lie OUTSIDE the method body int line = 0; InsnList initialiser = new InsnList(); boolean gatherNodes = false; int trimAtOpcode = -1; LabelNode optionalInsn = null; for (Iterator<AbstractInsnNode> iter = ctor.instructions.iterator(init.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 == init.end && next.getOpcode() != Opcodes.RETURN) { gatherNodes = true; trimAtOpcode = Opcodes.RETURN; } else { gatherNodes = init.excludes(line); trimAtOpcode = -1; } } else if (gatherNodes) { if (optionalInsn != null) { initialiser.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 : MixinTransformer.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(mixin, "Cannot handle " + ASMHelper.getOpcodeName(opcode) + " opcode (0x" + Integer.toHexString(opcode).toUpperCase() + ") in class initialiser"); } } initialiser.add(insn); } } } // Check that the last insn is a PUTFIELD, if it's not then AbstractInsnNode last = initialiser.getLast(); if (last != null) { if (last.getOpcode() != Opcodes.PUTFIELD) { throw new InvalidMixinException(mixin, "Could not parse initialiser, expected 0xB5, found 0x" + Integer.toHexString(last.getOpcode())); } } return initialiser; }
From source file:org.spongepowered.common.mixin.core.entity.item.MixinEntityItem.java
License:MIT License
@Inject(method = "onUpdate()V", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/item/EntityItem;delayBeforeCanPickup:I", opcode = Opcodes.PUTFIELD, shift = At.Shift.AFTER)) private void onOnUpdate(CallbackInfo ci) { if (this.delayBeforeCanPickup == MAGIC_INFINITE_PICKUP_DELAY && !this.infinitePickupDelay && this.pluginPickupSet) { this.delayBeforeCanPickup--; }/*from w ww. j a v a 2 s. c o m*/ }
From source file:org.spongepowered.common.mixin.core.entity.item.MixinEntityItem.java
License:MIT License
@Inject(method = "onUpdate()V", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/item/EntityItem;age:I", opcode = Opcodes.PUTFIELD, shift = At.Shift.AFTER)) private void onOnUpdateAge(CallbackInfo ci) { if (this.delayBeforeCanPickup == MAGIC_INFINITE_DESPAWN_TIME && !this.infiniteDespawnDelay && this.pluginDespawnSet) { this.delayBeforeCanPickup--; }// w w w .jav a 2s . c om }