aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-02-24 15:51:10 -0800
committerTor Norbye <tnorbye@google.com>2012-02-24 15:54:50 -0800
commit8a74e947c8ec79456d4f8011ee73b5fcefc03440 (patch)
treeaac66ceb1ba9f1ae918206152ac760a0a6b8f04f /eclipse
parent73013b8947af3d04f8c48031b51e013d05cb1b2e (diff)
downloadsdk-8a74e947c8ec79456d4f8011ee73b5fcefc03440.zip
sdk-8a74e947c8ec79456d4f8011ee73b5fcefc03440.tar.gz
sdk-8a74e947c8ec79456d4f8011ee73b5fcefc03440.tar.bz2
Fix SuppressLint annotations in outer classes
This changeset makes @SuppressLint work for classfile based detectors when the error is found in an inner class and the SuppressLint annotations is on an outer class. Innerclasses are actually separate classes from their outer classes, so they are processed separately (each .class file is read and analyzed independently). This changeset processes the class files in alphabetical* order such that it can maintain a stack of outerclasses when processing a class. The suppress lint check can then visit the outer class' annotations to see if the error should be suppressed. (*: The order isn't exactly alphabetical: We want Foo$Bar.class to come after Foo.class) This changeset also tweaks the Add Annotation quickfix such that it only offers per-method or per-class annotations, since class files do not maintain annotation info for other granularities (such as on variable declarations, so you cannot suppress classfile based issues with annotations there.) We could make a lint check which ensures that you don't try to put these annotations there :-) (This is related to issue http://b.android.com/25948) Change-Id: Ia9dbc39b1adc73a1b60e375edbf9b5618c7d2353
Diffstat (limited to 'eclipse')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java19
1 files changed, 15 insertions, 4 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java
index 3da617b..6b945f8 100644
--- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java
+++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java
@@ -27,6 +27,8 @@ import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.editors.IconFactory;
import com.android.tools.lint.checks.ApiDetector;
+import com.android.tools.lint.detector.api.Issue;
+import com.android.tools.lint.detector.api.Scope;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
@@ -306,10 +308,12 @@ class AddSuppressAnnotation implements IMarkerResolution2 {
}
}
+ Issue issue = EclipseLintClient.getRegistry().getIssue(id);
+ boolean isClassDetector = issue != null && issue.getScope().contains(Scope.CLASS_FILE);
+
NodeFinder nodeFinder = new NodeFinder(root, offset, length);
ASTNode coveringNode = nodeFinder.getCoveringNode();
- ASTNode body = coveringNode;
- while (body != null) {
+ for (ASTNode body = coveringNode; body != null; body = body.getParent()) {
if (body instanceof BodyDeclaration) {
BodyDeclaration declaration = (BodyDeclaration) body;
@@ -334,6 +338,15 @@ class AddSuppressAnnotation implements IMarkerResolution2 {
target = body.getClass().getSimpleName();
}
+ // In class files, detectors can only find annotations on methods
+ // and on classes, not on variable declarations
+ if (isClassDetector && !(body instanceof MethodDeclaration
+ || body instanceof TypeDeclaration
+ || body instanceof AnonymousClassDeclaration
+ || body instanceof FieldDeclaration)) {
+ continue;
+ }
+
String desc = String.format("Add @SuppressLint '%1$s\' to '%2$s'", id, target);
resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, -1));
@@ -345,8 +358,6 @@ class AddSuppressAnnotation implements IMarkerResolution2 {
resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, api));
}
}
-
- body = body.getParent();
}
}
}