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);
}
}
}
}
}
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;
}
}
}
}
}
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;
}
}
}
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);
}
}
}
}
}
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();
}
Aggregations