Search in sources :

Example 46 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class Instrumenter method instrumentGETXXXFieldAccesses.

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName, final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;
    AbstractInsnNode ins = null;
    FieldInsnNode fieldIns = null;
    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;
            /*
				 * Is field referencing outermost instance? if yes, ignore it
				 * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
				 */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }
            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
                final InsnList il = new InsnList();
                if (opcode == Opcodes.GETFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        il.add(new InsnNode(Opcodes.SWAP));
                    } else if (fieldType.getSize() == 2) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        // v
                        // GETFIELD
                        // v, w
                        il.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        il.add(new InsnNode(Opcodes.POP2));
                    // w, v
                    // -> Call
                    // w
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));
                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));
                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class), "notifyReadAccess", "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));
                i += il.size();
                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 47 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class Instrumenter method instrumentPUTXXXFieldAccesses.

private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName, final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;
    AbstractInsnNode ins = null;
    FieldInsnNode fieldIns = null;
    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;
            /*
				 * Is field referencing outermost instance? if yes, ignore it
				 * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
				 */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }
            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
                // construction of
                // Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
                // call
                final InsnList il = new InsnList();
                if (opcode == Opcodes.PUTFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2));
                        il.add(new InsnNode(Opcodes.POP));
                    } else if (fieldType.getSize() == 2) {
                        InsnList uglyList = new InsnList();
                        // v, w
                        uglyList.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        uglyList.add(new InsnNode(Opcodes.DUP));
                        // w, v, v
                        uglyList.add(new InsnNode(Opcodes.DUP2_X2));
                        // v, v, w, v, v
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // v, v, w
                        instructions.insertBefore(fieldIns, uglyList);
                    // PUTFIELD
                    // v
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));
                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));
                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification", "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));
                // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions
                this.captureId++;
                i += il.size();
                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 48 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchMovementInput.

private byte[] patchMovementInput(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found MovementInputFromOptions Class: " + classNode.name);
    MethodNode updatePlayerMoveState = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_78898_a"))) {
            updatePlayerMoveState = mn;
            break;
        }
    }
    if (updatePlayerMoveState != null) {
        logger.log(Level.DEBUG, " - Found updatePlayerMoveState");
        for (int i = 0; i < updatePlayerMoveState.instructions.size(); i++) {
            AbstractInsnNode ain = updatePlayerMoveState.instructions.get(i);
            if (ain instanceof FieldInsnNode) {
                FieldInsnNode fin = (FieldInsnNode) ain;
                if (fin.name.equals(MCPNames.field("field_78899_d"))) {
                    logger.log(Level.DEBUG, " - Found insert point");
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 0));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "modifyInput", "(Lnet/minecraft/util/MovementInputFromOptions;)V", false));
                    updatePlayerMoveState.instructions.insert(fin, toInsert);
                    break;
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 49 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project spring-loaded by spring-projects.

the class TypeDiffComputer method sameFieldInsn.

private static boolean sameFieldInsn(AbstractInsnNode o, AbstractInsnNode n) {
    FieldInsnNode oi = (FieldInsnNode) o;
    if (!(n instanceof FieldInsnNode)) {
        return false;
    }
    FieldInsnNode ni = (FieldInsnNode) n;
    return oi.name.equals(ni.name) && oi.desc.equals(ni.desc) && oi.owner.equals(ni.owner);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode)

Example 50 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project pinpoint by naver.

the class ASMClassNodeAdapter method addGetterMethod.

public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    Objects.requireNonNull(methodName, "methodName");
    Objects.requireNonNull(fieldNode, "fieldNode");
    // no argument is ().
    final String desc = "()" + fieldNode.getDesc();
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    final InsnList instructions = getInsnList(methodNode);
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    // get fieldNode.
    instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return of type.
    final Type type = Type.getType(fieldNode.getDesc());
    instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN)));
    addMethodNode0(methodNode);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)54 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)29 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)26 InsnList (org.objectweb.asm.tree.InsnList)24 InsnNode (org.objectweb.asm.tree.InsnNode)20 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)20 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)16 MethodNode (org.objectweb.asm.tree.MethodNode)15 Type (org.objectweb.asm.Type)14 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)13 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)11 ClassNode (org.objectweb.asm.tree.ClassNode)10 LabelNode (org.objectweb.asm.tree.LabelNode)9 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)8 ClassReader (org.objectweb.asm.ClassReader)7 Label (org.objectweb.asm.Label)5 FieldNode (org.objectweb.asm.tree.FieldNode)5 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)5 FrameNode (org.objectweb.asm.tree.FrameNode)4