Search in sources :

Example 1 with Context

use of com.android.tools.klint.detector.api.Context 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 Context

use of com.android.tools.klint.detector.api.Context 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 Context

use of com.android.tools.klint.detector.api.Context 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 4 with Context

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

the class LintDriver method analyze.

/** Runs the driver to analyze the requested files */
private void analyze() {
    mCanceled = false;
    mScope = mRequest.getScope();
    assert mScope == null || !mScope.contains(Scope.ALL_RESOURCE_FILES) || mScope.contains(Scope.RESOURCE_FILE);
    Collection<Project> projects;
    try {
        projects = mRequest.getProjects();
        if (projects == null) {
            projects = computeProjects(mRequest.getFiles());
        }
    } catch (CircularDependencyException e) {
        mCurrentProject = e.getProject();
        if (mCurrentProject != null) {
            Location location = e.getLocation();
            File file = location != null ? location.getFile() : mCurrentProject.getDir();
            Context context = new Context(this, mCurrentProject, null, file);
            context.report(IssueRegistry.LINT_ERROR, e.getLocation(), e.getMessage());
            mCurrentProject = null;
        }
        return;
    }
    if (projects.isEmpty()) {
        mClient.log(null, "No projects found for %1$s", mRequest.getFiles().toString());
        return;
    }
    if (mCanceled) {
        return;
    }
    registerCustomDetectors(projects);
    if (mScope == null) {
        mScope = Scope.infer(projects);
    }
    fireEvent(EventType.STARTING, null);
    for (Project project : projects) {
        mPhase = 1;
        Project main = mRequest.getMainProject(project);
        // The set of available detectors varies between projects
        computeDetectors(project);
        if (mApplicableDetectors.isEmpty()) {
            // No detectors enabled in this project: skip it
            continue;
        }
        checkProject(project, main);
        if (mCanceled) {
            break;
        }
        runExtraPhases(project, main);
    }
    fireEvent(mCanceled ? EventType.CANCELED : EventType.COMPLETED, 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) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) Location(com.android.tools.klint.detector.api.Location)

Example 5 with Context

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

the class DefaultConfiguration method formatError.

private void formatError(String message, Object... args) {
    if (args != null && args.length > 0) {
        message = String.format(message, args);
    }
    message = "Failed to parse `lint.xml` configuration file: " + message;
    LintDriver driver = new LintDriver(new IssueRegistry() {

        @Override
        @NonNull
        public List<Issue> getIssues() {
            return Collections.emptyList();
        }
    }, mClient);
    mClient.report(new Context(driver, mProject, mProject, mConfigFile), IssueRegistry.LINT_ERROR, mProject.getConfiguration(driver).getSeverity(IssueRegistry.LINT_ERROR), Location.create(mConfigFile), message, TextFormat.RAW);
}
Also used : Context(com.android.tools.klint.detector.api.Context) NonNull(com.android.annotations.NonNull) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

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