Search in sources :

Example 1 with ResourceXmlDetector

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

the class LintDriver method getVisitor.

@Nullable
private ResourceVisitor getVisitor(@NonNull ResourceFolderType type, @NonNull List<ResourceXmlDetector> checks, @Nullable List<Detector> binaryChecks) {
    if (type != mCurrentFolderType) {
        mCurrentFolderType = type;
        // Determine which XML resource detectors apply to the given folder type
        List<ResourceXmlDetector> applicableXmlChecks = new ArrayList<ResourceXmlDetector>(checks.size());
        for (ResourceXmlDetector check : checks) {
            if (check.appliesTo(type)) {
                applicableXmlChecks.add(check);
            }
        }
        List<Detector> applicableBinaryChecks = null;
        if (binaryChecks != null) {
            applicableBinaryChecks = new ArrayList<Detector>(binaryChecks.size());
            for (Detector check : binaryChecks) {
                if (check.appliesTo(type)) {
                    applicableBinaryChecks.add(check);
                }
            }
        }
        // If the list of detectors hasn't changed, then just use the current visitor!
        if (mCurrentXmlDetectors != null && mCurrentXmlDetectors.equals(applicableXmlChecks) && Objects.equal(mCurrentBinaryDetectors, applicableBinaryChecks)) {
            return mCurrentVisitor;
        }
        mCurrentXmlDetectors = applicableXmlChecks;
        mCurrentBinaryDetectors = applicableBinaryChecks;
        if (applicableXmlChecks.isEmpty() && (applicableBinaryChecks == null || applicableBinaryChecks.isEmpty())) {
            mCurrentVisitor = null;
            return null;
        }
        XmlParser parser = mClient.getXmlParser();
        if (parser != null) {
            mCurrentVisitor = new ResourceVisitor(parser, applicableXmlChecks, applicableBinaryChecks);
        } else {
            mCurrentVisitor = null;
        }
    }
    return mCurrentVisitor;
}
Also used : ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) ArrayList(java.util.ArrayList) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Nullable(com.android.annotations.Nullable)

Example 2 with ResourceXmlDetector

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

the class LintDriver method validateScopeList.

/** Development diagnostics only, run with assertions on */
// Turn off warnings for the intentional assertion side effect below
@SuppressWarnings("all")
private void validateScopeList() {
    boolean assertionsEnabled = false;
    // Intentional side-effect
    assert assertionsEnabled = true;
    if (assertionsEnabled) {
        List<Detector> resourceFileDetectors = mScopeDetectors.get(Scope.RESOURCE_FILE);
        if (resourceFileDetectors != null) {
            for (Detector detector : resourceFileDetectors) {
                // This is wrong; it should allow XmlScanner instead of ResourceXmlScanner!
                assert detector instanceof ResourceXmlDetector : detector;
            }
        }
        List<Detector> manifestDetectors = mScopeDetectors.get(Scope.MANIFEST);
        if (manifestDetectors != null) {
            for (Detector detector : manifestDetectors) {
                assert detector instanceof Detector.XmlScanner : detector;
            }
        }
        List<Detector> javaCodeDetectors = mScopeDetectors.get(Scope.ALL_JAVA_FILES);
        if (javaCodeDetectors != null) {
            for (Detector detector : javaCodeDetectors) {
                assert detector instanceof Detector.JavaScanner || // TODO: Migrate all
                detector instanceof Detector.UastScanner || detector instanceof Detector.JavaPsiScanner : detector;
            }
        }
        List<Detector> javaFileDetectors = mScopeDetectors.get(Scope.JAVA_FILE);
        if (javaFileDetectors != null) {
            for (Detector detector : javaFileDetectors) {
                assert detector instanceof Detector.JavaScanner || // TODO: Migrate all
                detector instanceof Detector.UastScanner || detector instanceof Detector.JavaPsiScanner : detector;
            }
        }
        List<Detector> classDetectors = mScopeDetectors.get(Scope.CLASS_FILE);
        if (classDetectors != null) {
            for (Detector detector : classDetectors) {
                assert detector instanceof Detector.ClassScanner : detector;
            }
        }
        List<Detector> classCodeDetectors = mScopeDetectors.get(Scope.ALL_CLASS_FILES);
        if (classCodeDetectors != null) {
            for (Detector detector : classCodeDetectors) {
                assert detector instanceof Detector.ClassScanner : detector;
            }
        }
        List<Detector> gradleDetectors = mScopeDetectors.get(Scope.GRADLE_FILE);
        if (gradleDetectors != null) {
            for (Detector detector : gradleDetectors) {
                assert detector instanceof Detector.GradleScanner : detector;
            }
        }
        List<Detector> otherDetectors = mScopeDetectors.get(Scope.OTHER);
        if (otherDetectors != null) {
            for (Detector detector : otherDetectors) {
                assert detector instanceof Detector.OtherFileScanner : detector;
            }
        }
        List<Detector> dirDetectors = mScopeDetectors.get(Scope.RESOURCE_FOLDER);
        if (dirDetectors != null) {
            for (Detector detector : dirDetectors) {
                assert detector instanceof Detector.ResourceFolderScanner : detector;
            }
        }
        List<Detector> binaryDetectors = mScopeDetectors.get(Scope.BINARY_RESOURCE_FILE);
        if (binaryDetectors != null) {
            for (Detector detector : binaryDetectors) {
                assert detector instanceof Detector.BinaryResourceScanner : detector;
            }
        }
    }
}
Also used : ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector)

Example 3 with ResourceXmlDetector

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

the class LintDriver method checkResourceFolder.

private void checkResourceFolder(@NonNull Project project, @Nullable Project main, @NonNull File dir, @NonNull ResourceFolderType type, @NonNull List<ResourceXmlDetector> xmlChecks, @Nullable List<Detector> dirChecks, @Nullable List<Detector> binaryChecks) {
    if (dirChecks != null && !dirChecks.isEmpty()) {
        ResourceContext context = new ResourceContext(this, project, main, dir, type);
        String folderName = dir.getName();
        fireEvent(EventType.SCANNING_FILE, context);
        for (Detector check : dirChecks) {
            if (check.appliesTo(type)) {
                check.beforeCheckFile(context);
                check.checkFolder(context, folderName);
                check.afterCheckFile(context);
            }
        }
        if (binaryChecks == null && xmlChecks.isEmpty()) {
            return;
        }
    }
    File[] files = dir.listFiles();
    if (files == null || files.length <= 0) {
        return;
    }
    ResourceVisitor visitor = getVisitor(type, xmlChecks, binaryChecks);
    if (visitor != null) {
        // if not, there are no applicable rules in this folder
        // Process files in alphabetical order, to ensure stable output
        // (for example for the duplicate resource detector)
        Arrays.sort(files);
        for (File file : files) {
            if (LintUtils.isXmlFile(file)) {
                XmlContext context = new XmlContext(this, project, main, file, type, visitor.getParser());
                fireEvent(EventType.SCANNING_FILE, context);
                visitor.visitFile(context, file);
            } else if (binaryChecks != null && (LintUtils.isBitmapFile(file) || type == ResourceFolderType.RAW)) {
                ResourceContext context = new ResourceContext(this, project, main, file, type);
                fireEvent(EventType.SCANNING_FILE, context);
                visitor.visitBinaryResource(context);
            }
            if (mCanceled) {
                return;
            }
        }
    }
}
Also used : ResourceContext(com.android.tools.klint.detector.api.ResourceContext) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) XmlContext(com.android.tools.klint.detector.api.XmlContext) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 4 with ResourceXmlDetector

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

the class LintDriver method runFileDetectors.

private void runFileDetectors(@NonNull Project project, @Nullable Project main) {
    // Look up manifest information (but not for library projects)
    if (project.isAndroidProject()) {
        for (File manifestFile : project.getManifestFiles()) {
            XmlParser parser = mClient.getXmlParser();
            if (parser != null) {
                XmlContext context = new XmlContext(this, project, main, manifestFile, null, parser);
                context.document = parser.parseXml(context);
                if (context.document != null) {
                    try {
                        project.readManifest(context.document);
                        if ((!project.isLibrary() || (main != null && main.isMergingManifests())) && mScope.contains(Scope.MANIFEST)) {
                            List<Detector> detectors = mScopeDetectors.get(Scope.MANIFEST);
                            if (detectors != null) {
                                ResourceVisitor v = new ResourceVisitor(parser, detectors, null);
                                fireEvent(EventType.SCANNING_FILE, context);
                                v.visitFile(context, manifestFile);
                            }
                        }
                    } finally {
                        if (context.document != null) {
                            // else: freed by XmlVisitor above
                            parser.dispose(context, context.document);
                        }
                    }
                }
            }
        }
        // in a single pass through the resource directories.
        if (mScope.contains(Scope.ALL_RESOURCE_FILES) || mScope.contains(Scope.RESOURCE_FILE) || mScope.contains(Scope.RESOURCE_FOLDER) || mScope.contains(Scope.BINARY_RESOURCE_FILE)) {
            List<Detector> dirChecks = mScopeDetectors.get(Scope.RESOURCE_FOLDER);
            List<Detector> binaryChecks = mScopeDetectors.get(Scope.BINARY_RESOURCE_FILE);
            List<Detector> checks = union(mScopeDetectors.get(Scope.RESOURCE_FILE), mScopeDetectors.get(Scope.ALL_RESOURCE_FILES));
            boolean haveXmlChecks = checks != null && !checks.isEmpty();
            List<ResourceXmlDetector> xmlDetectors;
            if (haveXmlChecks) {
                xmlDetectors = new ArrayList<ResourceXmlDetector>(checks.size());
                for (Detector detector : checks) {
                    if (detector instanceof ResourceXmlDetector) {
                        xmlDetectors.add((ResourceXmlDetector) detector);
                    }
                }
                haveXmlChecks = !xmlDetectors.isEmpty();
            } else {
                xmlDetectors = Collections.emptyList();
            }
            if (haveXmlChecks || dirChecks != null && !dirChecks.isEmpty() || binaryChecks != null && !binaryChecks.isEmpty()) {
                List<File> files = project.getSubset();
                if (files != null) {
                    checkIndividualResources(project, main, xmlDetectors, dirChecks, binaryChecks, files);
                } else {
                    List<File> resourceFolders = project.getResourceFolders();
                    if (!resourceFolders.isEmpty()) {
                        for (File res : resourceFolders) {
                            checkResFolder(project, main, res, xmlDetectors, dirChecks, binaryChecks);
                        }
                    }
                }
            }
        }
        if (mCanceled) {
            return;
        }
    }
    if (mScope.contains(Scope.JAVA_FILE) || mScope.contains(Scope.ALL_JAVA_FILES)) {
        List<Detector> checks = union(mScopeDetectors.get(Scope.JAVA_FILE), mScopeDetectors.get(Scope.ALL_JAVA_FILES));
        if (checks != null && !checks.isEmpty()) {
            List<File> files = project.getSubset();
            if (files != null) {
                checkIndividualJavaFiles(project, main, checks, files);
            } else {
                List<File> sourceFolders = project.getJavaSourceFolders();
                if (mScope.contains(Scope.TEST_SOURCES)) {
                    List<File> testFolders = project.getTestSourceFolders();
                    if (!testFolders.isEmpty()) {
                        List<File> combined = Lists.newArrayListWithExpectedSize(sourceFolders.size() + testFolders.size());
                        combined.addAll(sourceFolders);
                        combined.addAll(testFolders);
                        sourceFolders = combined;
                    }
                }
                checkJava(project, main, sourceFolders, checks);
            }
        }
    }
    if (mCanceled) {
        return;
    }
    if (mScope.contains(Scope.CLASS_FILE) || mScope.contains(Scope.ALL_CLASS_FILES) || mScope.contains(Scope.JAVA_LIBRARIES)) {
        checkClasses(project, main);
    }
    if (mCanceled) {
        return;
    }
    if (mScope.contains(Scope.GRADLE_FILE)) {
        checkBuildScripts(project, main);
    }
    if (mCanceled) {
        return;
    }
    if (mScope.contains(Scope.OTHER)) {
        List<Detector> checks = mScopeDetectors.get(Scope.OTHER);
        if (checks != null) {
            OtherFileVisitor visitor = new OtherFileVisitor(checks);
            visitor.scan(this, project, main);
        }
    }
    if (mCanceled) {
        return;
    }
    if (project == main && mScope.contains(Scope.PROGUARD_FILE) && project.isAndroidProject()) {
        checkProGuard(project, main);
    }
    if (project == main && mScope.contains(Scope.PROPERTY_FILE)) {
        checkProperties(project, main);
    }
}
Also used : ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) XmlContext(com.android.tools.klint.detector.api.XmlContext) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Aggregations

Detector (com.android.tools.klint.detector.api.Detector)4 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)4 XmlContext (com.android.tools.klint.detector.api.XmlContext)2 PsiFile (com.intellij.psi.PsiFile)2 File (java.io.File)2 Nullable (com.android.annotations.Nullable)1 ResourceContext (com.android.tools.klint.detector.api.ResourceContext)1 ArrayList (java.util.ArrayList)1