protected DefaultConfiguration(LintClient client, Project project, Configuration parent) { this(client, project, parent, new File(project.getDir(), CONFIG_FILE_NAME)); }
@Override @Nullable public File getBaselineFile() { if (baselineFile != null) { if (project != null && !baselineFile.isAbsolute()) { return new File(project.getDir(), baselineFile.getPath()); } } return baselineFile; }
/** * Returns true if this project is a Gradle-based Android project * * @param project the project to check * @return true if this is a Gradle-based project */ public boolean isGradleProject(Project project) { // This is not an accurate test; specific LintClient implementations (e.g. // IDEs or a gradle-integration of lint) have more context and can perform a more accurate // check if (new File(project.getDir(), SdkConstants.FN_BUILD_GRADLE).exists()) { return true; } File parent = project.getDir().getParentFile(); if (parent != null && parent.getName().equals(SdkConstants.FD_SOURCES)) { File root = parent.getParentFile(); if (root != null && new File(root, SdkConstants.FN_BUILD_GRADLE).exists()) { return true; } } return false; }
/** * Returns the resource folders. * * @param project the project to look up the resource folder for * @return a list of files pointing to the resource folders, possibly empty */ @NonNull public List<File> getResourceFolders(@NonNull Project project) { File res = new File(project.getDir(), RES_FOLDER); if (res.exists()) { return Collections.singletonList(res); } return Collections.emptyList(); }
/** * Returns the asset folders. * * @param project the project to look up the asset folder for * @return a list of files pointing to the asset folders, possibly empty */ @NonNull public List<File> getAssetFolders(@NonNull Project project) { File assets = new File(project.getDir(), FD_ASSETS); if (assets.exists()) { return Collections.singletonList(assets); } return Collections.emptyList(); }
@NonNull @Override public List<File> getTestSourceFolders(@NonNull Project project) { List<File> testSourceFolders = super.getTestSourceFolders(project); File tests = new File(project.getDir(), "test"); if (tests.exists()) { List<File> all = Lists.newArrayList(testSourceFolders); all.add(tests); testSourceFolders = all; } return testSourceFolders; }
@NonNull @Override public List<File> getTestSourceFolders(@NonNull Project project) { List<File> testSourceFolders = super.getTestSourceFolders(project); File tests = new File(project.getDir(), "test"); if (tests.exists()) { List<File> all = Lists.newArrayList(testSourceFolders); all.add(tests); testSourceFolders = all; } return testSourceFolders; }
/** * Returns the name of the given project * * @param project the project to look up * @return the name of the project */ @NonNull public String getProjectName(@NonNull Project project) { return project.getDir().getName(); }
/** * Returns the project containing a given file, or null if not found. This searches * only among the currently checked project and its library projects, not among all * possible projects being scanned sequentially. * * @param file the file to be checked * @return the corresponding project, or null if not found */ @Nullable public Project findProjectFor(@NonNull File file) { if (currentProjects != null) { if (currentProjects.length == 1) { return currentProjects[0]; } String path = file.getPath(); for (Project project : currentProjects) { if (path.startsWith(project.getDir().getPath())) { return project; } } } return null; }
protected DefaultConfiguration( @NonNull LintClient client, @NonNull Project project, @Nullable Configuration parent) { this(client, project, parent, new File(project.getDir(), CONFIG_FILE_NAME)); }
private void reportMap(Context context, Issue issue, Map<String, Location> map) { if (map != null) { for (Map.Entry<String, Location> entry : map.entrySet()) { Location location = entry.getValue(); String name = entry.getKey(); String message = mDescriptions.get(name); if (location == null) { location = Location.create(context.getProject().getDir()); } // We were prepending locations, but we want to prefer the base folders location = Location.reverse(location); context.report(issue, location, message); } } }
private void reportMap(Context context, Issue issue, Map<String, Location> map) { if (map != null) { for (Map.Entry<String, Location> entry : map.entrySet()) { Location location = entry.getValue(); String name = entry.getKey(); String message = mDescriptions.get(name); if (location == null) { location = Location.create(context.getProject().getDir()); } // We were prepending locations, but we want to prefer the base folders location = Location.reverse(location); context.report(issue, location, message); } } }
/** * Create a {@link Location} for an error in the top level build.gradle file. * This is necessary when we're doing an analysis based on the Gradle interpreted model, * not from parsing Gradle files - and the model doesn't provide source positions. * @param project the project containing the gradle file being analyzed * @return location for the top level gradle file if it exists, otherwise fall back to * the project directory. */ public static Location guessGradleLocation(@NonNull Project project) { File dir = project.getDir(); Location location; File topLevel = new File(dir, FN_BUILD_GRADLE); if (topLevel.exists()) { location = Location.create(topLevel); } else { location = Location.create(dir); } return location; }
private void checkJava(Project project, List<File> sourceFolders, List<Detector> checks) { Context context = new Context(mClient, project, project.getDir(), mScope); fireEvent(EventType.SCANNING_FILE, context); for (Detector detector : checks) { ((Detector.JavaScanner) detector).checkJavaSources(context, sourceFolders); if (mCanceled) { return; } } }
private void checkClasses(Project project, List<File> binFolders, List<Detector> checks) { Context context = new Context(mClient, project, project.getDir(), mScope); fireEvent(EventType.SCANNING_FILE, context); for (Detector detector : checks) { ((Detector.ClassScanner) detector).checkJavaClasses(context); if (mCanceled) { return; } } }
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) { detector.beforeCheckFile(context); detector.run(context); detector.afterCheckFile(context); } } }
protected void reportNonExistingIssueId(@Nullable Project project, @NonNull String id) { String message = String.format("Unknown issue id \"%1$s\"", id); if (driver != null && project != null) { Location location = Location.create(project.getDir()); if (!isSuppressed(IssueRegistry.LINT_ERROR)) { report(new Context(driver, project, project, project.getDir()), IssueRegistry.LINT_ERROR, project.getConfiguration(driver).getSeverity(IssueRegistry.LINT_ERROR), location, message, TextFormat.RAW); } } else { log(Severity.ERROR, null, "Lint: %1$s", message); } }
private void checkProject(Project project) { File projectDir = project.getDir(); Context projectContext = new Context(mClient, project, projectDir, mScope); fireEvent(EventType.SCANNING_PROJECT, projectContext); for (Detector check : mApplicableDetectors) { check.beforeCheckProject(projectContext); if (mCanceled) { return; } } runFileDetectors(project, projectDir); 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 // is valid Issue.create("Lint", "", "", Category.PERFORMANCE, 0, Severity.INFORMATIONAL, //$NON-NLS-1$ null, EnumSet.noneOf(Scope.class)), null /*range*/, "Lint canceled by user", null); } }
@NonNull @Override protected Project createProject(@NonNull File dir, @NonNull File referenceDir) { Project project = super.createProject(dir, referenceDir); if (project.isGradleProject()) { @SuppressWarnings("SpellCheckingInspection") String message = String.format("\"`%1$s`\" is a Gradle project. To correctly " + "analyze Gradle projects, you should run \"`gradlew :lint`\" instead.", project.getName()); Location location = Location.create(project.getDir()); Context context = new Context(driver, project, project, project.getDir()); if (context.isEnabled(IssueRegistry.LINT_ERROR) && !getConfiguration(project, null).isIgnored(context, IssueRegistry.LINT_ERROR, location, message)) { report(context, IssueRegistry.LINT_ERROR, project.getConfiguration(null).getSeverity( IssueRegistry.LINT_ERROR), location, message, TextFormat.RAW); } } return project; }
File lintXml = lintOptions.getLintConfig(); if (lintXml == null) { lintXml = new File(project.getDir(), DefaultConfiguration.CONFIG_FILE_NAME);