Search in sources :

Example 6 with Detector

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

the class IssueRegistry method createDetectors.

/**
     * Creates a list of detectors applicable to the given scope, and with the
     * given configuration.
     *
     * @param client the client to report errors to
     * @param configuration the configuration to look up which issues are
     *            enabled etc from
     * @param scope the scope for the analysis, to filter out detectors that
     *            require wider analysis than is currently being performed
     * @param scopeToDetectors an optional map which (if not null) will be
     *            filled by this method to contain mappings from each scope to
     *            the applicable detectors for that scope
     * @return a list of new detector instances
     */
@NonNull
final List<? extends Detector> createDetectors(@NonNull LintClient client, @NonNull Configuration configuration, @NonNull EnumSet<Scope> scope, @Nullable Map<Scope, List<Detector>> scopeToDetectors) {
    List<Issue> issues = getIssuesForScope(scope);
    if (issues.isEmpty()) {
        return Collections.emptyList();
    }
    Set<Class<? extends Detector>> detectorClasses = new HashSet<Class<? extends Detector>>();
    Map<Class<? extends Detector>, EnumSet<Scope>> detectorToScope = new HashMap<Class<? extends Detector>, EnumSet<Scope>>();
    for (Issue issue : issues) {
        Implementation implementation = issue.getImplementation();
        Class<? extends Detector> detectorClass = implementation.getDetectorClass();
        EnumSet<Scope> issueScope = implementation.getScope();
        if (!detectorClasses.contains(detectorClass)) {
            // Determine if the issue is enabled
            if (!configuration.isEnabled(issue)) {
                continue;
            }
            // Ensured by getIssuesForScope above
            assert implementation.isAdequate(scope);
            detectorClass = client.replaceDetector(detectorClass);
            assert detectorClass != null : issue.getId();
            detectorClasses.add(detectorClass);
        }
        if (scopeToDetectors != null) {
            EnumSet<Scope> s = detectorToScope.get(detectorClass);
            if (s == null) {
                detectorToScope.put(detectorClass, issueScope);
            } else if (!s.containsAll(issueScope)) {
                EnumSet<Scope> union = EnumSet.copyOf(s);
                union.addAll(issueScope);
                detectorToScope.put(detectorClass, union);
            }
        }
    }
    List<Detector> detectors = new ArrayList<Detector>(detectorClasses.size());
    for (Class<? extends Detector> clz : detectorClasses) {
        try {
            Detector detector = clz.newInstance();
            detectors.add(detector);
            if (scopeToDetectors != null) {
                EnumSet<Scope> union = detectorToScope.get(clz);
                for (Scope s : union) {
                    List<Detector> list = scopeToDetectors.get(s);
                    if (list == null) {
                        list = new ArrayList<Detector>();
                        scopeToDetectors.put(s, list);
                    }
                    list.add(detector);
                }
            }
        } catch (Throwable t) {
            //$NON-NLS-1$
            client.log(t, "Can't initialize detector %1$s", clz.getName());
        }
    }
    return detectors;
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) HashMap(java.util.HashMap) EnumSet(java.util.EnumSet) ArrayList(java.util.ArrayList) Implementation(com.android.tools.klint.detector.api.Implementation) Detector(com.android.tools.klint.detector.api.Detector) Scope(com.android.tools.klint.detector.api.Scope) HashSet(java.util.HashSet) NonNull(com.android.annotations.NonNull)

Example 7 with Detector

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

the class LintDriver method getVisitor.

@Nullable
private ResourceVisitor getVisitor(@NonNull ResourceFolderType type, @NonNull List<ResourceXmlDetector> checks, @Nullable List<Detector> binaryChecks) {
    if (type != mCurrentFolderType) {
        mCurrentFolderType = type;
        // Determine which XML resource detectors apply to the given folder type
        List<ResourceXmlDetector> applicableXmlChecks = new ArrayList<ResourceXmlDetector>(checks.size());
        for (ResourceXmlDetector check : checks) {
            if (check.appliesTo(type)) {
                applicableXmlChecks.add(check);
            }
        }
        List<Detector> applicableBinaryChecks = null;
        if (binaryChecks != null) {
            applicableBinaryChecks = new ArrayList<Detector>(binaryChecks.size());
            for (Detector check : binaryChecks) {
                if (check.appliesTo(type)) {
                    applicableBinaryChecks.add(check);
                }
            }
        }
        // If the list of detectors hasn't changed, then just use the current visitor!
        if (mCurrentXmlDetectors != null && mCurrentXmlDetectors.equals(applicableXmlChecks) && Objects.equal(mCurrentBinaryDetectors, applicableBinaryChecks)) {
            return mCurrentVisitor;
        }
        mCurrentXmlDetectors = applicableXmlChecks;
        mCurrentBinaryDetectors = applicableBinaryChecks;
        if (applicableXmlChecks.isEmpty() && (applicableBinaryChecks == null || applicableBinaryChecks.isEmpty())) {
            mCurrentVisitor = null;
            return null;
        }
        XmlParser parser = mClient.getXmlParser();
        if (parser != null) {
            mCurrentVisitor = new ResourceVisitor(parser, applicableXmlChecks, applicableBinaryChecks);
        } else {
            mCurrentVisitor = null;
        }
    }
    return mCurrentVisitor;
}
Also used : ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) ArrayList(java.util.ArrayList) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Nullable(com.android.annotations.Nullable)

Example 8 with Detector

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

the class ResourceVisitor method visitBinaryResource.

public void visitBinaryResource(@NonNull ResourceContext context) {
    if (mBinaryDetectors == null) {
        return;
    }
    for (Detector check : mBinaryDetectors) {
        check.beforeCheckFile(context);
        check.checkBinaryResource(context);
        check.afterCheckFile(context);
    }
}
Also used : Detector(com.android.tools.klint.detector.api.Detector)

Example 9 with Detector

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

the class AsmVisitor method runClassDetectors.

// ASM API uses raw types
@SuppressWarnings("rawtypes")
void runClassDetectors(ClassContext context) {
    ClassNode classNode = context.getClassNode();
    for (Detector detector : mAllDetectors) {
        detector.beforeCheckFile(context);
    }
    for (Detector detector : mFullClassChecks) {
        Detector.ClassScanner scanner = (Detector.ClassScanner) detector;
        scanner.checkClass(context, classNode);
        detector.afterCheckFile(context);
    }
    if (!mMethodNameToChecks.isEmpty() || !mMethodOwnerToChecks.isEmpty() || mNodeTypeDetectors != null && mNodeTypeDetectors.length > 0) {
        List methodList = classNode.methods;
        for (Object m : methodList) {
            MethodNode method = (MethodNode) m;
            InsnList nodes = method.instructions;
            for (int i = 0, n = nodes.size(); i < n; i++) {
                AbstractInsnNode instruction = nodes.get(i);
                int type = instruction.getType();
                if (type == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode call = (MethodInsnNode) instruction;
                    String owner = call.owner;
                    List<ClassScanner> scanners = mMethodOwnerToChecks.get(owner);
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkCall(context, classNode, method, call);
                        }
                    }
                    String name = call.name;
                    scanners = mMethodNameToChecks.get(name);
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkCall(context, classNode, method, call);
                        }
                    }
                }
                if (mNodeTypeDetectors != null && type < mNodeTypeDetectors.length) {
                    List<ClassScanner> scanners = mNodeTypeDetectors[type];
                    if (scanners != null) {
                        for (ClassScanner scanner : scanners) {
                            scanner.checkInstruction(context, classNode, method, instruction);
                        }
                    }
                }
            }
        }
    }
    for (Detector detector : mAllDetectors) {
        detector.afterCheckFile(context);
    }
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) ClassScanner(com.android.tools.klint.detector.api.Detector.ClassScanner) InsnList(org.jetbrains.org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode) Detector(com.android.tools.klint.detector.api.Detector) ClassScanner(com.android.tools.klint.detector.api.Detector.ClassScanner) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.jetbrains.org.objectweb.asm.tree.MethodInsnNode) ArrayList(java.util.ArrayList) List(java.util.List) InsnList(org.jetbrains.org.objectweb.asm.tree.InsnList)

Example 10 with Detector

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

the class LintDriver method validateScopeList.

/** Development diagnostics only, run with assertions on */
// Turn off warnings for the intentional assertion side effect below
@SuppressWarnings("all")
private void validateScopeList() {
    boolean assertionsEnabled = false;
    // Intentional side-effect
    assert assertionsEnabled = true;
    if (assertionsEnabled) {
        List<Detector> resourceFileDetectors = mScopeDetectors.get(Scope.RESOURCE_FILE);
        if (resourceFileDetectors != null) {
            for (Detector detector : resourceFileDetectors) {
                // This is wrong; it should allow XmlScanner instead of ResourceXmlScanner!
                assert detector instanceof ResourceXmlDetector : detector;
            }
        }
        List<Detector> manifestDetectors = mScopeDetectors.get(Scope.MANIFEST);
        if (manifestDetectors != null) {
            for (Detector detector : manifestDetectors) {
                assert detector instanceof Detector.XmlScanner : detector;
            }
        }
        List<Detector> javaCodeDetectors = mScopeDetectors.get(Scope.ALL_JAVA_FILES);
        if (javaCodeDetectors != null) {
            for (Detector detector : javaCodeDetectors) {
                assert detector instanceof Detector.JavaScanner || // TODO: Migrate all
                detector instanceof Detector.UastScanner || detector instanceof Detector.JavaPsiScanner : detector;
            }
        }
        List<Detector> javaFileDetectors = mScopeDetectors.get(Scope.JAVA_FILE);
        if (javaFileDetectors != null) {
            for (Detector detector : javaFileDetectors) {
                assert detector instanceof Detector.JavaScanner || // TODO: Migrate all
                detector instanceof Detector.UastScanner || detector instanceof Detector.JavaPsiScanner : detector;
            }
        }
        List<Detector> classDetectors = mScopeDetectors.get(Scope.CLASS_FILE);
        if (classDetectors != null) {
            for (Detector detector : classDetectors) {
                assert detector instanceof Detector.ClassScanner : detector;
            }
        }
        List<Detector> classCodeDetectors = mScopeDetectors.get(Scope.ALL_CLASS_FILES);
        if (classCodeDetectors != null) {
            for (Detector detector : classCodeDetectors) {
                assert detector instanceof Detector.ClassScanner : detector;
            }
        }
        List<Detector> gradleDetectors = mScopeDetectors.get(Scope.GRADLE_FILE);
        if (gradleDetectors != null) {
            for (Detector detector : gradleDetectors) {
                assert detector instanceof Detector.GradleScanner : detector;
            }
        }
        List<Detector> otherDetectors = mScopeDetectors.get(Scope.OTHER);
        if (otherDetectors != null) {
            for (Detector detector : otherDetectors) {
                assert detector instanceof Detector.OtherFileScanner : detector;
            }
        }
        List<Detector> dirDetectors = mScopeDetectors.get(Scope.RESOURCE_FOLDER);
        if (dirDetectors != null) {
            for (Detector detector : dirDetectors) {
                assert detector instanceof Detector.ResourceFolderScanner : detector;
            }
        }
        List<Detector> binaryDetectors = mScopeDetectors.get(Scope.BINARY_RESOURCE_FILE);
        if (binaryDetectors != null) {
            for (Detector detector : binaryDetectors) {
                assert detector instanceof Detector.BinaryResourceScanner : detector;
            }
        }
    }
}
Also used : ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector)

Aggregations

Detector (com.android.tools.klint.detector.api.Detector)16 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)11 File (java.io.File)7 XmlContext (com.android.tools.klint.detector.api.XmlContext)6 PsiFile (com.intellij.psi.PsiFile)6 ClassContext (com.android.tools.klint.detector.api.ClassContext)5 Context (com.android.tools.klint.detector.api.Context)5 JavaContext (com.android.tools.klint.detector.api.JavaContext)5 ResourceContext (com.android.tools.klint.detector.api.ResourceContext)5 ArrayList (java.util.ArrayList)5 Scope (com.android.tools.klint.detector.api.Scope)3 List (java.util.List)3 Issue (com.android.tools.klint.detector.api.Issue)2 EnumMap (java.util.EnumMap)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)2 InsnList (org.jetbrains.org.objectweb.asm.tree.InsnList)2 NonNull (com.android.annotations.NonNull)1