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

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitVarInsn() . 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
Source Project: totallylazy   File: Proxy.java    License: 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
void addStridesAccessor(BinderClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_FINAL, "strides", "()[J", null, null);
    mv.visitCode();
    iConstInsn(mv, dimensions);
    mv.visitIntInsn(NEWARRAY, T_LONG);

    for (int i = 0 ; i < dimensions ; i++) {
        mv.visitInsn(DUP);
        iConstInsn(mv, i);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, "dim" + i, "J");
        mv.visitInsn(LASTORE);
    }

    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 3
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 4
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 5
/** Produces byte code for a class that is used as an injected invoker. */
private static byte[] generateInvokerTemplate() {
    ClassWriter cw = new ClassWriter(0);

    // private static class InjectedInvoker {
    //     @Hidden
    //     static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
    //        return vamh.invokeExact(args);
    //     }
    // }
    cw.visit(52, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);

    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
                  "(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
                  null, null);

    // Suppress invoker method in stack traces.
    AnnotationVisitor av0 = mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
    av0.visitEnd();

    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
                       "([Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 6
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 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
Source Project: Java8CN   File: BoundMethodHandle.java    License: Apache License 2.0 5 votes vote down vote up
private static void emitPushFields(String types, String className, MethodVisitor mv) {
    for (int i = 0; i < types.length(); ++i) {
        char tc = types.charAt(i);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc));
    }
}
 
Example 9
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 10
/**
 * Generate a class file with the given class name. The class implements Runnable
 * with a run method to invokestatic the given targetClass/targetMethod.
 */
byte[] generateRunner(String className,
                      String targetClass,
                      String targetMethod) throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_9,
            ACC_PUBLIC + ACC_SUPER,
            className.replace(".", "/"),
            null,
            "java/lang/Object",
            new String[] { "java/lang/Runnable" });

    // <init>
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    // run()
    String tc = targetClass.replace(".", "/");
    mv = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
    mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 11
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 12
private static void emitPushFields(String types, String className, MethodVisitor mv) {
    for (int i = 0; i < types.length(); ++i) {
        char tc = types.charAt(i);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc));
    }
}
 
Example 13
private static void emitPushFields(String types, String className, MethodVisitor mv) {
    for (int i = 0; i < types.length(); ++i) {
        char tc = types.charAt(i);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc));
    }
}
 
Example 14
private static void buildDurationMethod() {
    MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, METHOD_DURATION.getName(), METHOD_DURATION.getDescriptor(), null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.LLOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(EventHandler.class), METHOD_DURATION.getName(), METHOD_DURATION.getDescriptor(), false);
    mv.visitInsn(Opcodes.LRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 15
@Override
public void accept(final MethodVisitor mv) {
    mv.visitVarInsn(opcode, var);
    acceptAnnotations(mv);
}
 
Example 16
@Override
public void accept(final MethodVisitor mv) {
    mv.visitVarInsn(opcode, var);
    acceptAnnotations(mv);
}
 
Example 17
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 18
@Override
public void accept(final MethodVisitor mv) {
    mv.visitVarInsn(opcode, var);
    acceptAnnotations(mv);
}
 
Example 19
/**
 * 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();
}
 
Example 20
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();
}