Search in sources :

Example 11 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)

Example 12 with ClassReader

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

the class ClassesImpl method acceptClassVisitor.

@Override
public void acceptClassVisitor(String qualifiedName, int flags, ClassVisitor cv) throws IOException {
    Path classFilePath = resolveClassFilePath(qualifiedName);
    try (InputStream stream = Files.newInputStream(classFilePath)) {
        ClassReader reader = new ClassReader(stream);
        reader.accept(cv, flags);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader)

Example 13 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class AsmAnalyzer method parseZip.

/**
     * Parses a JAR file and returns a list of all classes founds using a map
     * class name => ASM ClassReader. Class names are in the form "android.view.View".
     */
Map<String, ClassReader> parseZip(List<String> jarPathList) throws IOException {
    TreeMap<String, ClassReader> classes = new TreeMap<String, ClassReader>();
    for (String jarPath : jarPathList) {
        ZipFile zip = new ZipFile(jarPath);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        ZipEntry entry;
        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            if (entry.getName().endsWith(".class")) {
                ClassReader cr = new ClassReader(zip.getInputStream(entry));
                String className = classReaderToClassName(cr);
                classes.put(className, cr);
            }
        }
    }
    return classes;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) TreeMap(java.util.TreeMap)

Example 14 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class AsmGenerator method generate.

/** Generates the final JAR */
public void generate() throws FileNotFoundException, IOException {
    TreeMap<String, byte[]> all = new TreeMap<String, byte[]>();
    for (Class<?> clazz : mInjectClasses) {
        String name = classToEntryPath(clazz);
        InputStream is = ClassLoader.getSystemResourceAsStream(name);
        ClassReader cr = new ClassReader(is);
        byte[] b = transform(cr, true);
        name = classNameToEntryPath(transformName(cr.getClassName()));
        all.put(name, b);
    }
    for (Entry<String, ClassReader> entry : mDeps.entrySet()) {
        ClassReader cr = entry.getValue();
        byte[] b = transform(cr, true);
        String name = classNameToEntryPath(transformName(cr.getClassName()));
        all.put(name, b);
    }
    for (Entry<String, ClassReader> entry : mKeep.entrySet()) {
        ClassReader cr = entry.getValue();
        byte[] b = transform(cr, true);
        String name = classNameToEntryPath(transformName(cr.getClassName()));
        all.put(name, b);
    }
    mLog.info("# deps classes: %d", mDeps.size());
    mLog.info("# keep classes: %d", mKeep.size());
    mLog.info("# renamed     : %d", mRenameCount);
    createJar(new FileOutputStream(mOsDestJar), all);
    mLog.info("Created JAR file %s", mOsDestJar);
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) TreeMap(java.util.TreeMap)

Example 15 with ClassReader

use of org.objectweb.asm.ClassReader in project android_frameworks_base by ParanoidAndroid.

the class DependencyFinder method findClassesDeps.

/**
     * Finds all dependencies for all classes in keepClasses which are also
     * listed in zipClasses. Returns a map of all the dependencies found.
     */
Map<String, Set<String>> findClassesDeps(Map<String, ClassReader> zipClasses) {
    // The dependencies that we'll collect.
    // It's a map Class name => uses class names.
    Map<String, Set<String>> dependencyMap = new TreeMap<String, Set<String>>();
    DependencyVisitor visitor = getVisitor();
    int count = 0;
    try {
        for (Entry<String, ClassReader> entry : zipClasses.entrySet()) {
            String name = entry.getKey();
            TreeSet<String> set = new TreeSet<String>();
            dependencyMap.put(name, set);
            visitor.setDependencySet(set);
            ClassReader cr = entry.getValue();
            cr.accept(visitor, 0);
            visitor.setDependencySet(null);
            mLog.debugNoln("Visited %d classes\r", ++count);
        }
    } finally {
        mLog.debugNoln("\n");
    }
    return dependencyMap;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) TreeSet(java.util.TreeSet) ClassReader(org.objectweb.asm.ClassReader) TreeMap(java.util.TreeMap)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)437 ClassWriter (org.objectweb.asm.ClassWriter)182 Test (org.junit.Test)134 IOException (java.io.IOException)77 InputStream (java.io.InputStream)75 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 ClassVisitor (org.objectweb.asm.ClassVisitor)48 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