Examples of ClassWriter


Examples of org.ow2.asm.ClassWriter

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

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

        cw.visit(V1_5,
                ACC_PUBLIC + ACC_SUPER,
                "pkg/Debug",
                null,
                "java/lang/Object",
                new String[] { "java/io/Serializable" });

        cw.visitSource("Debug.java", "source-debug");

        FieldVisitor fv = cw.visitField(ACC_FINAL + ACC_STATIC,
                "serialVersionUID",
                "J",
                null,
                new Long(1L));
        fv.visitEnd();

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
                "<init>",
                "()V",
                null,
                null);
        mv.visitCode();
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(3, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(ICONST_0);
        mv.visitJumpInsn(IFEQ, l1);
        mv.visitJumpInsn(GOTO, l1);
        mv.visitLabel(l1);
        mv.visitLineNumber(3, l1);
        mv.visitInsn(RETURN);
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "Lpkg/Debug;", "Lpkg/Debug;", l0, l2, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

        cw.visitEnd();

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

Examples of org.ow2.asm.ClassWriter

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

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

        cw.visit(1 << 16 | V1_5,
                ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
                "Interface",
                "<E:Ljava/lang/Object;>Ljava/lang/Object;",
                "java/lang/Object",
                null);

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT,
                "m",
                "(ZBCSIFJDLjava/lang/Object;)Ljava/lang/Object;",
                "(ZBCSIFJDTE;)TE;",
                null);
        mv.visitEnd();

        cw.visitEnd();

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

Examples of org.ow2.asm.ClassWriter

        /*
         * Returns the byte code of a class corresponding to this expression.
         */
        byte[] compile(final String name) {
            // class header
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
            cw.visit(V1_7,
                    ACC_PUBLIC,
                    name,
                    null,
                    "java/lang/Object",
                    null);

            // eval method type
            StringBuilder desc = new StringBuilder("(");
            for (int i = 0; i <= getMaxVarIndex(); ++i) {
                desc.append("Ljava/lang/Object;");
            }
            desc.append(")Ljava/lang/Object;");
           
            // eval method
            MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC,
                    "eval",
                    desc.toString(),
                    null,
                    null);
            compile(mv);
            mv.visitInsn(ARETURN);
            // max stack and max locals automatically computed
            mv.visitMaxs(0, 0);
            mv.visitEnd();

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

Examples of org.ow2.asm.ClassWriter

public class Attributes extends ClassLoader {

    public static void main(final String args[]) throws Exception {
        // loads the original class and adapts it
        ClassReader cr = new ClassReader("CommentAttribute");
        ClassWriter cw = new ClassWriter(0);
        ClassVisitor cv = new AddCommentClassAdapter(cw);
        cr.accept(cv, new Attribute[] { new CommentAttribute("") }, 0);
        byte[] b = cw.toByteArray();

        // stores the adapted class on disk
        FileOutputStream fos = new FileOutputStream("CommentAttribute.class.new");
        try {
            fos.write(b);
View Full Code Here

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

Examples of org.ow2.asm.ClassWriter

    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

Examples of org.ow2.asm.ClassWriter

        } 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

Examples of org.ow2.asm.ClassWriter

    }

    @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

Examples of org.ow2.asm.ClassWriter

    }

    @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

Examples of org.ow2.asm.ClassWriter

    }

    @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
TOP
Copyright © 2018 www.massapi.com. 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.