Package org.ow2.asm

Examples of org.ow2.asm.ClassWriter


    }

    private void END(final int maxStack, final int maxLocals) {
        this.current.visitMaxs(maxStack, maxLocals);
        this.current.visitEnd();
        ClassWriter cw = new ClassWriter(0);
        cw.visit(Opcodes.V1_1,
                Opcodes.ACC_PUBLIC,
                "C",
                null,
                "java/lang/Object",
                null);
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC,
                "<init>",
                "()V",
                null,
                null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
                "java/lang/Object",
                "<init>",
                "()V");
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
        ((MethodNode) this.current).accept(cw);
        cw.visitEnd();
        byte[] b = cw.toByteArray();
        try {
            TestClassLoader loader = new TestClassLoader();
            Class<?> c = loader.defineClass("C", b);
            c.newInstance();
        } catch (Throwable t) {
View Full Code Here


    public void generate(final String dir) throws IOException {
        generate(dir, "pkg/InvokeDynamic.class", dump());
    }

    public byte[] dump() {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        MethodVisitor mv;

        cw.visit(V1_7,
                ACC_PUBLIC,
                "pkg/InvokeDynamic",
                null,
                "java/lang/Object",
                null);

        mv = cw.visitMethod(ACC_PUBLIC, "foo", "()V", null, null);
        mv.visitCode();
        Handle h = new Handle(H_INVOKESTATIC,
                "C",
                "bsm",
                "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/CallSite;");
        mv.visitInvokeDynamicInsn("bar", "()V", h, Type.getType("()V"), h);
        mv.visitInsn(RETURN);
        mv.visitEnd();

        cw.visitEnd();
        return cw.toByteArray();
    }
View Full Code Here

        } catch (Exception ex) {
            trace(generated);
            throw ex;
        }

        ClassWriter cw = new ClassWriter(0);
        cr.accept(new ClassAdapter(cw) {
            @Override
            public MethodVisitor visitMethod(
                final int access,
                final String name,
                final String desc,
                final String signature,
                final String[] exceptions)
            {
                return new LocalVariablesSorter(access,
                        desc,
                        super.visitMethod(access,
                                name,
                                desc,
                                signature,
                                exceptions));
            }
        },
                new Attribute[] { new Comment(), new CodeComment() },
                ClassReader.EXPAND_FRAMES);
        cr = new ClassReader(cw.toByteArray());

        String nd = n + "Dump";
        if (n.indexOf('.') != -1) {
            nd = "asm." + nd;
        }
View Full Code Here

    }

    @Override
    public void test() throws Exception {
        ClassReader cr = new ClassReader(is);
        cr.accept(new ClassAdapter(new ClassWriter(0)) {
            @Override
            public MethodVisitor visitMethod(
                final int access,
                final String name,
                final String desc,
View Full Code Here

    }

    @Override
    public void test() throws Exception {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(0);

        SAXTransformerFactory saxtf = (SAXTransformerFactory) TransformerFactory.newInstance();
        TransformerHandler handler = saxtf.newTransformerHandler();
        handler.setResult(new SAXResult(new ASMContentHandler(cw)));
        handler.startDocument();
        cr.accept(new SAXClassAdapter(handler, false), 0);
        handler.endDocument();
       
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(cw.toByteArray());

        ClassWriter cw2 = new ClassWriter(0);
        cr.accept(cw2, new Attribute[] { new Attribute("Comment") {
            @Override
            protected Attribute read(
                final ClassReader cr,
                final int off,
                final int len,
                final char[] buf,
                final int codeOff,
                final Label[] labels)
            {
                return null; // skip these attributes
            }
        },
            new Attribute("CodeComment") {
                @Override
                protected Attribute read(
                    final ClassReader cr,
                    final int off,
                    final int len,
                    final char[] buf,
                    final int codeOff,
                    final Label[] labels)
                {
                    return null; // skip these attributes
                }
            } }, 0);

        assertEquals(new ClassReader(cw2.toByteArray()),
                new ClassReader(bos.toByteArray()));
    }
View Full Code Here

    }

    @Override
    public void test() throws Exception {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(0);
        cr.accept(new ClassAdapter(cw) {
            @Override
            public MethodVisitor visitMethod(
                final int access,
                final String name,
                final String desc,
                final String signature,
                final String[] exceptions)
            {
                return new LocalVariablesSorter(access,
                        desc,
                        super.visitMethod(access,
                                name,
                                desc,
                                signature,
                                exceptions));
            }
        }, ClassReader.EXPAND_FRAMES);
        byte[] b = cw.toByteArray();
        try {
            LOADER.defineClass(n, b);
        } catch (ClassFormatError cfe) {
            fail(cfe.getMessage());
        } catch (Throwable ignored) {
View Full Code Here

                String[] exceptions)
            {
                return null;
            }
        });
        ClassWriter cw = new ClassWriter(0);
        cn.accept(cw);
        assertEquals(cr, new ClassReader(cw.toByteArray()));
    }
View Full Code Here

    }

    @Override
    public void test() throws Exception {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw1 = new ClassWriter(0);
        ClassWriter cw2 = new ClassWriter(0);
        cr.accept(new ReferenceClassAdapter(cw1), ClassReader.EXPAND_FRAMES);
        cr.accept(new AdviceClassAdapter(cw2), ClassReader.EXPAND_FRAMES);
        assertEquals(new ClassReader(cw1.toByteArray()),
                new ClassReader(cw2.toByteArray()));
    }
View Full Code Here

    final static String I2 = "Ljava/lang/Comparable;";

    @Override
    public void generate(final String dir) throws IOException {
        byte[] b = dump();
        ClassWriter cw = new ClassWriter(0);
        ClassReader cr = new ClassReader(b);
        cr.accept(new RenameAdapter(cw), ClassReader.EXPAND_FRAMES);

        generate(dir, "pkg/FrameTable.class", b);
        generate(dir, "pkg/FrameMap.class", cw.toByteArray());
    }
View Full Code Here

        generate(dir, "pkg/FrameTable.class", b);
        generate(dir, "pkg/FrameMap.class", cw.toByteArray());
    }

    public byte[] dump() {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        MethodVisitor mv;
        FieldVisitor fv;

        cw.visit(V1_6,
                ACC_PUBLIC + ACC_SUPER,
                "pkg/FrameTable",
                null,
                "java/lang/Object",
                null);

        fv = cw.visitField(M, "long", "Ljava/lang/Long;", null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "double", "Ljava/lang/Double;", null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "number", "Ljava/lang/Number;", null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "serializable", I1, null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "comparable", I2, null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "longArray", "[Ljava/lang/Long;", null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "intArray", "[I", null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "floatArray", "[F", null, null);
        fv.visitEnd();

        fv = cw.visitField(M, "objectArray", "[Ljava/lang/Object;", null, null);
        fv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC,
                "<init>",
                "(Ljava/lang/Object;)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();

        uninitializedThisType(cw);
        uninitializedLocalType(cw);
        uninitializedStackType(cw);
        nullType(cw);
        topType(cw);
        arrayTypes(cw);

        mergeTypes(cw);
        mergeStackTypes(cw);
        mergeNullArray(cw);

        appendAndChopFrame(cw);
        sameLocals1stackItemFrame(cw);
        sameLocals1stackItemFrame2(cw);
        sameLocals1stackItemFrameExtended(cw);
        sameFrameExtended(cw);

        deadCode(cw);

        cw.visitEnd();

        return cw.toByteArray();
    }
View Full Code Here

TOP

Related Classes of org.ow2.asm.ClassWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.