Search in sources :

Example 1 with ClassContext

use of com.android.tools.klint.detector.api.ClassContext in project kotlin by JetBrains.

the class LintDriver method runClassDetectors.

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;
            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);
                } 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);
                sourceName = classNode.name;
            }
            mOuterClasses = null;
        }
    }
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) ClassContext(com.android.tools.klint.detector.api.ClassContext) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) IOException(java.io.IOException)

Aggregations

ClassContext (com.android.tools.klint.detector.api.ClassContext)1 Detector (com.android.tools.klint.detector.api.Detector)1 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)1 IOException (java.io.IOException)1 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)1 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)1