Search in sources :

Example 1 with LintIdeClient

use of com.android.tools.idea.lint.LintIdeClient in project android by JetBrains.

the class TemplateTest method assertLintsCleanly.

private static void assertLintsCleanly(@NotNull Project project, @NotNull Severity maxSeverity, @NotNull Set<Issue> ignored) throws Exception {
    BuiltinIssueRegistry registry = new LintIdeIssueRegistry();
    Map<Issue, Map<File, List<ProblemData>>> map = new HashMap<>();
    LintIdeClient client = LintIdeClient.forBatch(project, map, new AnalysisScope(project), registry.getIssues());
    LintDriver driver = new LintDriver(registry, client);
    List<Module> modules = Arrays.asList(ModuleManager.getInstance(project).getModules());
    LintRequest request = new LintIdeRequest(client, project, null, modules, false);
    EnumSet<Scope> scope = EnumSet.allOf(Scope.class);
    scope.remove(Scope.CLASS_FILE);
    scope.remove(Scope.ALL_CLASS_FILES);
    scope.remove(Scope.JAVA_LIBRARIES);
    request.setScope(scope);
    driver.analyze(request);
    if (!map.isEmpty()) {
        for (Map<File, List<ProblemData>> fileListMap : map.values()) {
            for (Map.Entry<File, List<ProblemData>> entry : fileListMap.entrySet()) {
                File file = entry.getKey();
                List<ProblemData> problems = entry.getValue();
                for (ProblemData problem : problems) {
                    Issue issue = problem.getIssue();
                    if (ignored.contains(issue)) {
                        continue;
                    }
                    if (issue.getDefaultSeverity().compareTo(maxSeverity) < 0) {
                        fail("Found lint issue " + issue.getId() + " with severity " + issue.getDefaultSeverity() + " in " + file + " at " + problem.getTextRange() + ": " + problem.getMessage());
                    }
                }
            }
        }
    }
}
Also used : Issue(com.android.tools.lint.detector.api.Issue) LintIdeClient(com.android.tools.idea.lint.LintIdeClient) ProblemData(org.jetbrains.android.inspections.lint.ProblemData) AnalysisScope(com.intellij.analysis.AnalysisScope) LintRequest(com.android.tools.lint.client.api.LintRequest) Scope(com.android.tools.lint.detector.api.Scope) AnalysisScope(com.intellij.analysis.AnalysisScope) BuiltinIssueRegistry(com.android.tools.lint.checks.BuiltinIssueRegistry) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) LintIdeIssueRegistry(com.android.tools.idea.lint.LintIdeIssueRegistry) LintIdeRequest(com.android.tools.idea.lint.LintIdeRequest) LintDriver(com.android.tools.lint.client.api.LintDriver)

Example 2 with LintIdeClient

use of com.android.tools.idea.lint.LintIdeClient in project android by JetBrains.

the class RepositoryUrlManager method resolveDynamicCoordinateVersion.

@Nullable
@VisibleForTesting
String resolveDynamicCoordinateVersion(@NotNull GradleCoordinate coordinate, @Nullable Project project, @NotNull AndroidSdkHandler sdkHandler) {
    if (coordinate.getGroupId() == null || coordinate.getArtifactId() == null) {
        return null;
    }
    String filter = coordinate.getRevision();
    if (!filter.endsWith("+")) {
        // Already resolved. That was easy.
        return filter;
    }
    filter = filter.substring(0, filter.length() - 1);
    File sdkLocation = sdkHandler.getLocation();
    if (sdkLocation != null) {
        // If this coordinate points to an artifact in one of our repositories, mark it will a comment if they don't
        // have that repository available.
        String libraryCoordinate = getLibraryRevision(coordinate.getGroupId(), coordinate.getArtifactId(), filter, false, sdkLocation, sdkHandler.getFileOp());
        if (libraryCoordinate != null) {
            return libraryCoordinate;
        }
        // If that didn't yield any matches, try again, this time allowing preview platforms.
        // This is necessary if the artifact filter includes enough of a version where there are
        // only preview matches.
        libraryCoordinate = getLibraryRevision(coordinate.getGroupId(), coordinate.getArtifactId(), filter, true, sdkLocation, sdkHandler.getFileOp());
        if (libraryCoordinate != null) {
            return libraryCoordinate;
        }
    }
    // Regular Gradle dependency? Look in Gradle cache
    GradleVersion versionFound = GradleLocalCache.getInstance().findLatestArtifactVersion(coordinate, project, filter);
    if (versionFound != null) {
        return versionFound.toString();
    }
    // Maybe it's available for download as an SDK component
    RemotePackage sdkPackage = SdkMavenRepository.findLatestRemoteVersion(coordinate, sdkHandler, new StudioLoggerProgressIndicator(getClass()));
    if (sdkPackage != null) {
        GradleCoordinate found = SdkMavenRepository.getCoordinateFromSdkPath(sdkPackage.getPath());
        if (found != null) {
            return found.getRevision();
        }
    }
    // Perform network lookup to resolve current best version, if possible
    if (project != null) {
        LintClient client = new LintIdeClient(project);
        Revision latest = GradleDetector.getLatestVersionFromRemoteRepo(client, coordinate, coordinate.isPreview());
        if (latest != null) {
            String version = latest.toShortString();
            if (version.startsWith(filter)) {
                return version;
            }
        }
    }
    return null;
}
Also used : StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) Revision(com.android.repository.Revision) LintClient(com.android.tools.lint.client.api.LintClient) LintIdeClient(com.android.tools.idea.lint.LintIdeClient) GradleVersion(com.android.ide.common.repository.GradleVersion) File(java.io.File) RemotePackage(com.android.repository.api.RemotePackage) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with LintIdeClient

use of com.android.tools.idea.lint.LintIdeClient in project android by JetBrains.

the class UnusedResourcesProcessor method computeUnusedMap.

@NotNull
private Map<Issue, Map<File, List<ProblemData>>> computeUnusedMap() {
    Map<Issue, Map<File, List<ProblemData>>> map = Maps.newHashMap();
    List<Issue> issues = Lists.newArrayListWithExpectedSize(2);
    issues.add(UnusedResourceDetector.ISSUE);
    if (myIncludeIds) {
        issues.add(UnusedResourceDetector.ISSUE_IDS);
    }
    AnalysisScope scope = new AnalysisScope(myProject);
    boolean unusedWasEnabled = UnusedResourceDetector.ISSUE.isEnabledByDefault();
    boolean unusedIdsWasEnabled = UnusedResourceDetector.ISSUE_IDS.isEnabledByDefault();
    UnusedResourceDetector.ISSUE.setEnabledByDefault(true);
    UnusedResourceDetector.ISSUE_IDS.setEnabledByDefault(myIncludeIds);
    try {
        LintIdeClient client = LintIdeClient.forBatch(myProject, map, scope, issues);
        LintRequest request = new LintIdeRequest(client, myProject, null, Arrays.asList(myModules), false);
        request.setScope(Scope.ALL);
        LintDriver lint = new LintDriver(new LintIdeIssueRegistry(), client);
        lint.analyze(request);
    } finally {
        UnusedResourceDetector.ISSUE.setEnabledByDefault(unusedWasEnabled);
        UnusedResourceDetector.ISSUE_IDS.setEnabledByDefault(unusedIdsWasEnabled);
    }
    return map;
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) LintRequest(com.android.tools.lint.client.api.LintRequest) Issue(com.android.tools.lint.detector.api.Issue) LintIdeClient(com.android.tools.idea.lint.LintIdeClient) Map(java.util.Map) LintIdeRequest(com.android.tools.idea.lint.LintIdeRequest) LintIdeIssueRegistry(com.android.tools.idea.lint.LintIdeIssueRegistry) ProblemData(org.jetbrains.android.inspections.lint.ProblemData) LintDriver(com.android.tools.lint.client.api.LintDriver) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LintIdeClient (com.android.tools.idea.lint.LintIdeClient)3 LintIdeIssueRegistry (com.android.tools.idea.lint.LintIdeIssueRegistry)2 LintIdeRequest (com.android.tools.idea.lint.LintIdeRequest)2 LintDriver (com.android.tools.lint.client.api.LintDriver)2 LintRequest (com.android.tools.lint.client.api.LintRequest)2 Issue (com.android.tools.lint.detector.api.Issue)2 AnalysisScope (com.intellij.analysis.AnalysisScope)2 File (java.io.File)2 ProblemData (org.jetbrains.android.inspections.lint.ProblemData)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)1 GradleVersion (com.android.ide.common.repository.GradleVersion)1 Revision (com.android.repository.Revision)1 RemotePackage (com.android.repository.api.RemotePackage)1 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)1 BuiltinIssueRegistry (com.android.tools.lint.checks.BuiltinIssueRegistry)1 LintClient (com.android.tools.lint.client.api.LintClient)1 Scope (com.android.tools.lint.detector.api.Scope)1 Module (com.intellij.openapi.module.Module)1 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)1