Search in sources :

Example 11 with Project

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

the class RtlDetector method rtlApplies.

private boolean rtlApplies(@NonNull Context context) {
    Project project = context.getMainProject();
    if (project.getTargetSdk() < RTL_API) {
        return false;
    }
    int buildTarget = project.getBuildSdk();
    if (buildTarget != -1 && buildTarget < RTL_API) {
        return false;
    }
    //noinspection RedundantIfStatement
    if (mEnabledRtlSupport != null && !mEnabledRtlSupport) {
        return false;
    }
    return true;
}
Also used : Project(com.android.tools.klint.detector.api.Project)

Example 12 with Project

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

the class PrivateResourceDetector method visitResourceReference.

@Override
public void visitResourceReference(@NonNull JavaContext context, @Nullable UastVisitor visitor, @NonNull UElement node, @NonNull ResourceType resourceType, @NonNull String name, boolean isFramework) {
    if (context.getProject().isGradleProject() && !isFramework) {
        Project project = context.getProject();
        if (project.getGradleProjectModel() != null && project.getCurrentVariant() != null) {
            if (isPrivate(context, resourceType, name)) {
                String message = createUsageErrorMessage(context, resourceType, name);
                context.report(ISSUE, node, context.getUastLocation(node), message);
            }
        }
    }
}
Also used : Project(com.android.tools.klint.detector.api.Project)

Example 13 with Project

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

the class IntellijLintProject method create.

/** Creates a set of projects for the given IntelliJ modules */
@NonNull
public static List<Project> create(@NonNull IntellijLintClient client, @Nullable List<VirtualFile> files, @NonNull Module... modules) {
    List<Project> projects = Lists.newArrayList();
    Map<Project, Module> projectMap = Maps.newHashMap();
    Map<Module, Project> moduleMap = Maps.newHashMap();
    Map<AndroidLibrary, Project> libraryMap = Maps.newHashMap();
    if (files != null && !files.isEmpty()) {
        // Wrap list with a mutable list since we'll be removing the files as we see them
        files = Lists.newArrayList(files);
    }
    for (Module module : modules) {
        addProjects(client, module, files, moduleMap, libraryMap, projectMap, projects);
    }
    client.setModuleMap(projectMap);
    if (projects.size() > 1) {
        // Partition the projects up such that we only return projects that aren't
        // included by other projects (e.g. because they are library projects)
        Set<Project> roots = new HashSet<Project>(projects);
        for (Project project : projects) {
            roots.removeAll(project.getAllLibraries());
        }
        return Lists.newArrayList(roots);
    } else {
        return projects;
    }
}
Also used : Project(com.android.tools.klint.detector.api.Project) Module(com.intellij.openapi.module.Module) NonNull(com.android.annotations.NonNull)

Example 14 with Project

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

the class IntellijLintProject method addProjects.

/**
   * Recursively add lint projects for the given module, and any other module or library it depends on, and also
   * populate the reverse maps so we can quickly map from a lint project to a corresponding module/library (used
   * by the lint client
   */
private static void addProjects(@NonNull LintClient client, @NonNull Module module, @Nullable List<VirtualFile> files, @NonNull Map<Module, Project> moduleMap, @NonNull Map<AndroidLibrary, Project> libraryMap, @NonNull Map<Project, Module> projectMap, @NonNull List<Project> projects) {
    if (moduleMap.containsKey(module)) {
        return;
    }
    LintModuleProject project = createModuleProject(client, module);
    if (project == null) {
        // It's possible for the module to *depend* on Android code, e.g. in a Gradle
        // project there will be a top-level non-Android module
        List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(module, false);
        for (AndroidFacet dependentFacet : dependentFacets) {
            addProjects(client, dependentFacet.getModule(), files, moduleMap, libraryMap, projectMap, projects);
        }
        return;
    }
    projects.add(project);
    moduleMap.put(module, project);
    projectMap.put(project, module);
    if (processFileFilter(module, files, project)) {
        // No need to process dependencies when doing single file analysis
        return;
    }
    List<Project> dependencies = Lists.newArrayList();
    // No, this shouldn't use getAllAndroidDependencies; we may have non-Android dependencies that this won't include
    // (e.g. Java-only modules)
    List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(module, true);
    for (AndroidFacet dependentFacet : dependentFacets) {
        Project p = moduleMap.get(dependentFacet.getModule());
        if (p != null) {
            dependencies.add(p);
        } else {
            addProjects(client, dependentFacet.getModule(), files, moduleMap, libraryMap, projectMap, dependencies);
        }
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null) {
        AndroidGradleModel androidGradleModel = AndroidGradleModel.get(facet);
        if (androidGradleModel != null) {
            addGradleLibraryProjects(client, files, libraryMap, projects, facet, androidGradleModel, project, projectMap, dependencies);
        }
    }
    project.setDirectLibraries(dependencies);
}
Also used : Project(com.android.tools.klint.detector.api.Project) AndroidGradleModel(com.android.tools.idea.gradle.AndroidGradleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 15 with Project

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

the class IntellijLintProject method addGradleLibraryProjects.

/** Adds any gradle library projects to the dependency list */
private static void addGradleLibraryProjects(@NonNull LintClient client, @Nullable List<VirtualFile> files, @NonNull Map<AndroidLibrary, Project> libraryMap, @NonNull List<Project> projects, @NonNull AndroidFacet facet, @NonNull AndroidGradleModel androidGradleModel, @NonNull LintModuleProject project, @NonNull Map<Project, Module> projectMap, @NonNull List<Project> dependencies) {
    Collection<AndroidLibrary> libraries = androidGradleModel.getMainArtifact().getDependencies().getLibraries();
    for (AndroidLibrary library : libraries) {
        Project p = libraryMap.get(library);
        if (p == null) {
            File dir = library.getFolder();
            p = new LintGradleLibraryProject(client, dir, dir, library);
            libraryMap.put(library, p);
            projectMap.put(p, facet.getModule());
            projects.add(p);
            if (files != null) {
                VirtualFile libraryDir = LocalFileSystem.getInstance().findFileByIoFile(dir);
                if (libraryDir != null) {
                    ListIterator<VirtualFile> iterator = files.listIterator();
                    while (iterator.hasNext()) {
                        VirtualFile file = iterator.next();
                        if (VfsUtilCore.isAncestor(libraryDir, file, false)) {
                            project.addFile(VfsUtilCore.virtualToIoFile(file));
                            iterator.remove();
                        }
                    }
                }
                if (files.isEmpty()) {
                    // No more work in other modules
                    files = null;
                }
            }
        }
        dependencies.add(p);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.android.tools.klint.detector.api.Project) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

Project (com.android.tools.klint.detector.api.Project)17 File (java.io.File)8 PsiFile (com.intellij.psi.PsiFile)4 IOException (java.io.IOException)4 NonNull (com.android.annotations.NonNull)3 Module (com.intellij.openapi.module.Module)3 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)2 ResourceItem (com.android.ide.common.res2.ResourceItem)2 LintClient (com.android.tools.klint.client.api.LintClient)2 ClassContext (com.android.tools.klint.detector.api.ClassContext)2 Context (com.android.tools.klint.detector.api.Context)2 JavaContext (com.android.tools.klint.detector.api.JavaContext)2 Location (com.android.tools.klint.detector.api.Location)2 ResourceContext (com.android.tools.klint.detector.api.ResourceContext)2 XmlContext (com.android.tools.klint.detector.api.XmlContext)2 HashSet (java.util.HashSet)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)1 ResourceFile (com.android.ide.common.res2.ResourceFile)1 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1