Example usage for org.objectweb.asm.tree ClassNode ClassNode

List of usage examples for org.objectweb.asm.tree ClassNode ClassNode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree ClassNode ClassNode.

Prototype

public ClassNode() 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:com.android.build.gradle.internal2.incremental.IncrementalVisitor.java

License:Apache License

@NonNull
private static List<ClassNode> parseParents(@NonNull File inputFile, @NonNull ClassNode classNode)
        throws IOException {
    File binaryFolder = getBinaryFolder(inputFile, classNode);
    List<ClassNode> parentNodes = new ArrayList<>();
    String currentParentName = classNode.superName;

    while (currentParentName != null) {
        File parentFile = new File(binaryFolder, currentParentName + ".class");
        if (parentFile.exists()) {
            LOG.info("Parsing %s.", parentFile);
            InputStream parentFileClassReader = new BufferedInputStream(new FileInputStream(parentFile));
            ClassReader parentClassReader = new ClassReader(parentFileClassReader);
            ClassNode parentNode = new ClassNode();
            parentClassReader.accept(parentNode, ClassReader.EXPAND_FRAMES);
            parentNodes.add(parentNode);
            currentParentName = parentNode.superName;
        } else {/*from w w  w. j  av  a2s  . c om*/
            // May need method information from outside of the current project. Thread local class reader
            // should be the one
            try {
                ClassReader parentClassReader = new ClassReader(Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(currentParentName + ".class"));
                ClassNode parentNode = new ClassNode();
                parentClassReader.accept(parentNode, ClassReader.EXPAND_FRAMES);
                parentNodes.add(parentNode);
                currentParentName = parentNode.superName;
            } catch (IOException e) {
                // Could not locate parent class. This is as far as we can go locating parents.
                LOG.warning("IncrementalVisitor parseParents could not locate %1$s "
                        + "which is an ancestor of project class %2$s.\n"
                        + "%2$s is not eligible for hot swap.", currentParentName, classNode.name);
                return ImmutableList.of();
            }
        }
    }
    return parentNodes;
}

From source file:com.android.build.gradle.internal2.incremental.IncrementalVisitor.java

License:Apache License

@Nullable
private static ClassNode parsePackageInfo(@NonNull File inputFile, @NonNull ClassNode classNode)
        throws IOException {

    File packageFolder = inputFile.getParentFile();
    File packageInfoClass = new File(packageFolder, "package-info.class");
    if (packageInfoClass.exists()) {
        InputStream reader = new BufferedInputStream(new FileInputStream(packageInfoClass));
        ClassReader classReader = new ClassReader(reader);
        ClassNode packageInfo = new ClassNode();
        classReader.accept(packageInfo, ClassReader.EXPAND_FRAMES);
        return packageInfo;
    }//from w w w.  j av a  2  s . com
    return null;
}

From source file:com.android.ide.eclipse.apt.internal.crawler.ClassCrawler.java

License:Apache License

/**
 * Scans an Android project directory to retrieve all the class files in it.
 * @param dir - The directory to be scanned
 * @return A collection of class nodes ready to be used with the ASM library for the analysis
 * @throws FileNotFoundException// w  w  w .  j  a  v  a 2 s  .  c  o m
 * @throws IOException
 */
private static Collection<ClassNode> scanDirectory(final File dir) throws FileNotFoundException, IOException {
    final LinkedList<ClassNode> classes = new LinkedList<ClassNode>();
    final File[] files = dir.listFiles();
    for (final File file : files) {
        if (file.isFile()) {
            final String name = file.getName();
            //get all the classes except the class R
            if (name.endsWith(".class") && !name.startsWith("R$") && !name.equals("R.class")) {
                final ClassReader classReader = new ClassReader(new FileInputStream(file));
                final ClassNode classNode = new ClassNode();
                classReader.accept(classNode, 0);
                classes.add(classNode);
            }
        } else {
            classes.addAll(scanDirectory(file));
        }
    }
    sClassNodes = classes;
    return classes;
}

From source file:com.android.tools.klint.client.api.LintDriver.java

License:Apache License

private void runClassDetectors(Scope scope, List<ClassEntry> entries, Project project, Project main) {
    if (mScope.contains(scope)) {
        List<Detector> classDetectors = mScopeDetectors.get(scope);
        if (classDetectors != null && !classDetectors.isEmpty() && !entries.isEmpty()) {
            AsmVisitor visitor = new AsmVisitor(mClient, classDetectors);

            String sourceContents = null;
            String sourceName = "";
            mOuterClasses = new ArrayDeque<ClassNode>();
            ClassEntry prev = null;/* w  w  w .j  a  v  a 2 s  . c o  m*/
            for (ClassEntry entry : entries) {
                if (prev != null && prev.compareTo(entry) == 0) {
                    // Duplicate entries for some reason: ignore
                    continue;
                }
                prev = entry;

                ClassReader reader;
                ClassNode classNode;
                try {
                    reader = new ClassReader(entry.bytes);
                    classNode = new ClassNode();
                    reader.accept(classNode, 0 /* flags */);
                } catch (Throwable t) {
                    mClient.log(null, "Error processing %1$s: broken class file?", entry.path());
                    continue;
                }

                ClassNode peek;
                while ((peek = mOuterClasses.peek()) != null) {
                    if (classNode.name.startsWith(peek.name)) {
                        break;
                    } else {
                        mOuterClasses.pop();
                    }
                }
                mOuterClasses.push(classNode);

                if (isSuppressed(null, classNode)) {
                    // Class was annotated with suppress all -- no need to look any further
                    continue;
                }

                if (sourceContents != null) {
                    // Attempt to reuse the source buffer if initialized
                    // This means making sure that the source files
                    //    foo/bar/MyClass and foo/bar/MyClass$Bar
                    //    and foo/bar/MyClass$3 and foo/bar/MyClass$3$1 have the same prefix.
                    String newName = classNode.name;
                    int newRootLength = newName.indexOf('$');
                    if (newRootLength == -1) {
                        newRootLength = newName.length();
                    }
                    int oldRootLength = sourceName.indexOf('$');
                    if (oldRootLength == -1) {
                        oldRootLength = sourceName.length();
                    }
                    if (newRootLength != oldRootLength
                            || !sourceName.regionMatches(0, newName, 0, newRootLength)) {
                        sourceContents = null;
                    }
                }

                ClassContext context = new ClassContext(this, project, main, entry.file, entry.jarFile,
                        entry.binDir, entry.bytes, classNode, scope == Scope.JAVA_LIBRARIES /*fromLibrary*/,
                        sourceContents);

                try {
                    visitor.runClassDetectors(context);
                } catch (Exception e) {
                    mClient.log(e, null);
                }

                if (mCanceled) {
                    return;
                }

                sourceContents = context.getSourceContents(false/*read*/);
                sourceName = classNode.name;
            }

            mOuterClasses = null;
        }
    }
}

From source file:com.android.tools.klint.client.api.LintDriver.java

License:Apache License

/**
 * Returns the {@link ClassNode} corresponding to the given type, if possible, or null
 *
 * @param type the fully qualified type, using JVM signatures (/ and $, not . as path
 *             separators)/*from w w w  .  j a  va2  s . c o m*/
 * @param flags the ASM flags to pass to the {@link ClassReader}, normally 0 but can
 *              for example be {@link ClassReader#SKIP_CODE} and/oor
 *              {@link ClassReader#SKIP_DEBUG}
 * @return the class node for the type, or null
 */
@Nullable
public ClassNode findClass(@NonNull ClassContext context, @NonNull String type, int flags) {
    String relative = type.replace('/', File.separatorChar) + DOT_CLASS;
    File classFile = findClassFile(context.getProject(), relative);
    if (classFile != null) {
        if (classFile.getPath().endsWith(DOT_JAR)) {
            // TODO: Handle .jar files
            return null;
        }

        try {
            byte[] bytes = mClient.readBytes(classFile);
            ClassReader reader = new ClassReader(bytes);
            ClassNode classNode = new ClassNode();
            reader.accept(classNode, flags);

            return classNode;
        } catch (Throwable t) {
            mClient.log(null, "Error processing %1$s: broken class file?", classFile.getPath());
        }
    }

    return null;
}

From source file:com.android.tools.lint.checks.ApiLookupTest.java

License:Apache License

private static void analyzeClass(@NonNull byte[] bytes, @NonNull Map<String, ClassNode> classes,
        @NonNull Map<String, Integer> minSdkMap, int manifestMinSdk) {

    ClassReader reader = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    reader.accept(classNode, 0 /* flags */);

    assertNull(classes.get(classNode.name));
    classes.put(classNode.name, classNode);

    int minSdk = manifestMinSdk != -1 ? manifestMinSdk : getMinSdk(classNode.name, minSdkMap);
    minSdkMap.put(classNode.name, minSdk);
}

From source file:com.android.tools.lint.client.api.LintDriver.java

License:Apache License

private void runClassDetectors(Scope scope, List<ClassEntry> entries, Project project, Project main) {
    if (mScope.contains(scope)) {
        List<Detector> classDetectors = mScopeDetectors.get(scope);
        if (classDetectors != null && !classDetectors.isEmpty() && !entries.isEmpty()) {
            AsmVisitor visitor = new AsmVisitor(mClient, classDetectors);

            String sourceContents = null;
            String sourceName = "";
            mOuterClasses = new ArrayDeque<ClassNode>();
            for (ClassEntry entry : entries) {
                ClassReader reader;//from   w  ww  . j  ava  2  s . c  om
                ClassNode classNode;
                try {
                    reader = new ClassReader(entry.bytes);
                    classNode = new ClassNode();
                    reader.accept(classNode, 0 /* flags */);
                } catch (Throwable t) {
                    mClient.log(null, "Error processing %1$s: broken class file?", entry.path());
                    continue;
                }

                ClassNode peek;
                while ((peek = mOuterClasses.peek()) != null) {
                    if (classNode.name.startsWith(peek.name)) {
                        break;
                    } else {
                        mOuterClasses.pop();
                    }
                }
                mOuterClasses.push(classNode);

                if (isSuppressed(null, classNode)) {
                    // Class was annotated with suppress all -- no need to look any further
                    continue;
                }

                if (sourceContents != null) {
                    // Attempt to reuse the source buffer if initialized
                    // This means making sure that the source files
                    //    foo/bar/MyClass and foo/bar/MyClass$Bar
                    //    and foo/bar/MyClass$3 and foo/bar/MyClass$3$1 have the same prefix.
                    String newName = classNode.name;
                    int newRootLength = newName.indexOf('$');
                    if (newRootLength == -1) {
                        newRootLength = newName.length();
                    }
                    int oldRootLength = sourceName.indexOf('$');
                    if (oldRootLength == -1) {
                        oldRootLength = sourceName.length();
                    }
                    if (newRootLength != oldRootLength
                            || !sourceName.regionMatches(0, newName, 0, newRootLength)) {
                        sourceContents = null;
                    }
                }

                ClassContext context = new ClassContext(this, project, main, entry.file, entry.jarFile,
                        entry.binDir, entry.bytes, classNode, scope == Scope.JAVA_LIBRARIES /*fromLibrary*/,
                        sourceContents);

                try {
                    visitor.runClassDetectors(context);
                } catch (Exception e) {
                    mClient.log(e, null);
                }

                if (mCanceled) {
                    return;
                }

                sourceContents = context.getSourceContents(false/*read*/);
                sourceName = classNode.name;
            }

            mOuterClasses = null;
        }
    }
}

From source file:com.android.tools.lint.psi.extract.ExtractPsi.java

License:Apache License

private void readClasses(@NonNull List<File> jars) throws IOException {
    for (File jar : jars) {
        JarInputStream zis = null;
        try {//  w w  w.jav a 2  s .co m
            FileInputStream fis = new FileInputStream(jar);
            try {
                zis = new JarInputStream(fis);
                ZipEntry entry = zis.getNextEntry();
                while (entry != null) {
                    boolean directory = entry.isDirectory();
                    String name = entry.getName();
                    if (!directory && name.endsWith(DOT_CLASS)) {
                        byte[] bytes = ByteStreams.toByteArray(zis);
                        if (bytes != null) {
                            ClassReader reader;
                            ClassNode classNode;
                            reader = new ClassReader(bytes);
                            classNode = new ClassNode();
                            reader.accept(classNode, 0 /* flags */);
                            // Classes shouldn't have been duplicated
                            //assert !mClassNodeMap.containsKey(name) : name;
                            mClassNodeMap.put(name, classNode);
                        }
                    } else if (!directory) {
                        byte[] bytes = ByteStreams.toByteArray(zis);
                        if (bytes != null) {
                            mResourceMap.put(name, bytes);
                        }
                    }
                    entry = zis.getNextEntry();
                }
            } finally {
                Closeables.close(fis, true);
            }
        } finally {
            Closeables.close(zis, false);
        }
    }
}

From source file:com.builtbroken.mc.patch.ASMUtility.java

/**
 * Starts the injection process of editing byte code
 *
 * @param bytes/*w w  w .j  av a 2s  .c  om*/
 * @return
 */
public static ClassNode startInjection(String name, byte[] bytes) {
    CoreMod.logger.info("Starting injection process for " + name);
    final ClassNode node = new ClassNode();
    final ClassReader reader = new ClassReader(bytes);
    reader.accept(node, 0);
    return node;
}

From source file:com.builtbroken.profiler.asm.WorldTransformer.java

private ClassNode startInjection(byte[] bytes) {
    final ClassNode node = new ClassNode();
    final ClassReader reader = new ClassReader(bytes);
    reader.accept(node, 0);//from  w  w w. jav  a  2 s  .c o  m

    return node;
}