Search in sources :

Example 11 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project buck by facebook.

the class MutabilityAnalyzer method markClassAsHavingMutableDescendents.

private void markClassAsHavingMutableDescendents(String className) {
    if (classesWithMutableDescendents.contains(className)) {
        return;
    }
    ClassNode klass = allClasses.get(className);
    if (klass == null) {
        return;
    }
    classesWithMutableDescendents.add(className);
    madeProgress = true;
    markClassAsHavingMutableDescendents(klass.superName);
    for (String iface : klass.interfaces) {
        markClassAsHavingMutableDescendents(iface);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode)

Example 12 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project buck by facebook.

the class StaticStateAnalyzer method go.

private void go() {
    ImmutableSet.Builder<String> unsafeClassesBuilder = ImmutableSet.builder();
    for (ClassNode klass : allClasses.values()) {
        boolean classIsSafe = isClassSafe(klass);
        if (!classIsSafe) {
            unsafeClassesBuilder.add(klass.name);
        }
    }
    unsafeClasses = unsafeClassesBuilder.build();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 13 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project enumerable by hraberg.

the class LambdaExpressionTrees method findMethodNode.

@SuppressWarnings("unchecked")
static MethodNode findMethodNode(Method method) throws IOException {
    String className = method.getDeclaringClass().getName();
    ClassReader cr;
    if (InMemoryCompiler.bytesByClassName.containsKey(className))
        cr = new ClassReader(InMemoryCompiler.bytesByClassName.get(className));
    else
        cr = new ClassReader(className);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    String descriptor = getMethodDescriptor(method);
    for (MethodNode mn : (List<MethodNode>) cn.methods) {
        if (method.getName().equals(mn.name) && descriptor.equals(mn.desc))
            return mn;
    }
    throw new IllegalStateException("Cannot find method which does exist");
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) List(java.util.List)

Example 14 with ClassNode

use of org.objectweb.asm.tree.ClassNode in project MinecraftForge by MinecraftForge.

the class AccessTransformer method processJar.

private static void processJar(File inFile, File outFile, AccessTransformer[] 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 (AccessTransformer 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 15 with ClassNode

use of org.objectweb.asm.tree.ClassNode 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

ClassNode (org.objectweb.asm.tree.ClassNode)117 ClassReader (org.objectweb.asm.ClassReader)58 MethodNode (org.objectweb.asm.tree.MethodNode)42 ClassWriter (org.objectweb.asm.ClassWriter)28 IOException (java.io.IOException)14 FieldNode (org.objectweb.asm.tree.FieldNode)12 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Test (org.junit.Test)10 ZipEntry (java.util.zip.ZipEntry)8 Method (java.lang.reflect.Method)7 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)6 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)6 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)6 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 Label (org.objectweb.asm.Label)5