Search in sources :

Example 1 with ClassWriter

use of org.objectweb.asm.ClassWriter in project storm by apache.

the class DefaultShader method addRemappedClass.

private void addRemappedClass(RelocatorRemapper remapper, JarOutputStream jos, String name, InputStream is) throws IOException {
    LOG.debug("Remapping class... " + name);
    if (!remapper.hasRelocators()) {
        try {
            LOG.debug("Just copy class...");
            jos.putNextEntry(new JarEntry(name));
            IOUtil.copy(is, jos);
        } catch (ZipException e) {
            LOG.info("zip exception ", e);
        }
        return;
    }
    ClassReader cr = new ClassReader(is);
    // We don't pass the ClassReader here. This forces the ClassWriter to rebuild the constant pool.
    // Copying the original constant pool should be avoided because it would keep references
    // to the original class names. This is not a problem at runtime (because these entries in the
    // constant pool are never used), but confuses some tools such as Felix' maven-bundle-plugin
    // that use the constant pool to determine the dependencies of a class.
    ClassWriter cw = new ClassWriter(0);
    final String pkg = name.substring(0, name.lastIndexOf('/') + 1);
    ClassVisitor cv = new RemappingClassAdapter(cw, remapper) {

        @Override
        public void visitSource(final String source, final String debug) {
            LOG.debug("visitSource " + source);
            if (source == null) {
                super.visitSource(source, debug);
            } else {
                final String fqSource = pkg + source;
                final String mappedSource = remapper.map(fqSource);
                final String filename = mappedSource.substring(mappedSource.lastIndexOf('/') + 1);
                LOG.debug("Remapped to " + filename);
                super.visitSource(filename, debug);
            }
        }
    };
    try {
        cr.accept(cv, ClassReader.EXPAND_FRAMES);
    } catch (Throwable ise) {
        throw new IOException("Error in ASM processing class " + name, ise);
    }
    byte[] renamedClass = cw.toByteArray();
    // Need to take the .class off for remapping evaluation
    String mappedName = remapper.map(name.substring(0, name.indexOf('.')));
    LOG.debug("Remapped class name to " + mappedName);
    try {
        // Now we put it back on so the class file is written out with the right extension.
        jos.putNextEntry(new JarEntry(mappedName + ".class"));
        jos.write(renamedClass);
    } catch (ZipException e) {
        LOG.info("zip exception ", e);
    }
}
Also used : RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) ClassReader(org.objectweb.asm.ClassReader) ZipException(java.util.zip.ZipException) ClassVisitor(org.objectweb.asm.ClassVisitor) JarEntry(java.util.jar.JarEntry) ClassWriter(org.objectweb.asm.ClassWriter)

Example 2 with ClassWriter

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

the class DebuggingClassWriter method toByteArray.

public byte[] toByteArray() {
    return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
            if (debugLocation != null) {
                String dirs = className.replace('.', File.separatorChar);
                try {
                    new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
                    File file = new File(new File(debugLocation), dirs + ".class");
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    try {
                        out.write(b);
                    } finally {
                        out.close();
                    }
                    if (traceCtor != null) {
                        file = new File(new File(debugLocation), dirs + ".asm");
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        try {
                            ClassReader cr = new ClassReader(b);
                            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                            ClassVisitor tcv = (ClassVisitor) traceCtor.newInstance(new Object[] { null, pw });
                            cr.accept(tcv, 0);
                            pw.flush();
                        } finally {
                            out.close();
                        }
                    }
                } catch (Exception e) {
                    throw new CodeGenerationException(e);
                }
            }
            return b;
        }
    });
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) ClassReader(org.objectweb.asm.ClassReader)

Example 3 with ClassWriter

use of org.objectweb.asm.ClassWriter in project buck by facebook.

the class FilesystemStubJarWriter method getStubClassBytes.

private static ByteSource getStubClassBytes(ClassNode node) {
    ClassWriter writer = new ClassWriter(0);
    node.accept(new AbiFilteringClassVisitor(writer));
    return ByteSource.wrap(writer.toByteArray());
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter)

Example 4 with ClassWriter

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

the class DeobfuscationTransformer method transform.

// COMPUTE_FRAMES causes classes to be loaded, which could cause issues if the classes do not exist.
// However in testing this has not happened. {As we run post SideTransformer}
// If reported we need to add a custom implementation of ClassWriter.getCommonSuperClass
// that does not cause class loading.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    ClassReader classReader = new ClassReader(bytes);
    ClassWriter classWriter = new ClassWriter(WRITER_FLAGS);
    RemappingClassAdapter remapAdapter = new FMLRemappingAdapter(classWriter);
    classReader.accept(remapAdapter, READER_FLAGS);
    return classWriter.toByteArray();
}
Also used : RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) FMLRemappingAdapter(net.minecraftforge.fml.common.asm.transformers.deobf.FMLRemappingAdapter) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 5 with ClassWriter

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

the class EventSubscriptionTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null || name.equals("net.minecraftforge.fml.common.eventhandler.Event") || name.startsWith("net.minecraft.") || name.indexOf('.') == -1) {
        return bytes;
    }
    ClassReader cr = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);
    try {
        if (buildEvents(classNode)) {
            ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
            classNode.accept(cw);
            return cw.toByteArray();
        }
        return bytes;
    } catch (ClassNotFoundException ex) {
    // Discard silently- it's just noise
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

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