Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

List of usage examples for org.objectweb.asm MethodVisitor visitMethodInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitMethodInsn.

Prototype

@Deprecated
public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.sun.tdk.jcov.instrument.DeferringMethodClassAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (!params.isInstrumentSynthetic() && (access & ACC_SYNTHETIC) != 0) {
        return super.visitMethod(access, name, desc, signature, exceptions);
        //            return null;
    }//  w w w. ja  v a 2  s  .  c  om
    MethodVisitor mv = visitMethodCoverage(access, name, desc, signature, exceptions);

    if ("<clinit>".equals(name) && !params.isDynamicCollect()
            && (k.getPackageName().startsWith("java/lang/"))) {
        mv = new MethodVisitor(Opcodes.ASM4, mv) {
            public void visitCode() {
                mv.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/Collect", "init", "()V");
                super.visitCode();
            }
        };
    }

    if (params.isCallerFilterOn() && params.isCallerFilterAccept(k.getFullname())
            && !params.isDynamicCollect()) {

        if (name.equals("<clinit>")) {
            int id = (name + desc).hashCode();
            mv.visitLdcInsn(id);
            mv.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/CollectDetect", "setExpected", "(I)V");
        }

    }

    if (params.isInnerInvacationsOff() && Utils.isAdvanceStaticInstrAllowed(k.getFullname(), name)) {

        if (name.equals("<clinit>")) {
            mv.visitMethodInsn(INVOKESTATIC, "com/sun/tdk/jcov/runtime/CollectDetect", "enterClinit", "()V");
        }

    }

    // System.out.println("Seeing " + k.fullname + "." + name);
    if (params.isDataSaveFilterAccept(k.getFullname(), name, true)) {
        mv = new SavePointsMethodAdapter(mv, true);
    }

    if (params.isDataSaveFilterAccept(k.getFullname(), name, false)) {
        mv = new SavePointsMethodAdapter(mv, false);
    }
    mv = new MethodVisitor(Opcodes.ASM4, mv) {
        @Override
        public void visitLocalVariable(String arg0, String arg1, String arg2, Label arg3, Label arg4,
                int arg5) {
            //super.visitLocalVariable(arg0, arg1, arg2, arg3, arg4, arg5);
        }
    };
    if (params.isDynamicCollect()) {
        mv = new InvokeMethodAdapter(mv, k.getFullname(), params);
    } else {
        mv = new StaticInvokeMethodAdapter(mv, k.getFullname(), name, access, params);
    }
    return mv;
}

From source file:com.taobao.asm.AsmExample.java

License:Apache License

public static void main(String[] args) throws IOException, IllegalArgumentException, SecurityException,
        IllegalAccessException, InvocationTargetException {

    ClassReader cr = new ClassReader(Foo.class.getName());
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new MethodChangeClassAdapter(cw);
    cr.accept(cv, Opcodes.ASM4);/*from w  ww .  ja va 2s .c o m*/

    //  
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "add", "([Ljava/lang/String;)V", null, null);
    // pushes the 'out' field (of type PrintStream) of the System class  
    mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    // pushes the "Hello World!" String constant  
    mw.visitLdcInsn("this is add method print!");
    // invokes the 'println' method (defined in the PrintStream class)  
    mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of two stack elements and two local  
    // variables  
    mw.visitMaxs(0, 0);
    mw.visitEnd();

    // gets the bytecode of the Example class, and loads it dynamically  
    byte[] code = cw.toByteArray();

    AsmExample loader = new AsmExample();
    Class<?> exampleClass = loader.defineClass(Foo.class.getName(), code, 0, code.length);

    for (Method method : exampleClass.getMethods()) {
        System.out.println(method);
    }

    System.out.println("*************");

    // uses the dynamically generated class to print 'Helloworld'  
    exampleClass.getMethods()[0].invoke(null); //changeMethodContent

    System.out.println("*************");

    exampleClass.getMethods()[1].invoke(null); //execute,??

    // gets the bytecode of the Example class, and loads it dynamically  

    FileOutputStream fos = new FileOutputStream("e:\\logs\\Example.class");
    fos.write(code);
    fos.close();
}

From source file:com.taobao.asm.AsmTest.java

License:Apache License

@Test
public void test() throws Throwable {

    ClassReader cr = new ClassReader(Config.class.getName());
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new MethodChangeClassAdapter(cw);
    cr.accept(cv, Opcodes.ASM5);/*from   w  w w .j  av  a 2s . co m*/

    //
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "add", "([Ljava/lang/String;)V", null, null);

    // pushes the 'out' field (of type PrintStream) of the System class
    mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    // pushes the "Hello World!" String constant
    mw.visitLdcInsn("this is add method print!");
    // invokes the 'println' method (defined in the PrintStream class)
    mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of two stack elements and two local
    // variables
    mw.visitMaxs(0, 0);
    mw.visitEnd();

    //Type.getDescriptor(AdviceFlowOuterHolder.class)
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "age", Type.INT_TYPE.toString(), null,
            1);
    fv.visitEnd();

    FieldVisitor fv2 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name2",
            Type.getDescriptor(String.class), null, "name2");
    fv2.visitEnd();

    ModifyClassVisiter cv2 = new ModifyClassVisiter(Opcodes.ASM5);
    cv2.addRemoveField("name");
    cr.accept(cv2, Opcodes.ASM5);

    FieldVisitor fv3 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name",
            Type.getDescriptor(String.class), null, "name");
    fv3.visitEnd();

    byte[] code = cw.toByteArray();
    File file = new File("Config.class");
    System.out.println(file.getAbsolutePath());
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(code);
    fos.close();

}

From source file:com.toolazydogs.aunit.internal.LexerFactory.java

License:Apache License

private Class<L> loadClass() {
    final String parent = lexerClass.getName().replace('.', '/');
    final String name = "com/toolazydogs/aunit/asm/" + parent;

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*from   ww  w .  j a v  a 2  s  . c  o m*/
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name, null, parent,
            new String[] { "com/toolazydogs/aunit/internal/LexerWrapper" });

    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "errors", "Ljava/util/List;",
                "Ljava/util/List<Ljava/lang/String;>;", null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "failOnError", "Z", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>", "()V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Lorg/antlr/runtime/CharStream;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>", "(Lorg/antlr/runtime/CharStream;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>",
                "(Lorg/antlr/runtime/CharStream;Lorg/antlr/runtime/RecognizerSharedState;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>",
                "(Lorg/antlr/runtime/CharStream;Lorg/antlr/runtime/RecognizerSharedState;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "emitErrorMessage", "(Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getErrors", "()Ljava/util/List;",
                "()Ljava/util/List<Ljava/lang/String;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "errors", "Ljava/util/List;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "isFailOnError", "()Z", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "failOnError", "Z");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "setFailOnError", "(Z)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] b = cw.toByteArray();

    AunitClassloader ac = new AunitClassloader(lexerClass.getClassLoader());
    return ac.defineClass(name.replace('/', '.'), b);
}

From source file:com.toolazydogs.aunit.internal.ParserFactory.java

License:Apache License

private Class<P> loadClass() {
    final String parent = parserClass.getName().replace('.', '/');
    final String name = "com/toolazydogs/aunit/asm/" + parent;

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from  ww w .  j  a va2 s  . com
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name, null, parent,
            new String[] { "com/toolazydogs/aunit/internal/ParserWrapper" });

    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "errors", "Ljava/util/List;",
                "Ljava/util/List<Ljava/lang/String;>;", null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "failOnError", "Z", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Lorg/antlr/runtime/TokenStream;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>", "(Lorg/antlr/runtime/TokenStream;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>",
                "(Lorg/antlr/runtime/TokenStream;Lorg/antlr/runtime/RecognizerSharedState;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>",
                "(Lorg/antlr/runtime/TokenStream;Lorg/antlr/runtime/RecognizerSharedState;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "emitErrorMessage", "(Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getErrors", "()Ljava/util/List;",
                "()Ljava/util/List<Ljava/lang/String;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "errors", "Ljava/util/List;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "isFailOnError", "()Z", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "failOnError", "Z");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "setFailOnError", "(Z)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] b = cw.toByteArray();

    AunitClassloader ac = new AunitClassloader(parserClass.getClassLoader());
    return ac.defineClass(name.replace('/', '.'), b);
}

From source file:com.toolazydogs.aunit.internal.TreeParserFactory.java

License:Apache License

private Class<T> loadClass() {
    final String parent = treeParserClass.getName().replace('.', '/');
    final String name = "com/toolazydogs/aunit/asm/" + parent;

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/* www.j a v a  2s .c o  m*/
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name, null, parent,
            new String[] { "com/toolazydogs/aunit/internal/TreeParserWrapper" });

    {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "errors", "Ljava/util/List;",
                "Ljava/util/List<Ljava/lang/String;>;", null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "failOnError", "Z", null, null);
        fv.visitEnd();
    }

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Lorg/antlr/runtime/tree/TreeNodeStream;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>", "(Lorg/antlr/runtime/tree/TreeNodeStream;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>",
                "(Lorg/antlr/runtime/tree/TreeNodeStream;Lorg/antlr/runtime/RecognizerSharedState;)V", null,
                null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, parent, "<init>",
                "(Lorg/antlr/runtime/tree/TreeNodeStream;Lorg/antlr/runtime/RecognizerSharedState;)V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ICONST_1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "emitErrorMessage", "(Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "errors", "Ljava/util/List;");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getErrors", "()Ljava/util/List;",
                "()Ljava/util/List<Ljava/lang/String;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "errors", "Ljava/util/List;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "isFailOnError", "()Z", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "failOnError", "Z");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "setFailOnError", "(Z)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitFieldInsn(PUTFIELD, name, "failOnError", "Z");
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] b = cw.toByteArray();

    AunitClassloader ac = new AunitClassloader(treeParserClass.getClassLoader());
    return ac.defineClass(name.replace('/', '.'), b);
}

From source file:com.toolazydogs.maiden.agent.asm.IronClassVisitor.java

License:Apache License

public MethodVisitor visitMethod(int access, final String name, final String desc, String signature,
        String[] exceptions) {/*from   ww  w .  j  a v a  2  s . c  o  m*/
    LOGGER.entering(CLASS_NAME, "visitMethod", new Object[] { access, name, desc, signature, exceptions });

    boolean isNative = (access & ACC_NATIVE) != 0;
    boolean isSynchronized = (access & ACC_SYNCHRONIZED) != 0;
    boolean isStatic = (access & ACC_STATIC) != 0;
    MethodVisitor mv;

    /**
     * If native method prefix is supported we inject our own wrapper method.
     */
    if (nativeMethodPrefixSupported && isNative) {
        delegate.visitMethod(access, IronAgent.NATIVE_METHOD_PREFIX + name, desc, signature, exceptions);

        mv = delegate.visitMethod(access ^ (ACC_NATIVE | ACC_SYNCHRONIZED), name, desc, signature, exceptions);
    } else {
        mv = delegate.visitMethod((isSynchronized ? access ^ ACC_SYNCHRONIZED : access), name, desc, signature,
                exceptions);
    }

    mv = new WaitNotifyMethodVisitor(mv);

    BeginEndMethodVisitor bemv = new BeginEndMethodVisitor(mv, access, name, desc, signature, exceptions);

    if (isSynchronized) {
        LOGGER.finest("Method is synchronized");

        final Type classType = Type.getType("L" + clazz.replaceAll("\\.", "/") + ";.class");
        if (isStatic) {
            LOGGER.finest("Method is static");
            bemv.getListeners().add(new BeginEndMethodListener() {
                @Override
                public void begin(MethodVisitor visitor) {
                    push(visitor, line);
                    visitor.visitLdcInsn(classType);
                    visitor.visitMethodInsn(INVOKESTATIC, "com/toolazydogs/maiden/IronMaiden", "lockObject",
                            "(ILjava/lang/Object;)V");
                }

                @Override
                public void end(MethodVisitor visitor) {
                    push(visitor, line);
                    visitor.visitLdcInsn(classType);
                    visitor.visitMethodInsn(INVOKESTATIC, "com/toolazydogs/maiden/IronMaiden", "unlockObject",
                            "(ILjava/lang/Object;)V");
                }
            });
        } else {
            LOGGER.finest("Method is not static");
            bemv.getListeners().add(new BeginEndMethodListener() {
                @Override
                public void begin(MethodVisitor visitor) {
                    push(visitor, line);
                    visitor.visitVarInsn(ALOAD, 0);
                    visitor.visitMethodInsn(INVOKESTATIC, "com/toolazydogs/maiden/IronMaiden", "lockObject",
                            "(ILjava/lang/Object;)V");
                }

                @Override
                public void end(MethodVisitor visitor) {
                    push(visitor, line);
                    visitor.visitVarInsn(ALOAD, 0);
                    visitor.visitMethodInsn(INVOKESTATIC, "com/toolazydogs/maiden/IronMaiden", "unlockObject",
                            "(ILjava/lang/Object;)V");
                }
            });
        }
    }

    bemv.getListeners().add(new BeginEndMethodListener() {
        @Override
        public void begin(MethodVisitor visitor) {
            visitor.visitLdcInsn(clazz);
            visitor.visitLdcInsn(name);
            visitor.visitLdcInsn(desc);
            visitor.visitMethodInsn(INVOKESTATIC, "com/toolazydogs/maiden/IronMaiden", "push",
                    "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
        }

        @Override
        public void end(MethodVisitor visitor) {
            push(visitor, line);
            visitor.visitMethodInsn(INVOKESTATIC, "com/toolazydogs/maiden/IronMaiden", "pop", "(I)V");
        }
    });

    MethodVisitor result = new MonitorMethodVisitor(bemv);

    if (nativeMethodPrefixSupported && isNative) {
        MethodAdapter adapter = new MethodAdapter(result);
        adapter.visitCode();

        int args = 0;
        if (!isStatic)
            adapter.visitVarInsn(ALOAD, args++);
        for (Type param : Type.getArgumentTypes(desc)) {
            adapter.visitVarInsn(param.getOpcode(ILOAD), args);
            args += param.getSize();
        }
        adapter.visitMethodInsn((isStatic ? INVOKESTATIC : INVOKEVIRTUAL), clazz.replaceAll("\\.", "/"),
                IronAgent.NATIVE_METHOD_PREFIX + name, desc);

        Type returnType = Type.getReturnType(desc);
        adapter.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
        adapter.visitMaxs(0, 0);
        adapter.visitEnd();

        result = null;
    }

    LOGGER.exiting(CLASS_NAME, "visitMethod", result);

    return result;
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpArrayList(final TypeModel tm) {
    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from   w  ww  .  jav a2s .c  o m
    MethodVisitor mv;

    final Class interfaceClass = tm.type();
    final String name = interfaceClass.getName().replace('.', '/');

    cw.visit(
            V1_5, ACC_PUBLIC + ACC_SUPER, name + "ArrayList", "L" + collections + "impl/AbstractHugeArrayList<L"
                    + name + ";L" + name + "Allocation;L" + name + "Element;>;",
            collections + "impl/AbstractHugeArrayList", null);

    cw.visitSource(tm.type().getSimpleName() + "ArrayList.java", null);

    for (final FieldModel fm : tm.fields()) {
        if (fm instanceof Enum8FieldModel) {
            fv = cw.visitField(ACC_FINAL, fm.fieldName() + "FieldModel",
                    "L" + collections + "model/Enum8FieldModel;",
                    "L" + collections + "model/Enum8FieldModel<Ljava/lang/annotation/ElementType;>;", null);
            fv.visitEnd();
        } else if (fm instanceof Enumerated16FieldModel) {
            fv = cw.visitField(ACC_FINAL, fm.fieldName() + "FieldModel",
                    "L" + collections + "model/Enumerated16FieldModel;",
                    "L" + collections + "model/Enumerated16FieldModel<Ljava/lang/String;>;", null);
            fv.visitEnd();
        }
    }
    final List<FieldModel> fields = new ArrayList<FieldModel>();
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(L" + collections + "HugeArrayBuilder;)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(35, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, collections + "impl/AbstractHugeArrayList", "<init>",
                "(L" + collections + "HugeArrayBuilder;)V");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(29, l1);

        for (final FieldModel fm : tm.fields()) {
            if (fm instanceof Enum8FieldModel) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitTypeInsn(NEW, fm.bcModelType());
                mv.visitInsn(DUP);
                mv.visitLdcInsn(fm.fieldName());
                mv.visitIntInsn(BIPUSH, fm.fieldNumber());
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitMethodInsn(INVOKESTATIC, fm.bcFieldType(), "values", "()[" + fm.bcLFieldType());
                mv.visitMethodInsn(INVOKESPECIAL, fm.bcModelType(), "<init>",
                        "(Ljava/lang/String;ILjava/lang/Class;[Ljava/lang/Enum;)V");
                mv.visitFieldInsn(PUTFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, collections + "HugeArrayBuilder", "baseDirectory",
                        "()Ljava/lang/String;");
                mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "baseDirectory", "(Ljava/lang/String;)V");
                fields.add(fm);

            } else if (fm instanceof Enumerated16FieldModel) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitTypeInsn(NEW, fm.bcModelType());
                mv.visitInsn(DUP);
                mv.visitLdcInsn(fm.fieldName());
                mv.visitIntInsn(BIPUSH, fm.fieldNumber());
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitMethodInsn(INVOKESPECIAL, fm.bcModelType(), "<init>",
                        "(Ljava/lang/String;ILjava/lang/Class;)V");
                mv.visitFieldInsn(PUTFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel",
                        fm.bcLModelType());
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, collections + "HugeArrayBuilder", "baseDirectory",
                        "()Ljava/lang/String;");
                mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "baseDirectory", "(Ljava/lang/String;)V");
                fields.add(fm);
            }
        }

        mv.visitInsn(RETURN);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l4, 0);
        mv.visitLocalVariable("hab", "L" + collections + "HugeArrayBuilder;", null, l0, l4, 1);
        mv.visitMaxs(7, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "createAllocation",
                "(L" + collections + "impl/MappedFileChannel;)L" + name + "Allocation;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(43, l0);
        mv.visitTypeInsn(NEW, name + "Allocation");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "ArrayList", "allocationSize", "I");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, name + "Allocation", "<init>",
                "(IL" + collections + "impl/MappedFileChannel;)V");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("mfc", "L" + collections + "impl/MappedFileChannel;", null, l0, l1, 1);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "createElement", "(J)L" + name + "Element;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(48, l0);
        mv.visitTypeInsn(NEW, name + "Element");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(LLOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, name + "Element", "<init>",
                "(L" + collections + "impl/AbstractHugeArrayList;J)V");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("n", "J", null, l0, l1, 1);
        mv.visitMaxs(5, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "createImpl", "()L" + name + ";", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(53, l0);
        mv.visitTypeInsn(NEW, name + "Impl");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, name + "Impl", "<init>", "()V");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "compactStart", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(57, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactStart", "()V");
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(58, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "compactOnAllocation", "(L" + name + "Allocation;J)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(65, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitVarInsn(ALOAD, 1);
            mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_string", fm.bcLStoreType());
            mv.visitVarInsn(LLOAD, 2);
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactScan", "(" + fm.bcLStoreType() + "J)V");
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(66, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0);
        mv.visitLocalVariable("allocation", "L" + name + "Allocation;", null, l0, l2, 1);
        mv.visitLocalVariable("thisSize", "J", null, l0, l2, 2);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "compactEnd", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(69, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "compactEnd", "()V");
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(70, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l2, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(74, l0);
        for (final FieldModel fm : fields) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            mv.visitMethodInsn(INVOKEVIRTUAL, fm.bcModelType(), "clear", "()V");
        }
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(77, l3);
        mv.visitInsn(RETURN);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l4, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createImpl", "()Ljava/lang/Object;",
                null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createImpl", "()L" + name + ";");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createElement",
                "(J)L" + collections + "impl/AbstractHugeElement;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(LLOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createElement", "(J)L" + name + "Element;");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "J", null, l0, l1, 1);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "compactOnAllocation",
                "(L" + collections + "api/HugeAllocation;J)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, name + "Allocation");
        mv.visitVarInsn(LLOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "compactOnAllocation",
                "(L" + name + "Allocation;J)V");
        mv.visitInsn(RETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "L" + collections + "api/HugeAllocation;", null, l0, l1, 1);
        mv.visitLocalVariable("x1", "J", null, l0, l1, 2);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED + ACC_BRIDGE + ACC_SYNTHETIC, "createAllocation",
                "(L" + collections + "impl/MappedFileChannel;)L" + collections + "api/HugeAllocation;", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(29, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "ArrayList", "createAllocation",
                "(L" + collections + "impl/MappedFileChannel;)L" + name + "Allocation;");
        mv.visitInsn(ARETURN);
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + name + "ArrayList;", null, l0, l1, 0);
        mv.visitLocalVariable("x0", "L" + collections + "impl/MappedFileChannel;", null, l0, l1, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    return bytes;
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpAllocation(final TypeModel tm) {

    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//  ww w  .  j  av a 2 s  .c o  m
    MethodVisitor mv;

    final Class interfaceClass = tm.type();
    final String name = interfaceClass.getName().replace('.', '/');

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Allocation", null, "java/lang/Object",
            new String[] { collections + "api/HugeAllocation" });

    cw.visitSource(tm.type().getSimpleName() + "Allocation.java", null);

    for (final FieldModel fm : tm.fields()) {
        fv = cw.visitField(0, "m_" + fm.fieldName(), fm.bcLStoreType(), null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(IL" + collections + "impl/MappedFileChannel;)V", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(46, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

        for (final FieldModel fm : tm.fields()) {
            mv.visitVarInsn(ALOAD, 0);

            if (fm instanceof ObjectFieldModel) {
                mv.visitLdcInsn(Type.getType(fm.bcLFieldType()));
                mv.visitVarInsn(ILOAD, 1);
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "newArrayOfField",
                        "(Ljava/lang/Class;I)[Ljava/lang/Object;");
                mv.visitTypeInsn(CHECKCAST, fm.bcLStoreType());
            } else {
                mv.visitVarInsn(ILOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKESTATIC, fm.bcModelType(), "newArrayOfField",
                        "(IL" + collections + "impl/MappedFileChannel;)" + fm.bcLStoreType());
            }
            mv.visitFieldInsn(PUTFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
        }

        mv.visitInsn(RETURN);
        final Label l14 = new Label();
        mv.visitLabel(l14);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l14, 0);
        mv.visitLocalVariable("allocationSize", "I", null, l0, l14, 1);
        mv.visitLocalVariable("mfc", "L" + collections + "impl/MappedFileChannel;", null, l0, l14, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "clear", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(62, l0);
        for (final FieldModel fm : tm.fields()) {
            if (fm instanceof ObjectFieldModel) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
                mv.visitInsn(ACONST_NULL);
                mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "fill",
                        "([Ljava/lang/Object;Ljava/lang/Object;)V");
            }
        }
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(63, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l2, 0);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "destroy", "()V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(66, l0);
        for (final FieldModel fm : tm.fields()) {
            if (!fm.isBufferStore()) {
                continue;
            }
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
            mv.visitTypeInsn(CHECKCAST, "java/nio/Buffer");
            mv.visitMethodInsn(INVOKESTATIC, GenerateHugeArrays.class.getName().replace('.', '/'), "clean",
                    "(Ljava/nio/Buffer;)V");

        }
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(69, l3);
        mv.visitInsn(RETURN);
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l4, 0);
        mv.visitLocalVariable("m", "Ljava/lang/Object;", null, l3, l4, 1);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "finalize", "()V", null, new String[] { "java/lang/Throwable" });
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(76, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "finalize", "()V");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(77, l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, name + "Allocation", "destroy", "()V");
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLineNumber(78, l2);
        mv.visitInsn(RETURN);
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this", "L" + name + "Allocation;", null, l0, l3, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    return bytes;
}

From source file:com.weibo.lodil.mmap.impl.GenerateHugeArrays.java

License:Apache License

public static byte[] dumpElement(final TypeModel tm) {

    final ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/* w  w w.  j  av  a  2 s  . c o  m*/
    MethodVisitor mv;

    final String name = tm.bcType();

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name + "Element",
            "L" + collections + "impl/AbstractHugeElement<L" + name + "Allocation;>;L" + name
                    + ";Ljava/io/Externalizable;",
            collections + "impl/AbstractHugeElement", new String[] { name, "java/io/Externalizable" });

    cw.visitSource(tm.type().getSimpleName() + "Element.java", null);

    {
        fv = cw.visitField(0, "allocation", "L" + name + "Allocation;", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(L" + collections + "impl/AbstractHugeArrayList;J)V",
                "(L" + collections + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name
                        + "Element;>;J)V",
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(34, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(LLOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, collections + "impl/AbstractHugeElement", "<init>",
                "(L" + collections + "impl/AbstractHugeContainer;J)V");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(35, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0);
        mv.visitLocalVariable("list", "L" + collections + "impl/AbstractHugeArrayList;", "L" + collections
                + "impl/AbstractHugeArrayList<L" + name + ";L" + name + "Allocation;L" + name + "Element;>;",
                l0, l2, 1);
        mv.visitLocalVariable("n", "J", null, l0, l2, 2);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }
    for (final FieldModel fm : tm.fields()) {
        // /////// SETTER //////////

        int maxLocals = 2 + fm.bcFieldSize();

        mv = cw.visitMethod(ACC_PUBLIC, "set" + fm.titleFieldName(), "(" + fm.bcLFieldType() + ")V", null,
                null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(39, l0);

        int invoke = INVOKESTATIC;
        if (fm.virtualGetSet()) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "Element", "container",
                    "L" + collections + "impl/AbstractHugeContainer;");
            mv.visitTypeInsn(CHECKCAST, name + "ArrayList");
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            invoke = INVOKEVIRTUAL;
            maxLocals++;
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "allocation", "L" + name + "Allocation;");
        mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "offset", "I");
        mv.visitVarInsn(loadFor(fm.bcType()), 1);

        mv.visitMethodInsn(invoke, fm.bcModelType(), "set",
                "(" + fm.bcLStoreType() + "I" + fm.bcLSetType() + ")V");
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0);
        mv.visitLocalVariable("elementType", "Ljava/lang/annotation/ElementType;", null, l0, l2, 1);
        mv.visitMaxs(maxLocals, 1 + fm.bcFieldSize());
        mv.visitEnd();

        // /////// GETTER //////////

        mv = cw.visitMethod(ACC_PUBLIC, "get" + fm.titleFieldName(), "()" + fm.bcLFieldType(), null, null);
        mv.visitCode();
        final Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(144, l3);

        BCType bcType = fm.bcType();
        if (fm.virtualGetSet()) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, name + "Element", "container",
                    "L" + collections + "impl/AbstractHugeContainer;");
            mv.visitTypeInsn(CHECKCAST, name + "ArrayList");
            mv.visitFieldInsn(GETFIELD, name + "ArrayList", fm.fieldName() + "FieldModel", fm.bcLModelType());
            bcType = BCType.Reference;
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "allocation", "L" + name + "Allocation;");
        mv.visitFieldInsn(GETFIELD, name + "Allocation", "m_" + fm.fieldName(), fm.bcLStoreType());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "offset", "I");
        mv.visitMethodInsn(invoke, fm.bcModelType(), "get", "(" + fm.bcLStoreType() + "I)" + fm.bcLSetType());
        if (!fm.bcLSetType().equals(fm.bcLFieldType())) {
            mv.visitTypeInsn(CHECKCAST, fm.bcFieldType());
        }
        mv.visitInsn(returnFor(bcType));
        final Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l3, l4, 0);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "updateAllocation0", "(I)V", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(170, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "container",
                "L" + collections + "impl/AbstractHugeContainer;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name + "Element", "index", "J");
        mv.visitMethodInsn(INVOKEVIRTUAL, collections + "impl/AbstractHugeContainer", "getAllocation",
                "(J)L" + collections + "api/HugeAllocation;");
        mv.visitTypeInsn(CHECKCAST, name + "Allocation");
        mv.visitFieldInsn(PUTFIELD, name + "Element", "allocation", "L" + name + "Allocation;");
        final Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(171, l1);
        mv.visitInsn(RETURN);
        final Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + name + "Element;", null, l0, l2, 0);
        mv.visitLocalVariable("allocationSize", "I", null, l0, l2, 1);
        mv.visitMaxs(4, 2);
        mv.visitEnd();
    }
    appendToStringHashCodeEqualsCopyOf(tm, cw, name + "Element", true);
    cw.visitEnd();

    final byte[] bytes = cw.toByteArray();
    // ClassReader cr = new ClassReader(bytes);
    // cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    return bytes;
}