Search in sources :

Example 1 with Implementation

use of com.android.tools.klint.detector.api.Implementation 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)

Aggregations

NonNull (com.android.annotations.NonNull)1 Detector (com.android.tools.klint.detector.api.Detector)1 Implementation (com.android.tools.klint.detector.api.Implementation)1 Issue (com.android.tools.klint.detector.api.Issue)1 Scope (com.android.tools.klint.detector.api.Scope)1 ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1