use of com.android.tools.klint.detector.api.Detector in project kotlin by JetBrains.
the class IssueRegistry method createDetectors.
/**
* Creates a list of detectors applicable to the given scope, and with the
* given configuration.
*
* @param client the client to report errors to
* @param configuration the configuration to look up which issues are
* enabled etc from
* @param scope the scope for the analysis, to filter out detectors that
* require wider analysis than is currently being performed
* @param scopeToDetectors an optional map which (if not null) will be
* filled by this method to contain mappings from each scope to
* the applicable detectors for that scope
* @return a list of new detector instances
*/
@NonNull
final List<? extends Detector> createDetectors(@NonNull LintClient client, @NonNull Configuration configuration, @NonNull EnumSet<Scope> scope, @Nullable Map<Scope, List<Detector>> scopeToDetectors) {
List<Issue> issues = getIssuesForScope(scope);
if (issues.isEmpty()) {
return Collections.emptyList();
}
Set<Class<? extends Detector>> detectorClasses = new HashSet<Class<? extends Detector>>();
Map<Class<? extends Detector>, EnumSet<Scope>> detectorToScope = new HashMap<Class<? extends Detector>, EnumSet<Scope>>();
for (Issue issue : issues) {
Implementation implementation = issue.getImplementation();
Class<? extends Detector> detectorClass = implementation.getDetectorClass();
EnumSet<Scope> issueScope = implementation.getScope();
if (!detectorClasses.contains(detectorClass)) {
// Determine if the issue is enabled
if (!configuration.isEnabled(issue)) {
continue;
}
// Ensured by getIssuesForScope above
assert implementation.isAdequate(scope);
detectorClass = client.replaceDetector(detectorClass);
assert detectorClass != null : issue.getId();
detectorClasses.add(detectorClass);
}
if (scopeToDetectors != null) {
EnumSet<Scope> s = detectorToScope.get(detectorClass);
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);
}
}
}
List<Detector> detectors = new ArrayList<Detector>(detectorClasses.size());
for (Class<? extends Detector> clz : detectorClasses) {
try {
Detector detector = clz.newInstance();
detectors.add(detector);
if (scopeToDetectors != null) {
EnumSet<Scope> union = detectorToScope.get(clz);
for (Scope s : union) {
List<Detector> list = scopeToDetectors.get(s);
if (list == null) {
list = new ArrayList<Detector>();
scopeToDetectors.put(s, list);
}
list.add(detector);
}
}
} catch (Throwable t) {
//$NON-NLS-1$
client.log(t, "Can't initialize detector %1$s", clz.getName());
}
}
return detectors;
}
use of com.android.tools.klint.detector.api.Detector 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;
}
use of com.android.tools.klint.detector.api.Detector in project kotlin by JetBrains.
the class ResourceVisitor method visitBinaryResource.
public void visitBinaryResource(@NonNull ResourceContext context) {
if (mBinaryDetectors == null) {
return;
}
for (Detector check : mBinaryDetectors) {
check.beforeCheckFile(context);
check.checkBinaryResource(context);
check.afterCheckFile(context);
}
}
use of com.android.tools.klint.detector.api.Detector in project kotlin by JetBrains.
the class AsmVisitor method runClassDetectors.
// ASM API uses raw types
@SuppressWarnings("rawtypes")
void runClassDetectors(ClassContext context) {
ClassNode classNode = context.getClassNode();
for (Detector detector : mAllDetectors) {
detector.beforeCheckFile(context);
}
for (Detector detector : mFullClassChecks) {
Detector.ClassScanner scanner = (Detector.ClassScanner) detector;
scanner.checkClass(context, classNode);
detector.afterCheckFile(context);
}
if (!mMethodNameToChecks.isEmpty() || !mMethodOwnerToChecks.isEmpty() || mNodeTypeDetectors != null && mNodeTypeDetectors.length > 0) {
List methodList = classNode.methods;
for (Object m : methodList) {
MethodNode method = (MethodNode) m;
InsnList nodes = method.instructions;
for (int i = 0, n = nodes.size(); i < n; i++) {
AbstractInsnNode instruction = nodes.get(i);
int type = instruction.getType();
if (type == AbstractInsnNode.METHOD_INSN) {
MethodInsnNode call = (MethodInsnNode) instruction;
String owner = call.owner;
List<ClassScanner> scanners = mMethodOwnerToChecks.get(owner);
if (scanners != null) {
for (ClassScanner scanner : scanners) {
scanner.checkCall(context, classNode, method, call);
}
}
String name = call.name;
scanners = mMethodNameToChecks.get(name);
if (scanners != null) {
for (ClassScanner scanner : scanners) {
scanner.checkCall(context, classNode, method, call);
}
}
}
if (mNodeTypeDetectors != null && type < mNodeTypeDetectors.length) {
List<ClassScanner> scanners = mNodeTypeDetectors[type];
if (scanners != null) {
for (ClassScanner scanner : scanners) {
scanner.checkInstruction(context, classNode, method, instruction);
}
}
}
}
}
}
for (Detector detector : mAllDetectors) {
detector.afterCheckFile(context);
}
}
use of com.android.tools.klint.detector.api.Detector 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;
}
}
}
}
Aggregations