Search in sources :

Example 1 with Issue

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

the class IssueRegistry method createCategoryList.

@NonNull
private List<Category> createCategoryList() {
    Set<Category> categorySet = Sets.newHashSetWithExpectedSize(20);
    for (Issue issue : getIssues()) {
        categorySet.add(issue.getCategory());
    }
    List<Category> sorted = new ArrayList<Category>(categorySet);
    Collections.sort(sorted);
    return sorted;
}
Also used : Category(com.android.tools.klint.detector.api.Category) Issue(com.android.tools.klint.detector.api.Issue) ArrayList(java.util.ArrayList) NonNull(com.android.annotations.NonNull)

Example 2 with Issue

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

the class IssueRegistry method getIssuesForScope.

/**
     * Returns all available issues of a given scope (regardless of whether
     * they are actually enabled for a given configuration etc)
     *
     * @param scope the applicable scope set
     * @return a list of issues
     */
@NonNull
protected List<Issue> getIssuesForScope(@NonNull EnumSet<Scope> scope) {
    List<Issue> list = sScopeIssues.get(scope);
    if (list == null) {
        List<Issue> issues = getIssues();
        if (scope.equals(Scope.ALL)) {
            list = issues;
        } else {
            list = new ArrayList<Issue>(getIssueCapacity(scope));
            for (Issue issue : issues) {
                // Determine if the scope matches
                if (issue.getImplementation().isAdequate(scope)) {
                    list.add(issue);
                }
            }
        }
        sScopeIssues.put(scope, list);
    }
    return list;
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) NonNull(com.android.annotations.NonNull)

Example 3 with Issue

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

the class LintDriver method computeRepeatingDetectors.

private void computeRepeatingDetectors(List<Detector> detectors, Project project) {
    // Ensure that the current visitor is recomputed
    mCurrentFolderType = null;
    mCurrentVisitor = null;
    mCurrentXmlDetectors = null;
    mCurrentBinaryDetectors = null;
    // Create map from detector class to issue such that we can
    // compute applicable issues for each detector in the list of detectors
    // to be repeated
    List<Issue> issues = mRegistry.getIssues();
    Multimap<Class<? extends Detector>, Issue> issueMap = ArrayListMultimap.create(issues.size(), 3);
    for (Issue issue : issues) {
        issueMap.put(issue.getImplementation().getDetectorClass(), issue);
    }
    Map<Class<? extends Detector>, EnumSet<Scope>> detectorToScope = new HashMap<Class<? extends Detector>, EnumSet<Scope>>();
    Map<Scope, List<Detector>> scopeToDetectors = new EnumMap<Scope, List<Detector>>(Scope.class);
    List<Detector> detectorList = new ArrayList<Detector>();
    // Compute the list of detectors (narrowed down from mRepeatingDetectors),
    // and simultaneously build up the detectorToScope map which tracks
    // the scopes each detector is affected by (this is used to populate
    // the mScopeDetectors map which is used during iteration).
    Configuration configuration = project.getConfiguration(this);
    for (Detector detector : detectors) {
        Class<? extends Detector> detectorClass = detector.getClass();
        Collection<Issue> detectorIssues = issueMap.get(detectorClass);
        if (detectorIssues != null) {
            boolean add = false;
            for (Issue issue : detectorIssues) {
                // the detector is enabled here.
                if (!configuration.isEnabled(issue)) {
                    continue;
                }
                // Include detector if any of its issues are enabled
                add = true;
                EnumSet<Scope> s = detectorToScope.get(detectorClass);
                EnumSet<Scope> issueScope = issue.getImplementation().getScope();
                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);
                }
            }
            if (add) {
                detectorList.add(detector);
                EnumSet<Scope> union = detectorToScope.get(detector.getClass());
                for (Scope s : union) {
                    List<Detector> list = scopeToDetectors.get(s);
                    if (list == null) {
                        list = new ArrayList<Detector>();
                        scopeToDetectors.put(s, list);
                    }
                    list.add(detector);
                }
            }
        }
    }
    mApplicableDetectors = detectorList;
    mScopeDetectors = scopeToDetectors;
    mRepeatingDetectors = null;
    mRepeatScope = null;
    validateScopeList();
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) EnumSet(java.util.EnumSet) ArrayList(java.util.ArrayList) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) Scope(com.android.tools.klint.detector.api.Scope) LintUtils.isAnonymousClass(com.android.tools.klint.detector.api.LintUtils.isAnonymousClass) PsiModifierList(com.intellij.psi.PsiModifierList) InsnList(org.jetbrains.org.objectweb.asm.tree.InsnList) ArrayList(java.util.ArrayList) PsiAnnotationParameterList(com.intellij.psi.PsiAnnotationParameterList) List(java.util.List) EnumMap(java.util.EnumMap)

Example 4 with Issue

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

the class AndroidLintGlobalInspectionContext method performPreRunActivities.

@Override
public void performPreRunActivities(@NotNull List<Tools> globalTools, @NotNull List<Tools> localTools, @NotNull final GlobalInspectionContext context) {
    final Project project = context.getProject();
    if (!ProjectFacetManager.getInstance(project).hasFacets(AndroidFacet.ID)) {
        return;
    }
    final List<Issue> issues = AndroidLintExternalAnnotator.getIssuesFromInspections(project, null);
    if (issues.size() == 0) {
        return;
    }
    final Map<Issue, Map<File, List<ProblemData>>> problemMap = new HashMap<Issue, Map<File, List<ProblemData>>>();
    final AnalysisScope scope = context.getRefManager().getScope();
    if (scope == null) {
        return;
    }
    final IntellijLintClient client = IntellijLintClient.forBatch(project, problemMap, scope, issues);
    final LintDriver lint = new LintDriver(new IntellijLintIssueRegistry(), client);
    final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    if (indicator != null) {
        ProgressWrapper.unwrap(indicator).setText("Running Android Lint");
    }
    EnumSet<Scope> lintScope;
    //noinspection ConstantConditions
    if (!IntellijLintProject.SUPPORT_CLASS_FILES) {
        lintScope = EnumSet.copyOf(Scope.ALL);
        // Can't run class file based checks
        lintScope.remove(Scope.CLASS_FILE);
        lintScope.remove(Scope.ALL_CLASS_FILES);
        lintScope.remove(Scope.JAVA_LIBRARIES);
    } else {
        lintScope = Scope.ALL;
    }
    List<VirtualFile> files = null;
    final List<Module> modules = Lists.newArrayList();
    int scopeType = scope.getScopeType();
    switch(scopeType) {
        case AnalysisScope.MODULE:
            {
                SearchScope searchScope = scope.toSearchScope();
                if (searchScope instanceof ModuleWithDependenciesScope) {
                    ModuleWithDependenciesScope s = (ModuleWithDependenciesScope) searchScope;
                    if (!s.isSearchInLibraries()) {
                        modules.add(s.getModule());
                    }
                }
                break;
            }
        case AnalysisScope.FILE:
        case AnalysisScope.VIRTUAL_FILES:
        case AnalysisScope.UNCOMMITTED_FILES:
            {
                files = Lists.newArrayList();
                SearchScope searchScope = scope.toSearchScope();
                if (searchScope instanceof LocalSearchScope) {
                    final LocalSearchScope localSearchScope = (LocalSearchScope) searchScope;
                    final PsiElement[] elements = localSearchScope.getScope();
                    final List<VirtualFile> finalFiles = files;
                    ApplicationManager.getApplication().runReadAction(new Runnable() {

                        @Override
                        public void run() {
                            for (PsiElement element : elements) {
                                if (element instanceof PsiFile) {
                                    // should be the case since scope type is FILE
                                    Module module = ModuleUtilCore.findModuleForPsiElement(element);
                                    if (module != null && !modules.contains(module)) {
                                        modules.add(module);
                                    }
                                    VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
                                    if (virtualFile != null) {
                                        finalFiles.add(virtualFile);
                                    }
                                }
                            }
                        }
                    });
                } else {
                    final List<VirtualFile> finalList = files;
                    scope.accept(new PsiElementVisitor() {

                        @Override
                        public void visitFile(PsiFile file) {
                            VirtualFile virtualFile = file.getVirtualFile();
                            if (virtualFile != null) {
                                finalList.add(virtualFile);
                            }
                        }
                    });
                }
                if (files.isEmpty()) {
                    files = null;
                } else {
                    // Lint will compute it lazily based on actual files in the request
                    lintScope = null;
                }
                break;
            }
        case AnalysisScope.PROJECT:
            {
                modules.addAll(Arrays.asList(ModuleManager.getInstance(project).getModules()));
                break;
            }
        case AnalysisScope.CUSTOM:
        case AnalysisScope.MODULES:
        case AnalysisScope.DIRECTORY:
            {
                // Handled by the getNarrowedComplementaryScope case below
                break;
            }
        case AnalysisScope.INVALID:
            break;
        default:
            Logger.getInstance(this.getClass()).warn("Unexpected inspection scope " + scope + ", " + scopeType);
    }
    if (modules.isEmpty()) {
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            if (scope.containsModule(module)) {
                modules.add(module);
            }
        }
        if (modules.isEmpty() && files != null) {
            for (VirtualFile file : files) {
                Module module = ModuleUtilCore.findModuleForFile(file, project);
                if (module != null && !modules.contains(module)) {
                    modules.add(module);
                }
            }
        }
        if (modules.isEmpty()) {
            AnalysisScope narrowed = scope.getNarrowedComplementaryScope(project);
            for (Module module : ModuleManager.getInstance(project).getModules()) {
                if (narrowed.containsModule(module)) {
                    modules.add(module);
                }
            }
        }
    }
    LintRequest request = new IntellijLintRequest(client, project, files, modules, false);
    request.setScope(lintScope);
    lint.analyze(request);
    myResults = problemMap;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Issue(com.android.tools.klint.detector.api.Issue) HashMap(com.intellij.util.containers.HashMap) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) AnalysisScope(com.intellij.analysis.AnalysisScope) LintRequest(com.android.tools.klint.client.api.LintRequest) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) LintDriver(com.android.tools.klint.client.api.LintDriver) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ModuleWithDependenciesScope(com.intellij.openapi.module.impl.scopes.ModuleWithDependenciesScope) Project(com.intellij.openapi.project.Project) ModuleWithDependenciesScope(com.intellij.openapi.module.impl.scopes.ModuleWithDependenciesScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) AnalysisScope(com.intellij.analysis.AnalysisScope) Scope(com.android.tools.klint.detector.api.Scope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) HashMap(com.intellij.util.containers.HashMap)

Example 5 with Issue

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

the class AndroidLintExternalAnnotator method getIssuesFromInspections.

@NotNull
static List<Issue> getIssuesFromInspections(@NotNull Project project, @Nullable PsiElement context) {
    final List<Issue> result = new ArrayList<Issue>();
    final IssueRegistry fullRegistry = new IntellijLintIssueRegistry();
    for (Issue issue : fullRegistry.getIssues()) {
        final String inspectionShortName = AndroidLintInspectionBase.getInspectionShortNameByIssue(project, issue);
        if (inspectionShortName == null) {
            continue;
        }
        final HighlightDisplayKey key = HighlightDisplayKey.find(inspectionShortName);
        if (key == null) {
            continue;
        }
        final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
        final boolean enabled = context != null ? profile.isToolEnabled(key, context) : profile.isToolEnabled(key);
        if (!enabled) {
            continue;
        } else if (!issue.isEnabledByDefault()) {
            // If an issue is marked as not enabled by default, lint won't run it, even if it's in the set
            // of issues provided by an issue registry. Since in the IDE we're enforcing the enabled-state via
            // inspection profiles, mark the issue as enabled to allow users to turn on a lint check directly
            // via the inspections UI.
            issue.setEnabledByDefault(true);
        }
        result.add(issue);
    }
    return result;
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ArrayList(java.util.ArrayList) IssueRegistry(com.android.tools.klint.client.api.IssueRegistry) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Issue (com.android.tools.klint.detector.api.Issue)10 NonNull (com.android.annotations.NonNull)4 ArrayList (java.util.ArrayList)4 Scope (com.android.tools.klint.detector.api.Scope)3 Project (com.intellij.openapi.project.Project)3 Detector (com.android.tools.klint.detector.api.Detector)2 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)2 Module (com.intellij.openapi.module.Module)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 BuiltinIssueRegistry (com.android.tools.klint.checks.BuiltinIssueRegistry)1 IssueRegistry (com.android.tools.klint.client.api.IssueRegistry)1 LintDriver (com.android.tools.klint.client.api.LintDriver)1 LintRequest (com.android.tools.klint.client.api.LintRequest)1 Category (com.android.tools.klint.detector.api.Category)1 Implementation (com.android.tools.klint.detector.api.Implementation)1 LintUtils.isAnonymousClass (com.android.tools.klint.detector.api.LintUtils.isAnonymousClass)1