Example usage for org.objectweb.asm Opcodes ATHROW

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

Introduction

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

Prototype

int ATHROW

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

Click Source Link

Usage

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  .java  2 s  . 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  ww  w. ja v  a  2s  .c o  m
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exception, "<init>", "()V", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(origTarget);
}

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

License:Open Source License

@Override
public void visitEnd() {
    if (methodName.equals("<clinit>")) {
        super.visitLabel(endingTryLabel);
        String executionTracerClassName = ExecutionTracer.class.getName().replace('.', '/');
        String executionTracerDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class));

        String classNameWithDots = className.replace('/', '.');
        super.visitLdcInsn(classNameWithDots);
        super.visitMethodInsn(INVOKESTATIC, executionTracerClassName, EXIT_CLASS_INIT,
                executionTracerDescriptor, false);
        super.visitInsn(Opcodes.ATHROW);

        // regenerate try-catch table
        for (TryCatchBlock tryCatchBlock : tryCatchBlocks) {
            super.visitTryCatchBlock(tryCatchBlock.start, tryCatchBlock.end, tryCatchBlock.handler,
                    tryCatchBlock.type);
        }//www.ja v  a  2s.  c  om
        // add new try-catch for exiting method
        super.visitTryCatchBlock(startingTryLabel, endingTryLabel, endingTryLabel, null);
    }
    super.visitEnd();
}

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

License:Open Source License

/** {@inheritDoc} */
@Override//from w ww . j a  v  a  2 s .  c o  m
public void visitInsn(int opcode) {
    if (opcode == Opcodes.ATHROW && !inErrorBranch) {
        super.visitInsn(Opcodes.DUP);
        this.visitLdcInsn(className);
        this.visitLdcInsn(fullMethodName);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
                "exceptionThrown", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V", false);
    }
    super.visitInsn(opcode);
}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*w w w.  ja v a  2 s . c om*/
public void onMethodExit(int opcode) {
    // TODO: Check for <clinit>

    if (opcode != Opcodes.ATHROW) {

        mv.visitLdcInsn(className);
        mv.visitLdcInsn(fullMethodName);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(org.evosuite.testcase.execution.ExecutionTracer.class),
                "leftMethod", "(Ljava/lang/String;Ljava/lang/String;)V", false);
    }
    super.onMethodExit(opcode);
}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*from ww w .ja  v  a  2 s . com*/
public void visitInsn(int opcode) {
    if (!methodName.equals("<clinit>")) {
        switch (opcode) {
        case Opcodes.IRETURN:
            callLogIReturn();
            break;
        case Opcodes.ARETURN:
            callLogAReturn();
            break;
        case Opcodes.ATHROW:
            break;
        case Opcodes.DRETURN:
            callLogDReturn();
            break;
        case Opcodes.FRETURN:
            callLogFReturn();
            break;
        case Opcodes.LRETURN:
            callLogLReturn();
            break;
        case Opcodes.RETURN:
            break;
        default:
            break;
        }
    }
    super.visitInsn(opcode);

}

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

License:Open Source License

/** {@inheritDoc} */
@Override/*from   ww w . j ava2 s .  c  o m*/
public void visitInsn(int opcode) {
    if (opcode == Opcodes.ATHROW) {
        super.visitInsn(Opcodes.DUP);
        this.visitLdcInsn(className);
        this.visitLdcInsn(methodName);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
                "exceptionThrown", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V", false);
    }
    super.visitInsn(opcode);
}

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

License:Open Source License

/**
 *    public int myMethod(int i)/*from  w ww  . j a  va2s  .  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;
}

From source file:org.iobserve.mobile.instrument.bytecode.SensorBytecodeInstrumenter.java

License:Apache License

/**
 * {@inheritDoc}/*from w w w  .  j av  a2 s  .com*/
 */
@Override
public void onMethodExit(final int opcode, final String owner, final String name, final String desc,
        final AdviceAdapter parent, final MethodVisitor mv) {
    if (opcode == Opcodes.ATHROW) {
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.LLOAD, index);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(),
                exitBodyErrorMethod.getName(), exitBodyErrorType.getDescriptor(), false);
    } else {
        mv.visitVarInsn(Opcodes.LLOAD, index);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(), exitBodyMethod.getName(),
                exitBodyType.getDescriptor(), false);
    }
}

From source file:org.jacoco.core.internal.analysis.filter.AbstractMatcherTest.java

License:Open Source License

@Test
public void nextIs() {
    m.visitInsn(Opcodes.NOP);//ww  w .j  a  v a2 s .  co m
    m.visitInsn(Opcodes.NOP);

    // should set cursor to null when opcode mismatch
    matcher.cursor = m.instructions.getFirst();
    matcher.nextIs(Opcodes.ATHROW);
    assertNull(matcher.cursor);

    // should set cursor to next instruction when match
    matcher.cursor = m.instructions.getFirst();
    matcher.nextIs(Opcodes.NOP);
    assertSame(m.instructions.getLast(), matcher.cursor);

    // should not do anything when cursor is null
    matcher.cursor = null;
    matcher.nextIs(Opcodes.NOP);
}