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

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitCode() . 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: TestAMEnotNPE.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 2
Source File: AddressVarHandleGenerator.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
void addConstructor(BinderClassWriter cw) {
    MethodType constrType = MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class, long[].class);
    MethodVisitor mv = cw.visitMethod(0, "<init>", constrType.toMethodDescriptorString(), null, null);
    mv.visitCode();
    //super call
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(VarForm.class));
    mv.visitVarInsn(ILOAD, 2);
    mv.visitVarInsn(LLOAD, 3);
    mv.visitVarInsn(LLOAD, 5);
    mv.visitVarInsn(LLOAD, 7);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(BASE_CLASS), "<init>",
            MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class).toMethodDescriptorString(), false);
    //init dimensions
    for (int i = 0 ; i < dimensions ; i++) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 9);
        mv.visitLdcInsn(i);
        mv.visitInsn(LALOAD);
        mv.visitFieldInsn(PUTFIELD, implClassName, "dim" + i, "J");
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 3
Source File: ScriptClassInstrumentor.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void emitStaticInitializer() {
    final String className = scriptClassInfo.getJavaName();
    if (! staticInitFound) {
        // no user written <clinit> and so create one
        final MethodVisitor mv = ClassGenerator.makeStaticInitializer(this);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(Short.MAX_VALUE, 0);
        mv.visitEnd();
    }
    // Now generate $clinit$
    final MethodGenerator mi = ClassGenerator.makeStaticInitializer(this, $CLINIT$);
    ClassGenerator.emitStaticInitPrefix(mi, className, memberCount);
    if (memberCount > 0) {
        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
            if (memInfo.isInstanceProperty() || memInfo.isInstanceFunction()) {
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo);
            } else if (memInfo.isInstanceGetter()) {
                final MemberInfo setter = scriptClassInfo.findSetter(memInfo);
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo, setter);
            }
        }
    }
    ClassGenerator.emitStaticInitSuffix(mi, className);
}
 
Example 4
Source File: TestAMEnotNPE.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 5
Source File: ScriptClassInstrumentor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void emitStaticInitializer() {
    final String className = scriptClassInfo.getJavaName();
    if (! staticInitFound) {
        // no user written <clinit> and so create one
        final MethodVisitor mv = ClassGenerator.makeStaticInitializer(this);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(Short.MAX_VALUE, 0);
        mv.visitEnd();
    }
    // Now generate $clinit$
    final MethodGenerator mi = ClassGenerator.makeStaticInitializer(this, $CLINIT$);
    ClassGenerator.emitStaticInitPrefix(mi, className, memberCount);
    if (memberCount > 0) {
        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
            if (memInfo.isInstanceProperty() || memInfo.isInstanceFunction()) {
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo);
            } else if (memInfo.isInstanceGetter()) {
                final MemberInfo setter = scriptClassInfo.findSetter(memInfo);
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo, setter);
            }
        }
    }
    ClassGenerator.emitStaticInitSuffix(mi, className);
}
 
Example 6
Source File: UnsupportedClassFileVersion.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void writeClassFile() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", 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();
    cw.visitEnd();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
Example 7
Source File: ScriptClassInstrumentor.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void emitStaticInitializer() {
    final String className = scriptClassInfo.getJavaName();
    if (! staticInitFound) {
        // no user written <clinit> and so create one
        final MethodVisitor mv = ClassGenerator.makeStaticInitializer(this);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(Short.MAX_VALUE, 0);
        mv.visitEnd();
    }
    // Now generate $clinit$
    final MethodGenerator mi = ClassGenerator.makeStaticInitializer(this, $CLINIT$);
    ClassGenerator.emitStaticInitPrefix(mi, className, memberCount);
    if (memberCount > 0) {
        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
            if (memInfo.isInstanceProperty() || memInfo.isInstanceFunction()) {
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo);
            } else if (memInfo.isInstanceGetter()) {
                final MemberInfo setter = scriptClassInfo.findSetter(memInfo);
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo, setter);
            }
        }
    }
    ClassGenerator.emitStaticInitSuffix(mi, className);
}
 
Example 8
Source File: TestConcreteClassWithAbstractMethod.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static byte[] dumpT2() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T2", null, "p1/T1", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T1", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 9
Source File: TestAMEnotNPE.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 10
Source File: ScriptClassInstrumentor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void emitStaticInitializer() {
    final String className = scriptClassInfo.getJavaName();
    if (! staticInitFound) {
        // no user written <clinit> and so create one
        final MethodVisitor mv = ClassGenerator.makeStaticInitializer(this);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(Short.MAX_VALUE, 0);
        mv.visitEnd();
    }
    // Now generate $clinit$
    final MethodGenerator mi = ClassGenerator.makeStaticInitializer(this, $CLINIT$);
    ClassGenerator.emitStaticInitPrefix(mi, className, memberCount);
    if (memberCount > 0) {
        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
            if (memInfo.isInstanceProperty() || memInfo.isInstanceFunction()) {
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo);
            } else if (memInfo.isInstanceGetter()) {
                final MemberInfo setter = scriptClassInfo.findSetter(memInfo);
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo, setter);
            }
        }
    }
    ClassGenerator.emitStaticInitSuffix(mi, className);
}
 
Example 11
Source File: RedefineRunningMethodsWithResolutionErrors.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] loadC(boolean redefine) {
    ClassWriter cw = new ClassWriter(0);

    cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);
    {
        MethodVisitor mv;

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()V", null, null);
        mv.visitCode();

        // First time we run we will:
        // 1) Cache resolution errors
        // 2) Redefine the class / method
        // 3) Try to read the resolution errors that were cached
        //
        // The redefined method will never run, throw error to be sure
        if (redefine) {
            createThrowRuntimeExceptionCode(mv, "The redefined method was called");
        } else {
            createMethodBody(mv);
        }
        mv.visitMaxs(3, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 12
Source File: TestPrivateInterfaceMethodReflect.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    switch (name) {
        case INTERFACE_NAME:
            cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null);
            {
                mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(EXPECTED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        case CLASS_NAME:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME});
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        default:
            break;
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 13
Source File: OverriderMsg.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void dump_Overrider () throws Exception {

        ClassWriter cw = new ClassWriter(0);
        MethodVisitor mv;
        cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "Overrider", null, "HasFinal", null);

        {
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "HasFinal", "<init>", "()V");
            mv.visitInsn(RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_PUBLIC, "m", "(Ljava/lang/String;)V", null, null);
            mv.visitCode();
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 2);
            mv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
            mv.visitCode();
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 1);
            mv.visitEnd();
        }
        cw.visitEnd();

        try (FileOutputStream fos = new FileOutputStream(new File("Overrider.class"))) {
             fos.write(cw.toByteArray());
        }
    }
 
Example 14
Source File: TestPrivateInterfaceMethodReflect.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    switch (name) {
        case INTERFACE_NAME:
            cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null);
            {
                mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(EXPECTED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        case CLASS_NAME:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME});
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        default:
            break;
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 15
Source File: TestOSRWithNonEmptyStack.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void generateTestMethod(ClassWriter classWriter) {
    MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC,
            TestOSRWithNonEmptyStack.METHOD_NAME, "()V", null, null);
    Label osrEntryPoint = new Label();

    mv.visitCode();
    // Push 'this' into stack before OSR entry point to bail out compilation
    mv.visitVarInsn(ALOAD, 0);
    // Setup loop counter
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 1);
    // Begin loop
    mv.visitLabel(osrEntryPoint);
    // Increment loop counter
    mv.visitVarInsn(ILOAD, 1);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IADD);
    // Duplicate it for loop condition check
    mv.visitInsn(DUP);
    mv.visitVarInsn(ISTORE, 1);
    // Check loop condition
    mv.visitLdcInsn(TestOSRWithNonEmptyStack.ITERATIONS);
    mv.visitJumpInsn(IF_ICMPLT, osrEntryPoint);
    // Pop 'this'.
    mv.visitInsn(POP);
    mv.visitInsn(RETURN);

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 16
Source File: TestMultiANewArray.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void writeClassFile(int cfv) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(cfv, ACC_PUBLIC + ACC_SUPER, "ClassFile", 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();

    mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
    mv.visitCode();
    mv.visitInsn(ICONST_1);
    mv.visitInsn(ICONST_2);
    mv.visitMultiANewArrayInsn("[I", 2);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
Example 17
Source File: TestAMEnotNPE.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The bytecodes for a class p/T defining a methods test() and test(11args)
 * that contain an invokeExact of a particular methodHandle, I.m.
 *
 * Test will be passed values that may imperfectly implement I,
 * and thus may in turn throw exceptions.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForT() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/T", 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");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    // static int test(I)
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "(Lp/I;)I", null, null);
        mv.visitCode();
        mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "p/I", "m", "()I"));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
                "invokeExact", "(Lp/I;)I");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    // static int test(I,11args)
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "(Lp/I;BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I", null, null);
        mv.visitCode();
        mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "p/I", "m", "(BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I"));
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitVarInsn(ILOAD, 2);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitVarInsn(ILOAD, 4);
        mv.visitVarInsn(LLOAD, 5);
        mv.visitVarInsn(ALOAD, 7);
        mv.visitVarInsn(ALOAD, 8);
        mv.visitVarInsn(ALOAD, 9);
        mv.visitVarInsn(ALOAD, 10);
        mv.visitVarInsn(ALOAD, 11);
        mv.visitVarInsn(ALOAD, 12);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
                "invokeExact", "(Lp/I;BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 18
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 19
Source File: InvokeDynamicPatcher.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodVisitor visitMethod(final int access, final String name,
        final String desc, final String signature,
        final String[] exceptions) {
    /* a code generate looks like
     *  0: aload_0
     *  1: ldc           #125  // int 1
     *  3: ldc2_w        #126  // long 2l
     *  6: ldc           #128  // float 3.0f
     *  8: ldc2_w        #129  // double 4.0d
     * 11: ldc           #132  // String 5
     * 13: aload_0
     * 14: getfield      #135  // Field nativeCallee:Z
     * 17: ifeq          28
     * 20: invokedynamic #181,  0            // InvokeDynamic #1:calleeNative:(Lcompiler/calls/common/InvokeDynamic;IJFDLjava/lang/String;)Z
     * 25: goto          33
     * 28: invokedynamic #183,  0            // InvokeDynamic #1:callee:(Lcompiler/calls/common/InvokeDynamic;IJFDLjava/lang/String;)Z
     * 33: ldc           #185                // String Call insuccessfull
     * 35: invokestatic  #191                // Method jdk/test/lib/Asserts.assertTrue:(ZLjava/lang/String;)V
     * 38: return
     *
     * or, using java-like pseudo-code
     * if (this.nativeCallee == false) {
     *     invokedynamic-call-return-value = invokedynamic-of-callee
     * } else {
     *     invokedynamic-call-return-value = invokedynamic-of-nativeCallee
     * }
     * Asserts.assertTrue(invokedynamic-call-return-value, error-message);
     * return;
     */
    if (name.equals(CALLER_METHOD_NAME)) {
        MethodVisitor mv = cv.visitMethod(access, name, desc,
                signature, exceptions);
        Label nonNativeLabel = new Label();
        Label checkLabel = new Label();
        MethodType mtype = MethodType.methodType(CallSite.class,
                MethodHandles.Lookup.class, String.class, MethodType.class);
        Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, CLASS,
                BOOTSTRAP_METHOD_NAME, mtype.toMethodDescriptorString());
        mv.visitCode();
        // push callee parameters onto stack
        mv.visitVarInsn(Opcodes.ALOAD, 0);//push "this"
        mv.visitLdcInsn(1);
        mv.visitLdcInsn(2L);
        mv.visitLdcInsn(3.0f);
        mv.visitLdcInsn(4.0d);
        mv.visitLdcInsn("5");
        // params loaded. let's decide what method to call
        mv.visitVarInsn(Opcodes.ALOAD, 0); // push "this"
        // get nativeCallee field
        mv.visitFieldInsn(Opcodes.GETFIELD, CLASS, CALL_NATIVE_FIELD,
                CALL_NATIVE_FIELD_DESC);
        // if nativeCallee == false goto nonNativeLabel
        mv.visitJumpInsn(Opcodes.IFEQ, nonNativeLabel);
        // invokedynamic nativeCalleeMethod using bootstrap method
        mv.visitInvokeDynamicInsn(NATIVE_CALLEE_METHOD_NAME,
                CALLEE_METHOD_DESC, bootstrap);
        // goto checkLabel
        mv.visitJumpInsn(Opcodes.GOTO, checkLabel);
        // label: nonNativeLabel
        mv.visitLabel(nonNativeLabel);
        // invokedynamic calleeMethod using bootstrap method
        mv.visitInvokeDynamicInsn(CALLEE_METHOD_NAME, CALLEE_METHOD_DESC,
                bootstrap);
        mv.visitLabel(checkLabel);
        mv.visitLdcInsn(CallsBase.CALL_ERR_MSG);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ASSERTS_CLASS,
                ASSERTTRUE_METHOD_NAME, ASSERTTRUE_METHOD_DESC, false);
        // label: return
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        return null;
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}
 
Example 20
Source File: FinalStatic.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    switch (name) {
       case CLASS_NAME_A:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME_A, 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");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();

                mv = cw.visitMethod(ACC_FINAL | ACC_STATIC, "m", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(FAILED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        case CLASS_NAME_B:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME_B, null, CLASS_NAME_A, null);
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, CLASS_NAME_A, "<init>", "()V");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();

                mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(EXPECTED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();

            }
            break;
        default:
            break;
    }
    cw.visitEnd();

    return cw.toByteArray();
}