Example usage for org.objectweb.asm Opcodes NEW

List of usage examples for org.objectweb.asm Opcodes NEW

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes NEW.

Prototype

int NEW

To view the source code for org.objectweb.asm Opcodes NEW.

Click Source Link

Usage

From source file:org.evosuite.graphs.cfg.BytecodeInstruction.java

License:Open Source License

/**
 * <p>// www  .  j a v a  2 s .  c  o  m
 * getASMNodeString
 * </p>
 * 
 * @return a {@link java.lang.String} object.
 */
public String getASMNodeString() {
    String type = getType();
    String opcode = getInstructionType();

    String stack = "";
    if (frame == null)
        stack = "null";
    else
        for (int i = 0; i < frame.getStackSize(); i++) {
            stack += frame.getStack(i) + ",";
        }

    if (asmNode instanceof LabelNode) {
        return "LABEL " + ((LabelNode) asmNode).getLabel().toString();
    } else if (asmNode instanceof FieldInsnNode)
        return "Field" + " " + ((FieldInsnNode) asmNode).owner + "." + ((FieldInsnNode) asmNode).name + " Type="
                + type + ", Opcode=" + opcode;
    else if (asmNode instanceof FrameNode)
        return "Frame" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof IincInsnNode)
        return "IINC " + ((IincInsnNode) asmNode).var + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof InsnNode)
        return "" + opcode;
    else if (asmNode instanceof IntInsnNode)
        return "INT " + ((IntInsnNode) asmNode).operand + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof MethodInsnNode)
        return opcode + " " + ((MethodInsnNode) asmNode).owner + "." + ((MethodInsnNode) asmNode).name
                + ((MethodInsnNode) asmNode).desc;
    else if (asmNode instanceof JumpInsnNode)
        return "JUMP " + ((JumpInsnNode) asmNode).label.getLabel() + " Type=" + type + ", Opcode=" + opcode
                + ", Stack: " + stack + " - Line: " + lineNumber;
    else if (asmNode instanceof LdcInsnNode)
        return "LDC " + ((LdcInsnNode) asmNode).cst + " Type=" + type; // +
    // ", Opcode=";
    // + opcode; // cst starts with mutationid if
    // this is location of mutation
    else if (asmNode instanceof LineNumberNode)
        return "LINE " + " " + ((LineNumberNode) asmNode).line;
    else if (asmNode instanceof LookupSwitchInsnNode)
        return "LookupSwitchInsnNode" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof MultiANewArrayInsnNode)
        return "MULTIANEWARRAY " + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof TableSwitchInsnNode)
        return "TableSwitchInsnNode" + " " + asmNode.getOpcode() + " Type=" + type + ", Opcode=" + opcode;
    else if (asmNode instanceof TypeInsnNode) {
        switch (asmNode.getOpcode()) {
        case Opcodes.NEW:
            return "NEW " + ((TypeInsnNode) asmNode).desc;
        case Opcodes.ANEWARRAY:
            return "ANEWARRAY " + ((TypeInsnNode) asmNode).desc;
        case Opcodes.CHECKCAST:
            return "CHECKCAST " + ((TypeInsnNode) asmNode).desc;
        case Opcodes.INSTANCEOF:
            return "INSTANCEOF " + ((TypeInsnNode) asmNode).desc;
        default:
            return "Unknown node" + " Type=" + type + ", Opcode=" + opcode;
        }
    }
    // return "TYPE " + " " + node.getOpcode() + " Type=" + type
    // + ", Opcode=" + opcode;
    else if (asmNode instanceof VarInsnNode)
        return opcode + " " + ((VarInsnNode) asmNode).var;
    else
        return "Unknown node" + " Type=" + type + ", Opcode=" + opcode;
}

From source file:org.evosuite.graphs.cfg.BytecodeInstructionPool.java

License:Open Source License

/**
 * Determine how many bytes the current instruction occupies together with
 * its operands/*from w  w  w .  j  a  v a  2s. c  o m*/
 * 
 * @return
 */
private int getBytecodeIncrement(AbstractInsnNode instructionNode) {
    int opcode = instructionNode.getOpcode();
    switch (opcode) {
    case Opcodes.ALOAD: // index
    case Opcodes.ASTORE: // index
    case Opcodes.DLOAD:
    case Opcodes.DSTORE:
    case Opcodes.FLOAD:
    case Opcodes.FSTORE:
    case Opcodes.ILOAD:
    case Opcodes.ISTORE:
    case Opcodes.LLOAD:
    case Opcodes.LSTORE:
        VarInsnNode varNode = (VarInsnNode) instructionNode;
        if (varNode.var > 3)
            return 1;
        else
            return 0;
    case Opcodes.BIPUSH: // byte
    case Opcodes.NEWARRAY:
    case Opcodes.RET:
        return 1;
    case Opcodes.LDC:
        LdcInsnNode ldcNode = (LdcInsnNode) instructionNode;
        if (ldcNode.cst instanceof Double || ldcNode.cst instanceof Long)
            return 2; // LDC2_W
        else
            return 1;
    case 19: //LDC_W
    case 20: //LDC2_W
        return 2;
    case Opcodes.ANEWARRAY: // indexbyte1, indexbyte2
    case Opcodes.CHECKCAST: // indexbyte1, indexbyte2
    case Opcodes.GETFIELD:
    case Opcodes.GETSTATIC:
    case Opcodes.GOTO:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IFLE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFNE:
    case Opcodes.IFEQ:
    case Opcodes.IFNONNULL:
    case Opcodes.IFNULL:
    case Opcodes.IINC:
    case Opcodes.INSTANCEOF:
    case Opcodes.INVOKESPECIAL:
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.JSR:
    case Opcodes.NEW:
    case Opcodes.PUTFIELD:
    case Opcodes.PUTSTATIC:
    case Opcodes.SIPUSH:
        // case Opcodes.LDC_W
        // case Opcodes.LDC2_W

        return 2;
    case Opcodes.MULTIANEWARRAY:
        return 3;
    case Opcodes.INVOKEDYNAMIC:
    case Opcodes.INVOKEINTERFACE:
        return 4;

    case Opcodes.LOOKUPSWITCH:
    case Opcodes.TABLESWITCH:
        // TODO: Could be more
        return 4;
    // case Opcodes.GOTO_W 
    // case Opcodes.JSR_W
    }
    return 0;
}

From source file:org.evosuite.instrumentation.ArrayAllocationLimitMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override// w ww.java  2 s .  c  o m
public void visitIntInsn(int opcode, int operand) {
    if (opcode == Opcodes.NEWARRAY) {
        Label origTarget = new Label();
        visitInsn(Opcodes.DUP);
        visitFieldInsn(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(org.evosuite.Properties.class),
                "ARRAY_LIMIT", "I");
        super.visitJumpInsn(Opcodes.IF_ICMPLT, origTarget);
        super.visitTypeInsn(Opcodes.NEW, PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class));
        super.visitInsn(Opcodes.DUP);
        super.visitMethodInsn(Opcodes.INVOKESPECIAL,
                PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class), "<init>", "()V", false);
        super.visitInsn(Opcodes.ATHROW);
        super.visitLabel(origTarget);

    }
    super.visitIntInsn(opcode, operand);
}

From source file:org.evosuite.instrumentation.ArrayAllocationLimitMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*w  w w.j  a va2  s. com*/
public void visitTypeInsn(int opcode, String type) {

    if (opcode == Opcodes.ANEWARRAY) {
        Label origTarget = new Label();
        visitInsn(Opcodes.DUP);
        visitFieldInsn(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(org.evosuite.Properties.class),
                "ARRAY_LIMIT", "I");
        super.visitJumpInsn(Opcodes.IF_ICMPLT, origTarget);
        super.visitTypeInsn(Opcodes.NEW, PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class));
        super.visitInsn(Opcodes.DUP);
        super.visitMethodInsn(Opcodes.INVOKESPECIAL,
                PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class), "<init>", "()V", false);
        super.visitInsn(Opcodes.ATHROW);
        super.visitLabel(origTarget);

    }
    super.visitTypeInsn(opcode, type);
}

From source file:org.evosuite.instrumentation.ArrayAllocationLimitMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*  w w  w  .j av a  2s .  co  m*/
public void visitMultiANewArrayInsn(String desc, int dims) {

    Label origTarget = new Label();
    Label errorTarget = new Label();

    // Multidimensional arrays can only have max 256 dimensions
    if (Properties.ARRAY_LIMIT < 256) {
        push(dims);
        visitFieldInsn(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(org.evosuite.Properties.class),
                "ARRAY_LIMIT", "I");
        super.visitJumpInsn(Opcodes.IF_ICMPGE, errorTarget);
    }

    // Check each of the dimensions
    Map<Integer, Integer> to = new HashMap<Integer, Integer>();
    for (int i = dims - 1; i >= 0; i--) {
        int loc = newLocal(Type.INT_TYPE);
        storeLocal(loc);
        to.put(i, loc);
    }
    for (int i = 0; i < dims; i++) {
        loadLocal(to.get(i));
        visitFieldInsn(Opcodes.GETSTATIC, "org/evosuite/Properties", "ARRAY_LIMIT", "I");
        super.visitJumpInsn(Opcodes.IF_ICMPGE, errorTarget);
    }
    goTo(origTarget);
    super.visitLabel(errorTarget);
    super.visitTypeInsn(Opcodes.NEW, PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class));
    super.visitInsn(Opcodes.DUP);
    super.visitMethodInsn(Opcodes.INVOKESPECIAL,
            PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class), "<init>", "()V", false);
    super.visitInsn(Opcodes.ATHROW);
    super.visitLabel(origTarget);

    // Restore original dimensions
    for (int i = 0; i < dims; i++) {
        loadLocal(to.get(i));
    }

    super.visitMultiANewArrayInsn(desc, dims);
}

From source file:org.evosuite.instrumentation.error.CastErrorInstrumentation.java

License:Open Source License

@Override
public void visitTypeInsn(int opcode, String type) {

    if (opcode == Opcodes.CHECKCAST) {
        Label origTarget = new Label();
        // Label origTarget = new AnnotatedLabel();
        // origTarget.info = Boolean.FALSE;
        mv.visitInsn(Opcodes.DUP);/*from  w  ww .  j a  v a 2 s  .c o m*/
        mv.tagBranch();
        mv.visitJumpInsn(Opcodes.IFNULL, origTarget);
        mv.visitInsn(Opcodes.DUP);
        mv.visitTypeInsn(Opcodes.INSTANCEOF, type);
        mv.tagBranch();
        mv.visitJumpInsn(Opcodes.IFNE, origTarget);
        mv.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "()V", false);
        mv.visitInsn(Opcodes.ATHROW);
        mv.visitLabel(origTarget);
        mv.tagBranchExit();
    }
}

From source file:org.evosuite.instrumentation.error.ErrorBranchInstrumenter.java

License:Open Source License

protected void insertBranch(int opcode, String exception) {
    Label origTarget = new Label();
    mv.tagBranch();/*from   w  w  w . j  a  va2s  . c  o  m*/
    mv.visitJumpInsn(opcode, origTarget);
    mv.visitTypeInsn(Opcodes.NEW, exception);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exception, "<init>", "()V", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(origTarget);
    mv.tagBranchExit();
}

From source file:org.evosuite.instrumentation.error.ErrorBranchInstrumenter.java

License:Open Source License

protected void insertBranchWithoutTag(int opcode, String exception) {
    Label origTarget = new Label();
    mv.visitJumpInsn(opcode, origTarget);
    mv.visitTypeInsn(Opcodes.NEW, exception);
    mv.visitInsn(Opcodes.DUP);//from   www  . j  av a  2 s  .co  m
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exception, "<init>", "()V", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(origTarget);
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

@SuppressWarnings("unchecked")
private void addFieldRegistryRegisterCall(final MethodNode methodNode) {
    AbstractInsnNode ins = null;/*from  w ww.j  av a2  s  . com*/
    ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

    int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

    while (iter.hasNext()) {
        ins = iter.next();

        if (ins instanceof MethodInsnNode) {
            MethodInsnNode mins = (MethodInsnNode) ins;
            if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                if (mins.name.startsWith("<init>")) {
                    if (numInvokeSpecials == 0) {
                        break;
                    } else {
                        numInvokeSpecials--;
                    }
                }
            }
        } else if (ins instanceof TypeInsnNode) {
            TypeInsnNode typeIns = (TypeInsnNode) ins;
            if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                numInvokeSpecials++;
            }
        }
    }

    final InsnList instructions = new InsnList();

    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class),
            "register", "(Ljava/lang/Object;)V"));

    methodNode.instructions.insert(ins, instructions);
}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

/**
 *    public int myMethod(int i)/*from w  w  w .  jav  a 2 s  . co m*/
   {
 try
 {
    return _sw_prototype_original_myMethod(i)
 }
 finally
 {
    Capturer.enable();
 }
   }
        
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;

    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
            methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;

    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;

    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;

    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);

    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();

    final InsnList wInstructions = wrappingMethodNode.instructions;

    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor 

        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;

        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

        int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);

            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }

    int varReturnValue = 0;

    final Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));

    } else {

        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));

        //--- create "Object returnValue = null;"

        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }

        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;

            // long/double take two registers
            if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
                varReturnValue++;
            }
        }

        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }

    int var = 0;

    // --- L0
    wInstructions.add(l0);

    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className,
            wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));

    // --- construct call to wrapped methode

    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }

    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            var++;
        }
    }

    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }

    var++;

    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));

        // --- L1

        wInstructions.add(l1);

        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));

        // FIXME <--- DUPLICATE CODE

        // --- L2

        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result

        this.addBoxingStmt(wInstructions, returnType);

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));

        this.addUnBoxingStmt(wInstructions, returnType);

        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var

        // --- L1

        wInstructions.add(l1);

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));

        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);

        //---- L2

        wInstructions.add(l2);

        wInstructions.add(
                new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) },
                        1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}