Search in sources :

Example 1 with ClassNode

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

the class ClassNodeListSupplierTest method testOneJar.

@Test
public void testOneJar() throws IOException {
    File jar = new File(tmpDir.getRoot(), "primary.jar");
    ZipOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
    jarOut.putNextEntry(new JarEntry("com/facebook/buck/android/ClassNodeListSupplierTest.class"));
    writeClassBytes(ClassNodeListSupplierTest.class, jarOut);
    jarOut.close();
    Supplier<ImmutableList<ClassNode>> supplier = ClassNodeListSupplier.createMemoized(ImmutableList.of(jar.toPath()));
    ImmutableList<ClassNode> classNodes = supplier.get();
    assertEquals(1, classNodes.size());
    assertEquals(Type.getType(ClassNodeListSupplierTest.class).getInternalName(), classNodes.get(0).name);
    // Memoized should always return the same object
    assertSame(classNodes, supplier.get());
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipOutputStream(java.util.zip.ZipOutputStream) ImmutableList(com.google.common.collect.ImmutableList) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) Test(org.junit.Test)

Example 2 with ClassNode

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

the class FirstOrderHelper method addDependencies.

private ImmutableSet<String> addDependencies(Iterable<ClassNode> allClasses) {
    for (ClassNode classNode : allClasses) {
        FirstOrderVisitorContext context = new FirstOrderVisitorContext();
        classNode.accept(context.classVisitor);
        FirstOrderTypeInfo info = context.builder.build();
        knownTypes.put(info.type, info);
    }
    for (Type type : scenarioTypes) {
        addFirstOrderTypes(type);
    }
    return resultBuilder.build();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) Type(org.objectweb.asm.Type)

Example 3 with ClassNode

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

the class DxAnalysisMain method loadAllClasses.

private static ImmutableMap<String, ClassNode> loadAllClasses(String zipFileName) throws IOException {
    ImmutableMap.Builder<String, ClassNode> allClassesBuilder = ImmutableMap.builder();
    try (ZipFile inJar = new ZipFile(zipFileName)) {
        for (ZipEntry entry : Collections.list(inJar.entries())) {
            if (!entry.getName().endsWith(".class")) {
                continue;
            }
            // Skip external libraries.
            if (entry.getName().startsWith("junit/") || entry.getName().startsWith("org/junit/") || entry.getName().startsWith("com/google/common/")) {
                continue;
            }
            byte[] rawClass = ByteStreams.toByteArray(inJar.getInputStream(entry));
            ClassNode klass = new ClassNode();
            new ClassReader(rawClass).accept(klass, ClassReader.EXPAND_FRAMES);
            allClassesBuilder.put(klass.name, klass);
        }
    }
    return allClassesBuilder.build();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 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 5 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)

Aggregations

ClassNode (org.objectweb.asm.tree.ClassNode)328 ClassReader (org.objectweb.asm.ClassReader)148 MethodNode (org.objectweb.asm.tree.MethodNode)123 ClassWriter (org.objectweb.asm.ClassWriter)70 IOException (java.io.IOException)44 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)38 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)34 Test (org.junit.Test)29 InsnList (org.objectweb.asm.tree.InsnList)29 FieldNode (org.objectweb.asm.tree.FieldNode)28 ArrayList (java.util.ArrayList)27 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)26 Label (org.objectweb.asm.Label)21 InputStream (java.io.InputStream)20 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)18 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)18 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)17 InsnNode (org.objectweb.asm.tree.InsnNode)17 Method (java.lang.reflect.Method)16 List (java.util.List)16