Search in sources :

Example 1 with JavaContext

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

the class LintDriver method checkJava.

private void checkJava(@NonNull Project project, @Nullable Project main, @NonNull List<File> sourceFolders, @NonNull List<Detector> checks) {
    JavaParser javaParser = mClient.getJavaParser(project);
    if (javaParser == null) {
        mClient.log(null, "No java parser provided to lint: not running Java checks");
        return;
    }
    assert !checks.isEmpty();
    // Gather all Java source files in a single pass; more efficient.
    List<File> sources = new ArrayList<File>(100);
    for (File folder : sourceFolders) {
        gatherJavaFiles(folder, sources);
    }
    if (!sources.isEmpty()) {
        List<JavaContext> contexts = Lists.newArrayListWithExpectedSize(sources.size());
        for (File file : sources) {
            JavaContext context = new JavaContext(this, project, main, file, javaParser);
            contexts.add(context);
        }
        visitJavaFiles(checks, javaParser, contexts);
    }
}
Also used : JavaContext(com.android.tools.klint.detector.api.JavaContext) ArrayList(java.util.ArrayList) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 2 with JavaContext

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

the class LintDriver method visitJavaFiles.

private void visitJavaFiles(@NonNull List<Detector> checks, JavaParser javaParser, List<JavaContext> contexts) {
    // Temporary: we still have some builtin checks that aren't migrated to
    // PSI. Until that's complete, remove them from the list here
    //List<Detector> scanners = checks;
    List<Detector> scanners = Lists.newArrayListWithCapacity(0);
    List<Detector> uastScanners = Lists.newArrayListWithCapacity(checks.size());
    for (Detector detector : checks) {
        if (detector instanceof Detector.JavaPsiScanner) {
            scanners.add(detector);
        } else if (detector instanceof Detector.UastScanner) {
            uastScanners.add(detector);
        }
    }
    if (!scanners.isEmpty()) {
        JavaPsiVisitor visitor = new JavaPsiVisitor(javaParser, scanners);
        visitor.prepare(contexts);
        for (JavaContext context : contexts) {
            fireEvent(EventType.SCANNING_FILE, context);
            visitor.visitFile(context);
            if (mCanceled) {
                return;
            }
        }
        visitor.dispose();
    }
    if (!uastScanners.isEmpty()) {
        final UElementVisitor uElementVisitor = new UElementVisitor(javaParser, uastScanners);
        uElementVisitor.prepare(contexts);
        for (final JavaContext context : contexts) {
            fireEvent(EventType.SCANNING_FILE, context);
            ApplicationManager.getApplication().runReadAction(new Runnable() {

                @Override
                public void run() {
                    uElementVisitor.visitFile(context);
                }
            });
            if (mCanceled) {
                return;
            }
        }
        uElementVisitor.dispose();
    }
    //noinspection ConstantConditions
    if (mRunCompatChecks) {
        // Filter the checks to only those that implement JavaScanner
        List<Detector> filtered = Lists.newArrayListWithCapacity(checks.size());
        for (Detector detector : checks) {
            if (detector instanceof Detector.JavaScanner) {
                filtered.add(detector);
            }
        }
        if (!filtered.isEmpty()) {
            List<String> detectorNames = Lists.newArrayListWithCapacity(filtered.size());
            for (Detector detector : filtered) {
                detectorNames.add(detector.getClass().getName());
            }
            Collections.sort(detectorNames);
            /* Let's not complain quite yet
                String message = String.format("Lint found one or more custom checks using its "
                        + "older Java API; these checks are still run in compatibility mode, "
                        + "but this causes duplicated parsing, and in the next version lint "
                        + "will no longer include this legacy mode. Make sure the following "
                        + "lint detectors are upgraded to the new API: %1$s",
                        Joiner.on(", ").join(detectorNames));
                JavaContext first = contexts.get(0);
                Project project = first.getProject();
                Location location = Location.create(project.getDir());
                mClient.report(first,
                        IssueRegistry.LINT_ERROR,
                        project.getConfiguration(this).getSeverity(IssueRegistry.LINT_ERROR),
                        location, message, TextFormat.RAW);
                */
            JavaVisitor oldVisitor = new JavaVisitor(javaParser, filtered);
            oldVisitor.prepare(contexts);
            for (JavaContext context : contexts) {
                fireEvent(EventType.SCANNING_FILE, context);
                oldVisitor.visitFile(context);
                if (mCanceled) {
                    return;
                }
            }
            oldVisitor.dispose();
        }
    }
}
Also used : JavaContext(com.android.tools.klint.detector.api.JavaContext) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector)

Example 3 with JavaContext

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

the class LintDriver method checkIndividualJavaFiles.

private void checkIndividualJavaFiles(@NonNull Project project, @Nullable Project main, @NonNull List<Detector> checks, @NonNull List<File> files) {
    JavaParser javaParser = mClient.getJavaParser(project);
    if (javaParser == null) {
        mClient.log(null, "No java parser provided to lint: not running Java checks");
        return;
    }
    List<JavaContext> contexts = Lists.newArrayListWithExpectedSize(files.size());
    for (File file : files) {
        if (file.isFile() && file.getPath().endsWith(".kt")) {
            contexts.add(new JavaContext(this, project, main, file, javaParser));
        }
    }
    if (contexts.isEmpty()) {
        return;
    }
    visitJavaFiles(checks, javaParser, contexts);
}
Also used : JavaContext(com.android.tools.klint.detector.api.JavaContext) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Aggregations

JavaContext (com.android.tools.klint.detector.api.JavaContext)3 PsiFile (com.intellij.psi.PsiFile)2 File (java.io.File)2 Detector (com.android.tools.klint.detector.api.Detector)1 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)1 ArrayList (java.util.ArrayList)1