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

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitTypeInsn() . 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
Source File: Proxy.java    From totallylazy with Apache License 2.0 6 votes vote down vote up
private static void loadArguments(MethodVisitor mv, Method method) {
    int arguments = method.getParameterCount();
    mv.visitLdcInsn(arguments);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");

    Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0, local = 0; i < parameterTypes.length; i++) {
        Class<?> parameterType = parameterTypes[i];
        local++;
        mv.visitInsn(DUP);
        mv.visitLdcInsn(i);
        mv.visitVarInsn(Asm.load(parameterType), local);
        if (parameterType.isPrimitive() && !parameterType.equals(void.class)) {
            String internalName = Type.getInternalName(Reflection.box(parameterType));
            mv.visitMethodInsn(INVOKESTATIC, internalName, "valueOf", format("(%s)L%s;", Type.getDescriptor(parameterType), internalName), false);
        }
        if (parameterType.equals(double.class) || parameterType.equals(long.class)) local++;
        mv.visitInsn(AASTORE);
    }
}
 
Example 2
Source File: Proxy.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
private static void returnResult(MethodVisitor mv, Method method) {
    Class<?> returnType = method.getReturnType();
    if (returnType.equals(void.class)) {
        mv.visitInsn(POP);
    } else if (returnType.isPrimitive()) {
        String internalName = Type.getInternalName(Reflection.box(returnType));
        mv.visitTypeInsn(CHECKCAST, internalName);
        mv.visitMethodInsn(INVOKEVIRTUAL, internalName, format("%sValue", returnType.getSimpleName()), format("()%s", Type.getDescriptor(returnType)), false);
    } else {
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(returnType));
    }
    mv.visitInsn(Asm.returns(returnType));
}
 
Example 3
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate bytecode for SystemModules::hashes method
 */
private void genHashesMethod() {
    MethodVisitor hmv =
        cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
                       "hashes",
                       "()" + MODULE_HASHES_ARRAY_SIGNATURE,
                       "()" + MODULE_HASHES_ARRAY_SIGNATURE,
                       null);
    hmv.visitCode();
    pushInt(hmv, moduleInfos.size());
    hmv.visitTypeInsn(ANEWARRAY, "jdk/internal/module/ModuleHashes");
    hmv.visitVarInsn(ASTORE, MH_VAR);

    for (int index = 0; index < moduleInfos.size(); index++) {
        ModuleInfo minfo = moduleInfos.get(index);
        if (minfo.recordedHashes() != null) {
            new ModuleHashesBuilder(minfo.recordedHashes(),
                                    index,
                                    hmv).build();
        }
    }

    hmv.visitVarInsn(ALOAD, MH_VAR);
    hmv.visitInsn(ARETURN);
    hmv.visitMaxs(0, 0);
    hmv.visitEnd();
}
 
Example 4
Source File: RedefineRunningMethodsWithResolutionErrors.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void createThrowRuntimeExceptionCode(MethodVisitor mv, String msg) {
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(msg);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");
    mv.visitInsn(ATHROW);
}
 
Example 5
Source File: TestANewArray.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static byte[] dumpClassFile(int cfv, int testDimension264, String arrayDim) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;

    classCName = "classCName_" + cfv + "_" + testDimension264;

    cw.visit(cfv, ACC_PUBLIC + ACC_SUPER, classCName, null, "java/lang/Object", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {   // classCName main method
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitIntInsn(BIPUSH, 1);
        mv.visitTypeInsn(ANEWARRAY, arrayDim); // Test ANEWARRAY bytecode with various dimensions
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 6
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate bytecode for SystemModules::methodResoultions method
 */
private void genModuleResolutionsMethod() {
    MethodVisitor mresmv =
        cw.visitMethod(ACC_PUBLIC+ACC_STATIC,
                       "moduleResolutions",
                       "()" + MODULE_RESOLUTIONS_ARRAY_SIGNATURE,
                       "()" + MODULE_RESOLUTIONS_ARRAY_SIGNATURE,
                       null);
    mresmv.visitCode();
    pushInt(mresmv, moduleInfos.size());
    mresmv.visitTypeInsn(ANEWARRAY, MODULE_RESOLUTION_CLASSNAME);
    mresmv.visitVarInsn(ASTORE, 0);

    for (int index=0; index < moduleInfos.size(); index++) {
        ModuleInfo minfo = moduleInfos.get(index);
        if (minfo.moduleResolution() != null) {
            mresmv.visitVarInsn(ALOAD, 0);
            pushInt(mresmv, index);
            mresmv.visitTypeInsn(NEW, MODULE_RESOLUTION_CLASSNAME);
            mresmv.visitInsn(DUP);
            mresmv.visitLdcInsn(minfo.moduleResolution().value());
            mresmv.visitMethodInsn(INVOKESPECIAL,
                                   MODULE_RESOLUTION_CLASSNAME,
                                   "<init>",
                                   "(I)V", false);
            mresmv.visitInsn(AASTORE);
        }
    }
    mresmv.visitVarInsn(ALOAD, 0);
    mresmv.visitInsn(ARETURN);
    mresmv.visitMaxs(0, 0);
    mresmv.visitEnd();
}
 
Example 7
Source File: AddressVarHandleGenerator.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
void addCarrierAccessor(BinderClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_FINAL, "carrier", "()Ljava/lang/Class;", null, null);
    mv.visitCode();
    mv.visitLdcInsn(cw.makeConstantPoolPatch(carrier));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Class.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 8
Source File: TestConcreteClassWithAbstractMethod.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static byte[] dumpT3() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC + ACC_SUPER, "p1/T3", null, "p1/T2", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T2", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("p1/T3.m()");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
        mv.visitIntInsn(BIPUSH, 2);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "p1/T3");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T3", "<init>", "()V", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "p1/T2", "m", "()I", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 9
Source File: ArrayType.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type newarray(final MethodVisitor method) {
    method.visitTypeInsn(ANEWARRAY, getElementType().getInternalName());
    return this;
}
 
Example 10
Source File: TestConcreteClassWithAbstractMethod.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static byte[] dumpT3() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC + ACC_SUPER, "p1/T3", null, "p1/T2", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T2", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("p1/T3.m()");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
        mv.visitIntInsn(BIPUSH, 2);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "p1/T3");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T3", "<init>", "()V", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "p1/T2", "m", "()I", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 11
Source File: ObjectType.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type convert(final MethodVisitor method, final Type to) {
    final boolean toString = to.isString();
    if (!toString) {
        if (to.isArray()) {
            final Type elemType = ((ArrayType)to).getElementType();

            //note that if this an array, things won't work. see {link @ArrayType} subclass.
            //we also have the unpleasant case of NativeArray which looks like an Object, but is
            //an array to the type system. This is treated specially at the known load points

            if (elemType.isString()) {
                method.visitTypeInsn(CHECKCAST, CompilerConstants.className(String[].class));
            } else if (elemType.isNumber()) {
                method.visitTypeInsn(CHECKCAST, CompilerConstants.className(double[].class));
            } else if (elemType.isLong()) {
                method.visitTypeInsn(CHECKCAST, CompilerConstants.className(long[].class));
            } else if (elemType.isInteger()) {
                method.visitTypeInsn(CHECKCAST, CompilerConstants.className(int[].class));
            } else {
                method.visitTypeInsn(CHECKCAST, CompilerConstants.className(Object[].class));
            }
            return to;
        } else if (to.isObject()) {
            final Class<?> toClass = to.getTypeClass();
            if(!toClass.isAssignableFrom(getTypeClass())) {
                method.visitTypeInsn(CHECKCAST, CompilerConstants.className(toClass));
            }
            return to;
        }
    } else if (isString()) {
        return to;
    }

    if (to.isInteger()) {
        invokestatic(method, JSType.TO_INT32);
    } else if (to.isNumber()) {
        invokestatic(method, JSType.TO_NUMBER);
    } else if (to.isLong()) {
        invokestatic(method, JSType.TO_LONG);
    } else if (to.isBoolean()) {
        invokestatic(method, JSType.TO_BOOLEAN);
    } else if (to.isString()) {
        invokestatic(method, JSType.TO_PRIMITIVE_TO_STRING);
    } else if (to.isCharSequence()) {
        invokestatic(method, JSType.TO_PRIMITIVE_TO_CHARSEQUENCE);
    } else {
        throw new UnsupportedOperationException("Illegal conversion " + this + " -> " + to + " " + isString() + " " + toString);
    }

    return to;
}
 
Example 12
Source File: ArrayType.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type newarray(final MethodVisitor method) {
    method.visitTypeInsn(ANEWARRAY, getElementType().getInternalName());
    return this;
}
 
Example 13
Source File: TypeInsnNode.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    mv.visitTypeInsn(opcode, desc);
    acceptAnnotations(mv);
}
 
Example 14
Source File: TypeInsnNode.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    mv.visitTypeInsn(opcode, desc);
}
 
Example 15
Source File: TypeInsnNode.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    mv.visitTypeInsn(opcode, desc);
    acceptAnnotations(mv);
}
 
Example 16
Source File: TestConcreteClassWithAbstractMethod.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static byte[] dumpT3() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC + ACC_SUPER, "p1/T3", null, "p1/T2", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T2", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("p1/T3.m()");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
        mv.visitIntInsn(BIPUSH, 2);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "p1/T3");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T3", "<init>", "()V", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "p1/T2", "m", "()I", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 17
Source File: ArrayType.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type newarray(final MethodVisitor method) {
    method.visitTypeInsn(ANEWARRAY, getElementType().getInternalName());
    return this;
}
 
Example 18
Source File: ProxyGenerator.java    From Bytecoder with 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 19
Source File: TestConcreteClassWithAbstractMethod.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static byte[] dumpT3() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC + ACC_SUPER, "p1/T3", null, "p1/T2", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T2", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("p1/T3.m()");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
        mv.visitIntInsn(BIPUSH, 2);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, "p1/T3");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T3", "<init>", "()V", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "p1/T2", "m", "()I", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 20
Source File: TypeInsnNode.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void accept(final MethodVisitor mv) {
    mv.visitTypeInsn(opcode, desc);
    acceptAnnotations(mv);
}