Search in sources :

Example 6 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 7 with ClassNode

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

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

Example 9 with ClassNode

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

the class DescriptorAndSignatureFactoryTestBase method findErrors.

private void findErrors(TypeElement typeElement, Function<FieldNode, String> fieldNodeExpectedValueGetter, Function<MethodNode, String> methodNodeExpectedValueGetter, Function<ClassNode, String> classNodeExpectedValueGetter, Function<Element, String> elementActualValueGetter) throws IOException {
    ClassNode typeNode = getClassNode(elements.getBinaryName(typeElement).toString());
    for (Element enclosedElement : typeElement.getEnclosedElements()) {
        Name elementName = enclosedElement.getSimpleName();
        String actual = elementActualValueGetter.apply(enclosedElement);
        switch(enclosedElement.getKind()) {
            case FIELD:
                checkValue("Field", elementName, fieldNodeExpectedValueGetter.apply(getFieldNode(typeNode, elementName)), actual);
                break;
            case CONSTRUCTOR:
            case METHOD:
                checkValue("Method", elementName, methodNodeExpectedValueGetter.apply(getMethodNode(typeNode, elementName)), actual);
                break;
            case ANNOTATION_TYPE:
            case CLASS:
            case ENUM:
            case INTERFACE:
                ClassNode innerTypeNode = getClassNode(elements.getBinaryName((TypeElement) enclosedElement).toString());
                checkValue("Class", elementName, classNodeExpectedValueGetter.apply(innerTypeNode), actual);
                findErrors((TypeElement) enclosedElement, fieldNodeExpectedValueGetter, methodNodeExpectedValueGetter, classNodeExpectedValueGetter, elementActualValueGetter);
                break;
            // $CASES-OMITTED$
            default:
                fail(String.format("Didn't implement testing for element kind %s", enclosedElement.getKind()));
                continue;
        }
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Name(javax.lang.model.element.Name)

Example 10 with ClassNode

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

the class DescriptorAndSignatureFactoryTestBase method getClassNode.

private ClassNode getClassNode(String classBinaryName) throws IOException {
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    testCompiler.getClasses().acceptClassVisitor(classBinaryName, 0, classNode);
    return classNode;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode)

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