Search in sources :

Example 61 with ClassWriter

use of org.objectweb.asm.ClassWriter in project lombok by rzwitserloot.

the class AsmUtil method fixJSRInlining.

static byte[] fixJSRInlining(byte[] byteCode) {
    ClassReader reader = new ClassReader(byteCode);
    ClassWriter writer = new FixedClassWriter(reader, 0);
    ClassVisitor visitor = new ClassVisitor(Opcodes.ASM6, writer) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            return new JSRInlinerAdapter(super.visitMethod(access, name, desc, signature, exceptions), access, name, desc, signature, exceptions);
        }
    };
    reader.accept(visitor, 0);
    return writer.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) JSRInlinerAdapter(org.objectweb.asm.commons.JSRInlinerAdapter)

Example 62 with ClassWriter

use of org.objectweb.asm.ClassWriter in project JikesRVM by JikesRVM.

the class AnnotationAdder method adaptClass.

/**
 * Chain together a ClassReader than will read the given class and a
 * ClassWriter to write out a new class with an adapter in the middle to add
 * annotations
 * @param fromName the name of the class we're coming from
 */
private static void adaptClass(String fromName) {
    if (VERBOSE)
        System.out.println("Adding annotations to class: " + fromName);
    // gets an input stream to read the bytecode of the class
    String resource = fromName.replace('.', '/') + ".class";
    InputStream is = BootstrapClassLoader.getBootstrapClassLoader().getResourceAsStream(resource);
    byte[] b;
    // adapts the class on the fly
    try {
        ClassReader cr = new ClassReader(is);
        ClassWriter cw = new ClassWriter(0);
        ClassVisitor cv = new AddAnnotationClassAdapter(cw, fromName);
        cr.accept(cv, 0);
        b = cw.toByteArray();
    } catch (Exception e) {
        throw new Error("Couldn't find class " + fromName + " (" + resource + ")", e);
    }
    // store the adapted class on disk
    try {
        File file = new File(destinationDir + resource);
        // ensure we have a directory to write to
        new File(file.getParent()).mkdirs();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(b);
        fos.close();
    } catch (Exception e) {
        throw new Error("Error writing to " + destinationDir + resource + " to disk", e);
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter)

Example 63 with ClassWriter

use of org.objectweb.asm.ClassWriter in project graal by oracle.

the class DeoptimizeOnExceptionTest method makeClazz.

private static byte[] makeClazz() {
    // Code generated the class below using asm.
    String clazzName = DeoptimizeOnExceptionTest.class.getName().replace('.', '/');
    final ClassWriter w = new ClassWriter(0);
    w.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "t/TestJSR", null, "java/lang/Object", new String[] { "java/lang/Runnable" });
    MethodVisitor mv = w.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[] {});
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(10, 10);
    mv.visitEnd();
    mv = w.visitMethod(Opcodes.ACC_PUBLIC, "run", "()V", null, null);
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    Label l1 = new Label();
    mv.visitJumpInsn(Opcodes.JSR, l1);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(l1);
    mv.visitVarInsn(Opcodes.ASTORE, 1);
    Label lElse = new Label();
    Label lEnd = new Label();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
    mv.visitInsn(Opcodes.POP2);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
    mv.visitInsn(Opcodes.DUP);
    mv.visitJumpInsn(Opcodes.IFNULL, lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodA", "()V", false);
    mv.visitJumpInsn(Opcodes.GOTO, lEnd);
    mv.visitLabel(lElse);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodB", "()V", false);
    mv.visitLabel(lEnd);
    mv.visitVarInsn(Opcodes.RET, 1);
    mv.visitMaxs(10, 10);
    mv.visitEnd();
    return w.toByteArray();
}
Also used : Label(org.objectweb.asm.Label) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 64 with ClassWriter

use of org.objectweb.asm.ClassWriter in project wildfly-swarm by wildfly-swarm.

the class FaviconFactory method createFaviconServletExtension.

static byte[] createFaviconServletExtension(String name) throws IOException {
    ClassReader reader = new ClassReader(FaviconServletExtension.class.getClassLoader().getResourceAsStream(FaviconServletExtension.class.getName().replace('.', '/') + ".class"));
    String slashName = name.replace('.', '/');
    ClassWriter writer = new ClassWriter(0);
    Remapper remapper = new Remapper() {

        @Override
        public String map(String typeName) {
            if (typeName.equals("org/wildfly/swarm/undertow/internal/FaviconServletExtension")) {
                return slashName;
            }
            return super.map(typeName);
        }
    };
    ClassRemapper adapter = new ClassRemapper(writer, remapper);
    reader.accept(adapter, ClassReader.EXPAND_FRAMES);
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : Remapper(org.objectweb.asm.commons.Remapper) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) ClassReader(org.objectweb.asm.ClassReader) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) ClassWriter(org.objectweb.asm.ClassWriter)

Example 65 with ClassWriter

use of org.objectweb.asm.ClassWriter in project Fracture by HellFirePvP.

the class FractureTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (!isTransformationRequired(transformedName))
        return bytes;
    ClassNode node = new ClassNode();
    ClassReader reader = new ClassReader(bytes);
    reader.accept(node, 0);
    for (SubClassTransformer subTransformer : subTransformers) {
        try {
            subTransformer.transformClassNode(node, transformedName, name);
        } catch (ASMTransformationException asmException) {
            FMLLog.warning("Access transformation failed for Transformer: " + subTransformer.getIdentifier());
            FMLLog.warning("Transformer added information:");
            subTransformer.addErrorInformation();
            asmException.printStackTrace();
            // Rethrow
            throw asmException;
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    node.accept(writer);
    bytes = writer.toByteArray();
    if (false) {
        try {
            File f = new File("C:/FRTestClasses/" + transformedName + ".class");
            f.getParentFile().mkdirs();
            f.createNewFile();
            FileOutputStream out = new FileOutputStream(f);
            out.write(bytes);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return bytes;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException)

Aggregations

ClassWriter (org.objectweb.asm.ClassWriter)502 ClassReader (org.objectweb.asm.ClassReader)285 MethodVisitor (org.objectweb.asm.MethodVisitor)127 Test (org.junit.Test)99 ClassVisitor (org.objectweb.asm.ClassVisitor)88 ClassNode (org.objectweb.asm.tree.ClassNode)70 IOException (java.io.IOException)62 Label (org.objectweb.asm.Label)55 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)52 HashSet (java.util.HashSet)37 Method (java.lang.reflect.Method)36 Type (org.objectweb.asm.Type)33 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 File (java.io.File)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)28 FieldVisitor (org.objectweb.asm.FieldVisitor)28 FileOutputStream (java.io.FileOutputStream)27 InputStream (java.io.InputStream)26 MethodNode (org.objectweb.asm.tree.MethodNode)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23