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

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitLdcInsn() . 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
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 2
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 3
/**
 * The following method is copied from
 * org.objectweb.asm.commons.InstructionAdapter. Part of ASM: a very small
 * and fast Java bytecode manipulation framework.
 * Copyright (c) 2000-2005 INRIA, France Telecom All rights reserved.
 */
private static void iconst(MethodVisitor mv, final int cst) {
    if (cst >= -1 && cst <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + cst);
    } else if (cst >= Byte.MIN_VALUE && cst <= Byte.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, cst);
    } else if (cst >= Short.MIN_VALUE && cst <= Short.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, cst);
    } else {
        mv.visitLdcInsn(cst);
    }
}
 
Example 4
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 5
public static void main(String[] args) throws NoSuchMethodException,SecurityException,
        IllegalAccessException,IllegalArgumentException,InvocationTargetException{
    ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_7,ACC_PUBLIC,"Example",null,"java/lang/Object",null);
    MethodVisitor mw=cw.visitMethod(ACC_PUBLIC,"<init>","()V",null,null);
    mw.visitVarInsn(ALOAD,0);
    mw.visitMethodInsn(INVOKESPECIAL,"java/lang/Object","<init>","()V");
    mw.visitInsn(RETURN);
    mw.visitMaxs(0,0);
    mw.visitEnd();
    mw=cw.visitMethod(ACC_PUBLIC+ACC_STATIC,"main","([Ljava/lang/String;)V",null,null);
    mw.visitFieldInsn(GETSTATIC,"java/lang/System","out","Ljava/io/PrintStream;");
    mw.visitLdcInsn("Hello world!");
    mw.visitMethodInsn(INVOKEVIRTUAL,"java/io/PrintStream","println","(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    mw.visitMaxs(0,0);
    mw.visitEnd();
    byte[] code=cw.toByteArray();
    for(int i=0;i<1;i++){
        ClassLoader loader=UnloadClass.class.getClassLoader();
        Method m=ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
        m.setAccessible(true);
        m.invoke(loader,"Example",code,0,code.length);
        m.setAccessible(false);
        System.gc();
    }

}
 
Example 6
void initField(MethodVisitor mv) {
    if (hasDefaultValue) {
        return; // Nothing to do
    }
    if (!isStatic()) {
        mv.visitVarInsn(ALOAD, 0);
    }
    mv.visitLdcInsn(type.value);
    mv.visitFieldInsn((isStatic() ? PUTSTATIC : PUTFIELD), className, FIELD_NAME, fieldDesc);
}
 
Example 7
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 8
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 9
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 10
/**
 * 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 11
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 12
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_INT);
    return BOOLEAN;
}
 
Example 13
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_INT);
    return BOOLEAN;
}
 
Example 14
Source Project: openjdk-8   File: IntType.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(ObjectClassGenerator.UNDEFINED_INT);
    return INT;
}
 
Example 15
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_LONG);
    return LONG;
}
 
Example 16
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_LONG);
    return LONG;
}
 
Example 17
/**
 * 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
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_DOUBLE);
    return NUMBER;
}
 
Example 19
Source Project: totallylazy   File: Proxy.java    License: Apache License 2.0 4 votes vote down vote up
private static void loadMethod(MethodVisitor mv, String superClass, String methodName, String methodDescriptor) {
    mv.visitLdcInsn(superClass);
    mv.visitLdcInsn(methodName);
    mv.visitLdcInsn(methodDescriptor);
    mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/totallylazy/reflection/Methods", "method", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/reflect/Method;", false);
}
 
Example 20
/**
 * Returns the bytes for a class with name d_name (presumably "D" in some
 * package), extending some class with name sub_what, implementing p.I,
 * and defining two methods m() and m(11args) with access method_acc.
 *
 * @param d_name      Name of class that is defined
 * @param sub_what    Name of class that it extends
 * @param method_acc  Accessibility of method(s) m in defined class.
 * @return
 * @throws Exception
 */
public static byte[] bytesForSomeDsubSomethingSomeAccess
        (String d_name, String sub_what, int method_acc)
        throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    String[] interfaces = {"p/I"};

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    // int m() {return 3;}
    {
        mv = cw.visitMethod(method_acc, "m", "()I", null, null);
        mv.visitCode();
        mv.visitLdcInsn(new Integer(3));
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    // int m(11args) {return 3;}
    {
        mv = cw.visitMethod(method_acc, "m", "(BCSIJ"
                + "Ljava/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 Integer(3));
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}