Search in sources :

Example 66 with ClassReader

use of org.objectweb.asm.ClassReader 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)

Example 67 with ClassReader

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

the class MarkerTransformer method processJar.

private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;
    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }
        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }
        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }
            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);
            byte[] entryData = entryBuffer.toByteArray();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');
                for (MarkerTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }
            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 68 with ClassReader

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

the class SideTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    if (remove(classNode.visibleAnnotations, SIDE)) {
        if (DEBUG) {
            System.out.println(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
        }
        throw new RuntimeException(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
    }
    Iterator<FieldNode> fields = classNode.fields.iterator();
    while (fields.hasNext()) {
        FieldNode field = fields.next();
        if (remove(field.visibleAnnotations, SIDE)) {
            if (DEBUG) {
                System.out.println(String.format("Removing Field: %s.%s", classNode.name, field.name));
            }
            fields.remove();
        }
    }
    LambdaGatherer lambdaGatherer = new LambdaGatherer();
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        MethodNode method = methods.next();
        if (remove(method.visibleAnnotations, SIDE)) {
            if (DEBUG) {
                System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
            }
            methods.remove();
            lambdaGatherer.accept(method);
        }
    }
    // remove dynamic lambda methods that are inside of removed methods
    List<Handle> dynamicLambdaHandles = lambdaGatherer.getDynamicLambdaHandles();
    if (!dynamicLambdaHandles.isEmpty()) {
        methods = classNode.methods.iterator();
        while (methods.hasNext()) {
            MethodNode method = methods.next();
            for (Handle dynamicLambdaHandle : dynamicLambdaHandles) {
                if (method.name.equals(dynamicLambdaHandle.getName()) && method.desc.equals(dynamicLambdaHandle.getDesc())) {
                    if (DEBUG) {
                        System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
                    }
                    methods.remove();
                }
            }
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) Handle(org.objectweb.asm.Handle)

Example 69 with ClassReader

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

the class FirstOrderTest method loadClassNode.

private static ClassNode loadClassNode(Class<?> input) {
    try {
        ClassReader reader = new ClassReader(input.getName());
        ClassNode node = new ClassNode(Opcodes.ASM4);
        reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return node;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException)

Example 70 with ClassReader

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

the class AbiClass method extract.

public static AbiClass extract(Path pathToJar, String className) throws IOException {
    try (ZipFile zip = new ZipFile(pathToJar.toString())) {
        ZipEntry entry = zip.getEntry(className);
        if (entry == null) {
            return null;
        }
        try (InputStream entryStream = zip.getInputStream(entry)) {
            ClassReader reader = new ClassReader(entryStream);
            ClassNode classNode = new ClassNode();
            reader.accept(classNode, 0);
            return new AbiClass(classNode);
        }
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)449 ClassWriter (org.objectweb.asm.ClassWriter)187 Test (org.junit.Test)134 IOException (java.io.IOException)78 InputStream (java.io.InputStream)76 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 ClassVisitor (org.objectweb.asm.ClassVisitor)54 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 HashSet (java.util.HashSet)39 ZipEntry (java.util.zip.ZipEntry)34 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 ZipFile (java.util.zip.ZipFile)29 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 Method (java.lang.reflect.Method)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23 InnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass)23 PrintWriter (java.io.PrintWriter)23 MethodVisitor (org.objectweb.asm.MethodVisitor)23 MethodNode (org.objectweb.asm.tree.MethodNode)21