Example usage for org.objectweb.asm.tree InsnList InsnList

List of usage examples for org.objectweb.asm.tree InsnList InsnList

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList InsnList.

Prototype

InsnList

Source Link

Usage

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateCallAllBindingsCallInOrgMethod.java

License:Open Source License

@Override
public boolean transform() {
    MethodNode method = getMethod(orgMethod);
    if ((method.access & Opcodes.ACC_ABSTRACT) != 0)
        return false;

    //      if (method.name.equals("<init>")) {
    //         int size = method.instructions.size();
    //         for (int i = 0; i < size; i++) {
    //            AbstractInsnNode insn = method.instructions.get(i);
    //            System.out.println(insn+" "+insn.getOpcode());
    //         }/*from   w w  w . j  av  a2 s . co  m*/
    //      }
    //      System.out.println("================");

    AbstractInsnNode insertBefore = null;
    if (orgMethod.getName().equals("<init>")) {
        // keep instructions, find insertion point:
        int last = method.instructions.size();
        LabelNode callAll = new LabelNode();
        while (--last >= 0) {
            if (method.instructions.get(last).getOpcode() == Opcodes.RETURN) {
                AbstractInsnNode ret = method.instructions.get(last);
                method.instructions.set(ret, callAll);
                insertBefore = callAll;
                break;
            }
        }
        if (insertBefore == null)
            throw new IllegalStateException("Insertion point for weaving into ctor not found!!!");

        // FIXME: triggers NPE in MethodVisitor.visitMaxs
        //         // replace RETURN with GOTO
        //         for (int i=0; i<last; i++) {
        //            AbstractInsnNode current = method.instructions.get(i);
        //            if (current.getOpcode() == Opcodes.RETURN)
        //               method.instructions.set(current, new JumpInsnNode(Opcodes.GOTO, callAll));
        //         }

    } else {
        method.instructions.clear();
    }

    // start of try-block:
    InsnList newInstructions = new InsnList();
    LabelNode start = new LabelNode();
    newInstructions.add(start);

    // put this on the stack
    newInstructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    // put boundMethodId on the stack
    newInstructions.add(createLoadIntConstant(boundMethodId));
    Type[] args = Type.getArgumentTypes(method.desc);
    // box the arguments
    newInstructions.add(getBoxingInstructions(args, false));

    // this.callAllBindings(boundMethodId, args);
    newInstructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, this.name,
            ConstantMembers.callAllBindingsClient.getName(),
            ConstantMembers.callAllBindingsClient.getSignature()));
    Type returnType = Type.getReturnType(method.desc);
    newInstructions.add(getUnboxingInstructionsForReturnValue(returnType));

    if (insertBefore != null) {
        method.instructions.insertBefore(insertBefore, newInstructions);
        method.instructions.remove(insertBefore); // remove extra RETURN
    } else {
        method.instructions.add(newInstructions);
    }

    //      if (method.name.equals("<init>")) {
    //         int size = method.instructions.size();
    //         for (int i = 0; i < size; i++) {
    //            AbstractInsnNode insn = method.instructions.get(i);
    //            System.out.println(insn+" "+insn.getOpcode());
    //         }
    //      }

    // catch and unwrap SneakyException:
    addCatchSneakyException(method, start);

    int localSlots = 0;
    int maxArgSize = 1;
    for (Type type : args) {
        int size = type.getSize();
        localSlots += size;
        if (size == 2)
            maxArgSize = 2;
    }
    method.maxStack = args.length > 0 ? 5 + maxArgSize : 3;
    method.maxLocals = localSlots + 1;

    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateCallAllBindingsCallInOrgMethod.java

License:Open Source License

protected InsnList getReturnInsn(Type returnType) {
    InsnList instructions = new InsnList();
    switch (returnType.getSort()) {
    case Type.VOID:
        instructions.add(new InsnNode(Opcodes.RETURN));
        break;/*  www.  j av a  2 s  .  c  om*/
    case Type.ARRAY:
    case Type.OBJECT:
        instructions.add(new InsnNode(Opcodes.ACONST_NULL));
        instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.INT:
    case Type.SHORT:
    case Type.LONG:
        instructions.add(new InsnNode(Opcodes.ICONST_0));
        instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.DOUBLE:
        instructions.add(new InsnNode(Opcodes.DCONST_0));
        instructions.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.FLOAT:
        instructions.add(new InsnNode(Opcodes.FCONST_0));
        instructions.add(new InsnNode(Opcodes.FRETURN));
        break;
    }
    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateDispatchCodeInCallAllBindingsAdapter.java

License:Open Source License

@Override
protected InsnList getBoxedArguments(Type[] args) {
    InsnList instructions = new InsnList();
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 2));
    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateDispatchCodeInOrgMethodAdapter.java

License:Open Source License

@Override
protected InsnList createInstructionsToCheackTeams(MethodNode method) {
    InsnList instructions = new InsnList();
    instructions.add(new InsnNode(Opcodes.DUP));
    LabelNode label = new LabelNode();
    //if (teams == null) {
    instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, label));
    instructions.add(new InsnNode(Opcodes.POP));
    //put the boundMethodId on the stack
    instructions.add(createLoadIntConstant(boundMethodId));
    Type[] args = Type.getArgumentTypes(method.desc);
    // box the arguments
    instructions.add(getBoxingInstructions(args, (method.access & Opcodes.ACC_STATIC) != 0));
    //callOrigStatic(boundMethodId, args);
    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, name, ConstantMembers.callOrigStatic.getName(),
            ConstantMembers.callOrigStatic.getSignature()));
    Type returnType = Type.getReturnType(method.desc);
    instructions.add(getUnboxingInstructionsForReturnValue(returnType));
    instructions.add(label);//from w w w . j  av a2s .  c om

    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateFieldAccessAdapter.java

License:Open Source License

@Override
public boolean transform() {

    InsnList instructions = new InsnList();
    // put accessId on the stack
    instructions.add(new IntInsnNode(Opcodes.ILOAD, firstArgIndex + 1));
    // read or write access
    LabelNode writeAccess = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNE, writeAccess));
    // read access
    if (field.isStatic()) {
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, name, field.getName(), field.getSignature()));
    } else {//from  w  w  w  .  j  ava2  s. c om
        // put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, field.getName(), field.getSignature()));
    }

    //box value as "Object"
    Type type = Type.getType(field.getSignature());
    instructions.add(AsmTypeHelper.getBoxingInstructionForType(type));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //write access
    instructions.add(writeAccess);
    //put "args" on the stack 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
    //get the first element of "args"
    instructions.add(new InsnNode(Opcodes.ICONST_0));
    instructions.add(new InsnNode(Opcodes.AALOAD));
    //unbox it
    if (type.getSort() != Type.ARRAY && type.getSort() != Type.OBJECT) {
        String objectType = AsmTypeHelper.getObjectType(type);
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
        instructions.add(AsmTypeHelper.getUnboxingInstructionForType(type, objectType));
    } else {
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, type.getInternalName()));
    }

    if (field.isStatic()) {
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTSTATIC, name, field.getName(), field.getSignature()));
    } else {
        //put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        instructions.add(new InsnNode(Opcodes.SWAP));
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, field.getName(), field.getSignature()));
    }

    //dummy return 
    instructions.add(new InsnNode(Opcodes.ACONST_NULL));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //add the instructions to a new label in the existing switch
    MethodNode method = getMethod(access);
    addNewLabelToSwitch(method.instructions, instructions, accessId);

    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateMethodAccessAdapter.java

License:Open Source License

@Override
public boolean transform() {
    MethodNode methodNode = getMethod(method);
    InsnList instructions = new InsnList();

    if (isConstructor) {
        // create empty object for constructor invocation:
        instructions.add(new TypeInsnNode(Opcodes.NEW, name));
        instructions.add(new InsnNode(Opcodes.DUP));
    } else if (!method.isStatic()) {
        //put "this" on the stack for a non-static method
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    }/*from  ww w.j ava  2 s.  c  o  m*/

    //Unbox arguments
    Type[] args = Type.getArgumentTypes(methodNode.desc);

    if (args.length > 0) {

        for (int i = 0; i < args.length; i++) {
            instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
            instructions.add(createLoadIntConstant(i));
            instructions.add(new InsnNode(Opcodes.AALOAD));
            Type arg = args[i];
            if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) {
                String objectType = AsmTypeHelper.getObjectType(arg);
                instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
                instructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType));
            } else {
                instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName()));
            }
        }
    }

    //call original method
    int opcode = Opcodes.INVOKEVIRTUAL;
    if (method.isStatic()) {
        opcode = Opcodes.INVOKESTATIC;
    } else if (isConstructor) {
        opcode = Opcodes.INVOKESPECIAL;
    }
    instructions.add(new MethodInsnNode(opcode, name, method.getName(), method.getSignature()));

    //box return value
    Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY
            && returnType.getSort() != Type.VOID) {

        instructions.add(AsmTypeHelper.getBoxingInstructionForType(returnType));
        instructions.add(new InsnNode(Opcodes.ARETURN));
    } else if (returnType.getSort() == Type.VOID && !isConstructor) {
        instructions.add(new InsnNode(Opcodes.ACONST_NULL));
        instructions.add(new InsnNode(Opcodes.ARETURN));
    } else {
        instructions.add(new InsnNode(Opcodes.ARETURN));
    }

    //add the instructions to a new label in the existing switch
    MethodNode access = getMethod(this.access);
    addNewLabelToSwitch(access.instructions, instructions, accessId);

    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateSuperCallInCallOrigAdapter.java

License:Open Source License

@Override
public boolean transform() {
    MethodNode callOrig = getMethod(ConstantMembers.callOrig);
    InsnList instructions = new InsnList();

    Type[] args = Type.getArgumentTypes(callOrig.desc);
    addInstructionsForLoadArguments(instructions, args, false);

    instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, superClassName,
            ConstantMembers.callOrig.getName(), ConstantMembers.callOrig.getSignature()));
    instructions.add(new InsnNode(Opcodes.ARETURN));
    addNewLabelToSwitch(callOrig.instructions, instructions, joinpointId);
    callOrig.maxStack = Math.max(callOrig.maxStack, args.length + 1);
    return true;/*  www . j  a  va 2  s . c om*/
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.MoveCodeToCallOrigAdapter.java

License:Open Source License

public boolean transform() {
    MethodNode orgMethod = getMethod(method);
    if ((orgMethod.access & Opcodes.ACC_ABSTRACT) != 0)
        return false;

    MethodNode callOrig = getMethod(this.callOrig);

    Type returnType = Type.getReturnType(orgMethod.desc);

    InsnList newInstructions = new InsnList();

    //Unboxing arguments
    Type[] args = Type.getArgumentTypes(orgMethod.desc);

    int boundMethodIdSlot = firstArgIndex;

    if (args.length > 0) {
        // move boundMethodId to a higher slot, to make lower slots available for original locals
        newInstructions.add(new IntInsnNode(Opcodes.ILOAD, boundMethodIdSlot));
        boundMethodIdSlot = callOrig.maxLocals + 1;
        newInstructions.add(new IntInsnNode(Opcodes.ISTORE, boundMethodIdSlot));

        newInstructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + argOffset + 1));

        int slot = firstArgIndex + argOffset;
        for (int i = argOffset; i < args.length; i++) {
            if (i < args.length - 1) {
                newInstructions.add(new InsnNode(Opcodes.DUP));
            }/*from w w w.  ja  va 2  s . co  m*/
            newInstructions.add(createLoadIntConstant(i));
            newInstructions.add(new InsnNode(Opcodes.AALOAD));
            Type arg = args[i];
            if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) {
                String objectType = AsmTypeHelper.getObjectType(arg);
                newInstructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
                newInstructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType));
            } else {
                newInstructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName()));
            }

            newInstructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ISTORE), slot));
            slot += arg.getSize();
        }
    }

    if (superIsWeavable)
        adjustSuperCalls(orgMethod.instructions, orgMethod.name, args, returnType, boundMethodIdSlot);

    // replace return of the original method with areturn and box the result value if needed
    replaceReturn(orgMethod.instructions, returnType);

    newInstructions.add(orgMethod.instructions);

    addNewLabelToSwitch(callOrig.instructions, newInstructions, boundMethodId);

    // a minimum stacksize of 3 is needed to box the arguments
    callOrig.maxStack = Math.max(Math.max(callOrig.maxStack, orgMethod.maxStack), 3);

    // we have to increment the max. stack size, because we have to put NULL on the stack
    if (returnType.getSort() == Type.VOID) {
        callOrig.maxStack += 1;
    }
    callOrig.maxLocals = Math.max(callOrig.maxLocals, orgMethod.maxLocals);
    return true;
}

From source file:org.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static InsnList getImportantList(InsnList list) {

    if (list.size() == 0)
        return list;

    final HashMap<LabelNode, LabelNode> labels = new HashMap<>();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext())
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }/*from w w w. j av a2  s  .c  om*/

    final InsnList importantNodeList = new InsnList();

    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {

        if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
            continue;
        }

        importantNodeList.add(insn.clone(labels));
    }

    return importantNodeList;
}

From source file:org.evosuite.coverage.mutation.Mutation.java

License:Open Source License

/**
 * <p>//w ww  .  ja va  2s  .  c o  m
 * Constructor for Mutation.
 * </p>
 * 
 * @param className
 *            a {@link java.lang.String} object.
 * @param methodName
 *            a {@link java.lang.String} object.
 * @param mutationName
 *            a {@link java.lang.String} object.
 * @param id
 *            a int.
 * @param original
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @param mutation
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param distance
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 */
public Mutation(String className, String methodName, String mutationName, int id, BytecodeInstruction original,
        AbstractInsnNode mutation, InsnList distance) {
    this.className = className;
    this.methodName = methodName;
    this.mutationName = mutationName;
    this.id = id;
    this.original = original;
    this.mutation = new InsnList();
    this.mutation.add(mutation);
    this.infection = distance;
    this.lineNo = original.getLineNumber();
}