Java Code Examples for jdk.internal.org.objectweb.asm.MethodVisitor#visitTryCatchBlock()

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitTryCatchBlock() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {
    Label tryLoadBegin = new Label();
    Label tryLoadEnd = new Label();
    Label catchLoadBlock = new Label();
    mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");

    // Try to load a class that does not exist to provoke resolution errors
    mv.visitLabel(tryLoadBegin);
    mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");
    mv.visitLabel(tryLoadEnd);

    // No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen
    mv.visitJumpInsn(GOTO, classExists);

    mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });
    mv.visitLabel(catchLoadBlock);

    // Ignore the expected NoClassDefFoundError
    mv.visitInsn(POP);
}
 
Example 2
private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {
    Label tryLoadBegin = new Label();
    Label tryLoadEnd = new Label();
    Label catchLoadBlock = new Label();
    mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");

    // Try to load a class that does not exist to provoke resolution errors
    mv.visitLabel(tryLoadBegin);
    mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");
    mv.visitLabel(tryLoadEnd);

    // No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen
    mv.visitJumpInsn(GOTO, classExists);

    mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });
    mv.visitLabel(catchLoadBlock);

    // Ignore the expected NoClassDefFoundError
    mv.visitInsn(POP);
}
 
Example 3
private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {
    Label tryLoadBegin = new Label();
    Label tryLoadEnd = new Label();
    Label catchLoadBlock = new Label();
    mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");

    // Try to load a class that does not exist to provoke resolution errors
    mv.visitLabel(tryLoadBegin);
    mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");
    mv.visitLabel(tryLoadEnd);

    // No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen
    mv.visitJumpInsn(GOTO, classExists);

    mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });
    mv.visitLabel(catchLoadBlock);

    // Ignore the expected NoClassDefFoundError
    mv.visitInsn(POP);
}
 
Example 4
private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {
    Label tryLoadBegin = new Label();
    Label tryLoadEnd = new Label();
    Label catchLoadBlock = new Label();
    mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");

    // Try to load a class that does not exist to provoke resolution errors
    mv.visitLabel(tryLoadBegin);
    mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");
    mv.visitLabel(tryLoadEnd);

    // No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen
    mv.visitJumpInsn(GOTO, classExists);

    mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });
    mv.visitLabel(catchLoadBlock);

    // Ignore the expected NoClassDefFoundError
    mv.visitInsn(POP);
}
 
Example 5
private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {
    Label tryLoadBegin = new Label();
    Label tryLoadEnd = new Label();
    Label catchLoadBlock = new Label();
    mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");

    // Try to load a class that does not exist to provoke resolution errors
    mv.visitLabel(tryLoadBegin);
    mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");
    mv.visitLabel(tryLoadEnd);

    // No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen
    mv.visitJumpInsn(GOTO, classExists);

    mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });
    mv.visitLabel(catchLoadBlock);

    // Ignore the expected NoClassDefFoundError
    mv.visitInsn(POP);
}
 
Example 6
Source Project: Bytecoder   File: ProxyGenerator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Generate the static initializer method for the proxy class.
 */
private void generateStaticInitializer() {

    MethodVisitor mv = visitMethod(Modifier.STATIC, NAME_CLINIT,
            "()V", null, null);
    mv.visitCode();
    Label L_startBlock = new Label();
    Label L_endBlock = new Label();
    Label L_NoMethodHandler = new Label();
    Label L_NoClassHandler = new Label();

    mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_NoMethodHandler,
            JL_NO_SUCH_METHOD_EX);
    mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_NoClassHandler,
            JL_CLASS_NOT_FOUND_EX);

    mv.visitLabel(L_startBlock);
    for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
        for (ProxyMethod pm : sigmethods) {
            pm.codeFieldInitialization(mv, className);
        }
    }
    mv.visitInsn(RETURN);
    mv.visitLabel(L_endBlock);
    // Generate exception handler

    mv.visitLabel(L_NoMethodHandler);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitTypeInsn(Opcodes.NEW, JL_NO_SUCH_METHOD_ERROR);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, JL_THROWABLE,
            "getMessage", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, JL_NO_SUCH_METHOD_ERROR,
            "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);

    mv.visitLabel(L_NoClassHandler);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitTypeInsn(Opcodes.NEW, JL_NO_CLASS_DEF_FOUND_ERROR);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, JL_THROWABLE,
            "getMessage", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, JL_NO_CLASS_DEF_FOUND_ERROR,
            "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);

    // Maxs computed by ClassWriter.COMPUTE_FRAMES, these arguments ignored
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
 
Example 7
Source Project: Bytecoder   File: ProxyGenerator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Generate this method, including the code and exception table entry.
 */
private void generateMethod(ClassWriter cw, String className) {
    MethodType mt = MethodType.methodType(returnType, parameterTypes);
    String desc = mt.toMethodDescriptorString();
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL,
            method.getName(), desc, null,
            typeNames(Arrays.asList(exceptionTypes)));

    int[] parameterSlot = new int[parameterTypes.length];
    int nextSlot = 1;
    for (int i = 0; i < parameterSlot.length; i++) {
        parameterSlot[i] = nextSlot;
        nextSlot += getWordsPerType(parameterTypes[i]);
    }

    mv.visitCode();
    Label L_startBlock = new Label();
    Label L_endBlock = new Label();
    Label L_RuntimeHandler = new Label();
    Label L_ThrowableHandler = new Label();

    List<Class<?>> catchList = computeUniqueCatchList(exceptionTypes);
    if (catchList.size() > 0) {
        for (Class<?> ex : catchList) {
            mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_RuntimeHandler,
                    dotToSlash(ex.getName()));
        }

        mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_ThrowableHandler,
                JL_THROWABLE);
    }
    mv.visitLabel(L_startBlock);

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, JLR_PROXY, handlerFieldName,
            LJLR_INVOCATION_HANDLER);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETSTATIC, dotToSlash(className), methodFieldName,
            LJLR_METHOD);

    if (parameterTypes.length > 0) {
        // Create an array and fill with the parameters converting primitives to wrappers
        emitIconstInsn(mv, parameterTypes.length);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, JL_OBJECT);
        for (int i = 0; i < parameterTypes.length; i++) {
            mv.visitInsn(DUP);
            emitIconstInsn(mv, i);
            codeWrapArgument(mv, parameterTypes[i], parameterSlot[i]);
            mv.visitInsn(Opcodes.AASTORE);
        }
    } else {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

    mv.visitMethodInsn(INVOKEINTERFACE, JLR_INVOCATION_HANDLER,
            "invoke",
            "(Ljava/lang/Object;Ljava/lang/reflect/Method;" +
                    "[Ljava/lang/Object;)Ljava/lang/Object;", true);

    if (returnType == void.class) {
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
    } else {
        codeUnwrapReturnValue(mv, returnType);
    }

    mv.visitLabel(L_endBlock);

    // Generate exception handler
    mv.visitLabel(L_RuntimeHandler);
    mv.visitInsn(ATHROW);   // just rethrow the exception

    mv.visitLabel(L_ThrowableHandler);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitTypeInsn(Opcodes.NEW, JLR_UNDECLARED_THROWABLE_EX);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, JLR_UNDECLARED_THROWABLE_EX,
            "<init>", "(Ljava/lang/Throwable;)V", false);
    mv.visitInsn(ATHROW);
    // Maxs computed by ClassWriter.COMPUTE_FRAMES, these arguments ignored
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
 
Example 8
/**
 * Makes the given visitor visit this try catch block.
 *
 * @param mv a method visitor.
 */
public void accept(final MethodVisitor mv) {
    mv.visitTryCatchBlock(start.getLabel(), end.getLabel(), handler == null
            ? null
            : handler.getLabel(), type);
}