Search in sources :

Example 1 with Detector

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

the class LintDriver method checkProGuard.

private void checkProGuard(Project project, Project main) {
    List<Detector> detectors = mScopeDetectors.get(Scope.PROGUARD_FILE);
    if (detectors != null) {
        List<File> files = project.getProguardFiles();
        for (File file : files) {
            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 2 with Detector

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

the class OtherFileVisitor method scan.

/** Analyze other files in the given project */
void scan(@NonNull LintDriver driver, @NonNull Project project, @Nullable Project main) {
    // Collect all project files
    File projectFolder = project.getDir();
    EnumSet<Scope> scopes = EnumSet.noneOf(Scope.class);
    for (Detector detector : mDetectors) {
        OtherFileScanner fileScanner = (OtherFileScanner) detector;
        EnumSet<Scope> applicable = fileScanner.getApplicableFiles();
        if (applicable.contains(Scope.OTHER)) {
            scopes = Scope.ALL;
            break;
        }
        scopes.addAll(applicable);
    }
    List<File> subset = project.getSubset();
    if (scopes.contains(Scope.RESOURCE_FILE)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (SdkUtils.endsWith(file.getPath(), DOT_XML) && !file.getName().equals(ANDROID_MANIFEST_XML)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.RESOURCE_FILE, files);
            }
        } else {
            List<File> files = Lists.newArrayListWithExpectedSize(100);
            for (File res : project.getResourceFolders()) {
                collectFiles(files, res);
            }
            File assets = new File(projectFolder, FD_ASSETS);
            if (assets.exists()) {
                collectFiles(files, assets);
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.RESOURCE_FILE, files);
            }
        }
    }
    if (scopes.contains(Scope.JAVA_FILE)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (file.getPath().endsWith(".kt")) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.JAVA_FILE, files);
            }
        } else {
            List<File> files = Lists.newArrayListWithExpectedSize(100);
            for (File srcFolder : project.getJavaSourceFolders()) {
                collectFiles(files, srcFolder);
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.JAVA_FILE, files);
            }
        }
    }
    if (scopes.contains(Scope.CLASS_FILE)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (file.getPath().endsWith(DOT_CLASS)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.CLASS_FILE, files);
            }
        } else {
            List<File> files = Lists.newArrayListWithExpectedSize(100);
            for (File classFolder : project.getJavaClassFolders()) {
                collectFiles(files, classFolder);
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.CLASS_FILE, files);
            }
        }
    }
    if (scopes.contains(Scope.MANIFEST)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (file.getName().equals(ANDROID_MANIFEST_XML)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.MANIFEST, files);
            }
        } else {
            List<File> manifestFiles = project.getManifestFiles();
            if (manifestFiles != null) {
                mFiles.put(Scope.MANIFEST, manifestFiles);
            }
        }
    }
    for (Map.Entry<Scope, List<File>> entry : mFiles.entrySet()) {
        Scope scope = entry.getKey();
        List<File> files = entry.getValue();
        List<Detector> applicable = new ArrayList<Detector>(mDetectors.size());
        for (Detector detector : mDetectors) {
            OtherFileScanner fileScanner = (OtherFileScanner) detector;
            EnumSet<Scope> appliesTo = fileScanner.getApplicableFiles();
            if (appliesTo.contains(Scope.OTHER) || appliesTo.contains(scope)) {
                applicable.add(detector);
            }
        }
        if (!applicable.isEmpty()) {
            for (File file : files) {
                Context context = new Context(driver, project, main, file);
                for (Detector detector : applicable) {
                    detector.beforeCheckFile(context);
                    detector.run(context);
                    detector.afterCheckFile(context);
                }
                if (driver.isCanceled()) {
                    return;
                }
            }
        }
    }
}
Also used : Context(com.android.tools.klint.detector.api.Context) Detector(com.android.tools.klint.detector.api.Detector) Scope(com.android.tools.klint.detector.api.Scope) OtherFileScanner(com.android.tools.klint.detector.api.Detector.OtherFileScanner) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) EnumMap(java.util.EnumMap) Map(java.util.Map)

Example 3 with Detector

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

the class ResourceVisitor method visitFile.

void visitFile(@NonNull XmlContext context, @NonNull File file) {
    assert LintUtils.isXmlFile(file);
    try {
        if (context.document == null) {
            context.document = mParser.parseXml(context);
            if (context.document == null) {
                // with details, location, etc.
                return;
            }
            if (context.document.getDocumentElement() == null) {
                // Ignore empty documents
                return;
            }
        }
        for (Detector check : mAllDetectors) {
            check.beforeCheckFile(context);
        }
        for (Detector.XmlScanner check : mDocumentDetectors) {
            check.visitDocument(context, context.document);
        }
        if (!mElementToCheck.isEmpty() || !mAttributeToCheck.isEmpty() || !mAllAttributeDetectors.isEmpty() || !mAllElementDetectors.isEmpty()) {
            visitElement(context, context.document.getDocumentElement());
        }
        for (Detector check : mAllDetectors) {
            check.afterCheckFile(context);
        }
    } finally {
        if (context.document != null) {
            mParser.dispose(context, context.document);
            context.document = null;
        }
    }
}
Also used : Detector(com.android.tools.klint.detector.api.Detector) XmlScanner(com.android.tools.klint.detector.api.Detector.XmlScanner)

Example 4 with Detector

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

the class LintDriver method checkBuildScripts.

private void checkBuildScripts(Project project, Project main) {
    List<Detector> detectors = mScopeDetectors.get(Scope.GRADLE_FILE);
    if (detectors != null) {
        List<File> files = project.getSubset();
        if (files == null) {
            files = project.getGradleBuildScripts();
        }
        for (File file : files) {
            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.visitBuildScript(context, Maps.<String, Object>newHashMap());
                    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 5 with Detector

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

the class LintDriver method computeRepeatingDetectors.

private void computeRepeatingDetectors(List<Detector> detectors, Project project) {
    // Ensure that the current visitor is recomputed
    mCurrentFolderType = null;
    mCurrentVisitor = null;
    mCurrentXmlDetectors = null;
    mCurrentBinaryDetectors = null;
    // Create map from detector class to issue such that we can
    // compute applicable issues for each detector in the list of detectors
    // to be repeated
    List<Issue> issues = mRegistry.getIssues();
    Multimap<Class<? extends Detector>, Issue> issueMap = ArrayListMultimap.create(issues.size(), 3);
    for (Issue issue : issues) {
        issueMap.put(issue.getImplementation().getDetectorClass(), issue);
    }
    Map<Class<? extends Detector>, EnumSet<Scope>> detectorToScope = new HashMap<Class<? extends Detector>, EnumSet<Scope>>();
    Map<Scope, List<Detector>> scopeToDetectors = new EnumMap<Scope, List<Detector>>(Scope.class);
    List<Detector> detectorList = new ArrayList<Detector>();
    // Compute the list of detectors (narrowed down from mRepeatingDetectors),
    // and simultaneously build up the detectorToScope map which tracks
    // the scopes each detector is affected by (this is used to populate
    // the mScopeDetectors map which is used during iteration).
    Configuration configuration = project.getConfiguration(this);
    for (Detector detector : detectors) {
        Class<? extends Detector> detectorClass = detector.getClass();
        Collection<Issue> detectorIssues = issueMap.get(detectorClass);
        if (detectorIssues != null) {
            boolean add = false;
            for (Issue issue : detectorIssues) {
                // the detector is enabled here.
                if (!configuration.isEnabled(issue)) {
                    continue;
                }
                // Include detector if any of its issues are enabled
                add = true;
                EnumSet<Scope> s = detectorToScope.get(detectorClass);
                EnumSet<Scope> issueScope = issue.getImplementation().getScope();
                if (s == null) {
                    detectorToScope.put(detectorClass, issueScope);
                } else if (!s.containsAll(issueScope)) {
                    EnumSet<Scope> union = EnumSet.copyOf(s);
                    union.addAll(issueScope);
                    detectorToScope.put(detectorClass, union);
                }
            }
            if (add) {
                detectorList.add(detector);
                EnumSet<Scope> union = detectorToScope.get(detector.getClass());
                for (Scope s : union) {
                    List<Detector> list = scopeToDetectors.get(s);
                    if (list == null) {
                        list = new ArrayList<Detector>();
                        scopeToDetectors.put(s, list);
                    }
                    list.add(detector);
                }
            }
        }
    }
    mApplicableDetectors = detectorList;
    mScopeDetectors = scopeToDetectors;
    mRepeatingDetectors = null;
    mRepeatScope = null;
    validateScopeList();
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) EnumSet(java.util.EnumSet) ArrayList(java.util.ArrayList) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) Scope(com.android.tools.klint.detector.api.Scope) LintUtils.isAnonymousClass(com.android.tools.klint.detector.api.LintUtils.isAnonymousClass) PsiModifierList(com.intellij.psi.PsiModifierList) InsnList(org.jetbrains.org.objectweb.asm.tree.InsnList) ArrayList(java.util.ArrayList) PsiAnnotationParameterList(com.intellij.psi.PsiAnnotationParameterList) List(java.util.List) EnumMap(java.util.EnumMap)

Aggregations

Detector (com.android.tools.klint.detector.api.Detector)16 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)11 File (java.io.File)7 XmlContext (com.android.tools.klint.detector.api.XmlContext)6 PsiFile (com.intellij.psi.PsiFile)6 ClassContext (com.android.tools.klint.detector.api.ClassContext)5 Context (com.android.tools.klint.detector.api.Context)5 JavaContext (com.android.tools.klint.detector.api.JavaContext)5 ResourceContext (com.android.tools.klint.detector.api.ResourceContext)5 ArrayList (java.util.ArrayList)5 Scope (com.android.tools.klint.detector.api.Scope)3 List (java.util.List)3 Issue (com.android.tools.klint.detector.api.Issue)2 EnumMap (java.util.EnumMap)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)2 InsnList (org.jetbrains.org.objectweb.asm.tree.InsnList)2 NonNull (com.android.annotations.NonNull)1