Search in sources :

Example 6 with Context

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

the class LintDriver method runExtraPhases.

private void runExtraPhases(@NonNull Project project, @NonNull Project main) {
    // Did any detectors request another phase?
    if (mRepeatingDetectors != null) {
        // Yes. Iterate up to MAX_PHASES times.
        // During the extra phases, we might be narrowing the scope, and setting it in the
        // scope field such that detectors asking about the available scope will get the
        // correct result. However, we need to restore it to the original scope when this
        // is done in case there are other projects that will be checked after this, since
        // the repeated phases is done *per project*, not after all projects have been
        // processed.
        EnumSet<Scope> oldScope = mScope;
        do {
            mPhase++;
            fireEvent(EventType.NEW_PHASE, new Context(this, project, null, project.getDir()));
            // the rules.
            if (mRepeatScope == null) {
                mRepeatScope = Scope.ALL;
            }
            mScope = Scope.intersect(mScope, mRepeatScope);
            if (mScope.isEmpty()) {
                break;
            }
            // Compute the detectors to use for this pass.
            // Unlike the normal computeDetectors(project) call,
            // this is going to use the existing instances, and include
            // those that apply for the configuration.
            computeRepeatingDetectors(mRepeatingDetectors, project);
            if (mApplicableDetectors.isEmpty()) {
                // No detectors enabled in this project: skip it
                continue;
            }
            checkProject(project, main);
            if (mCanceled) {
                break;
            }
        } while (mPhase < MAX_PHASES && mRepeatingDetectors != null);
        mScope = oldScope;
    }
}
Also used : ClassContext(com.android.tools.klint.detector.api.ClassContext) XmlContext(com.android.tools.klint.detector.api.XmlContext) JavaContext(com.android.tools.klint.detector.api.JavaContext) ResourceContext(com.android.tools.klint.detector.api.ResourceContext) Context(com.android.tools.klint.detector.api.Context) Scope(com.android.tools.klint.detector.api.Scope)

Example 7 with Context

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

the class LintDriver method checkPropertyFile.

private void checkPropertyFile(Project project, Project main, List<Detector> detectors, String relativePath) {
    File file = new File(project.getDir(), relativePath);
    if (file.exists()) {
        Context context = new Context(this, project, main, file);
        fireEvent(EventType.SCANNING_FILE, context);
        for (Detector detector : detectors) {
            if (detector.appliesTo(context, file)) {
                detector.beforeCheckFile(context);
                detector.run(context);
                detector.afterCheckFile(context);
            }
        }
    }
}
Also used : ClassContext(com.android.tools.klint.detector.api.ClassContext) XmlContext(com.android.tools.klint.detector.api.XmlContext) JavaContext(com.android.tools.klint.detector.api.JavaContext) ResourceContext(com.android.tools.klint.detector.api.ResourceContext) Context(com.android.tools.klint.detector.api.Context) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 8 with Context

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

the class LintDriver method checkProject.

private void checkProject(@NonNull Project project, @NonNull Project main) {
    File projectDir = project.getDir();
    Context projectContext = new Context(this, project, null, projectDir);
    fireEvent(EventType.SCANNING_PROJECT, projectContext);
    List<Project> allLibraries = project.getAllLibraries();
    Set<Project> allProjects = new HashSet<Project>(allLibraries.size() + 1);
    allProjects.add(project);
    allProjects.addAll(allLibraries);
    mCurrentProjects = allProjects.toArray(new Project[allProjects.size()]);
    mCurrentProject = project;
    for (Detector check : mApplicableDetectors) {
        check.beforeCheckProject(projectContext);
        if (mCanceled) {
            return;
        }
    }
    assert mCurrentProject == project;
    runFileDetectors(project, main);
    if (!Scope.checkSingleFile(mScope)) {
        List<Project> libraries = project.getAllLibraries();
        for (Project library : libraries) {
            Context libraryContext = new Context(this, library, project, projectDir);
            fireEvent(EventType.SCANNING_LIBRARY_PROJECT, libraryContext);
            mCurrentProject = library;
            for (Detector check : mApplicableDetectors) {
                check.beforeCheckLibraryProject(libraryContext);
                if (mCanceled) {
                    return;
                }
            }
            assert mCurrentProject == library;
            runFileDetectors(library, main);
            if (mCanceled) {
                return;
            }
            assert mCurrentProject == library;
            for (Detector check : mApplicableDetectors) {
                check.afterCheckLibraryProject(libraryContext);
                if (mCanceled) {
                    return;
                }
            }
        }
    }
    mCurrentProject = project;
    for (Detector check : mApplicableDetectors) {
        check.afterCheckProject(projectContext);
        if (mCanceled) {
            return;
        }
    }
    if (mCanceled) {
        mClient.report(projectContext, // Must provide an issue since API guarantees that the issue parameter
        IssueRegistry.CANCELLED, Severity.INFORMATIONAL, Location.create(project.getDir()), "Lint canceled by user", TextFormat.RAW);
    }
    mCurrentProjects = null;
}
Also used : ClassContext(com.android.tools.klint.detector.api.ClassContext) XmlContext(com.android.tools.klint.detector.api.XmlContext) JavaContext(com.android.tools.klint.detector.api.JavaContext) ResourceContext(com.android.tools.klint.detector.api.ResourceContext) Context(com.android.tools.klint.detector.api.Context) Project(com.android.tools.klint.detector.api.Project) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) HashSet(java.util.HashSet)

Example 9 with Context

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

the class LintDriver method checkClasses.

/** Check the classes in this project (and if applicable, in any library projects */
private void checkClasses(Project project, Project main) {
    List<File> files = project.getSubset();
    if (files != null) {
        checkIndividualClassFiles(project, main, files);
        return;
    }
    // We need to read in all the classes up front such that we can initialize
    // the parent chains (such that for example for a virtual dispatch, we can
    // also check the super classes).
    List<File> libraries = project.getJavaLibraries(false);
    List<ClassEntry> libraryEntries = ClassEntry.fromClassPath(mClient, libraries, true);
    List<File> classFolders = project.getJavaClassFolders();
    List<ClassEntry> classEntries;
    if (classFolders.isEmpty()) {
        String message = String.format("No `.class` files were found in project \"%1$s\", " + "so none of the classfile based checks could be run. " + "Does the project need to be built first?", project.getName());
        Location location = Location.create(project.getDir());
        mClient.report(new Context(this, project, main, project.getDir()), IssueRegistry.LINT_ERROR, project.getConfiguration(this).getSeverity(IssueRegistry.LINT_ERROR), location, message, TextFormat.RAW);
        classEntries = Collections.emptyList();
    } else {
        classEntries = ClassEntry.fromClassPath(mClient, classFolders, true);
    }
    // Actually run the detectors. Libraries should be called before the
    // main classes.
    runClassDetectors(Scope.JAVA_LIBRARIES, libraryEntries, project, main);
    if (mCanceled) {
        return;
    }
    runClassDetectors(Scope.CLASS_FILE, classEntries, project, main);
    runClassDetectors(Scope.ALL_CLASS_FILES, classEntries, project, main);
}
Also used : ClassContext(com.android.tools.klint.detector.api.ClassContext) XmlContext(com.android.tools.klint.detector.api.XmlContext) JavaContext(com.android.tools.klint.detector.api.JavaContext) ResourceContext(com.android.tools.klint.detector.api.ResourceContext) Context(com.android.tools.klint.detector.api.Context) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) Location(com.android.tools.klint.detector.api.Location)

Aggregations

Context (com.android.tools.klint.detector.api.Context)9 ClassContext (com.android.tools.klint.detector.api.ClassContext)7 JavaContext (com.android.tools.klint.detector.api.JavaContext)7 ResourceContext (com.android.tools.klint.detector.api.ResourceContext)7 XmlContext (com.android.tools.klint.detector.api.XmlContext)7 File (java.io.File)7 PsiFile (com.intellij.psi.PsiFile)6 Detector (com.android.tools.klint.detector.api.Detector)5 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)4 Location (com.android.tools.klint.detector.api.Location)2 Project (com.android.tools.klint.detector.api.Project)2 Scope (com.android.tools.klint.detector.api.Scope)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NonNull (com.android.annotations.NonNull)1 OtherFileScanner (com.android.tools.klint.detector.api.Detector.OtherFileScanner)1 EnumMap (java.util.EnumMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 NodeList (org.w3c.dom.NodeList)1