diff options
author | Tor Norbye <tnorbye@google.com> | 2012-03-19 11:56:36 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-03-19 11:56:36 -0700 |
commit | 14391267fac1be70fcbc7a5e80ba9132aa223b8c (patch) | |
tree | c91d91533ce55ed8ffdbe17d8f8922de859ba8ff /lint/libs/lint_api | |
parent | 8ed01fea9feb66e4e223c094c3cf7011afea28cb (diff) | |
parent | 381b3a420b08c1b0e1661cc9c131ead2419ec666 (diff) | |
download | sdk-14391267fac1be70fcbc7a5e80ba9132aa223b8c.zip sdk-14391267fac1be70fcbc7a5e80ba9132aa223b8c.tar.gz sdk-14391267fac1be70fcbc7a5e80ba9132aa223b8c.tar.bz2 |
Merge "Fix lint issues 27108, 27109 and 27110"
Diffstat (limited to 'lint/libs/lint_api')
-rw-r--r-- | lint/libs/lint_api/src/com/android/tools/lint/client/api/JavaVisitor.java | 78 | ||||
-rw-r--r-- | lint/libs/lint_api/src/com/android/tools/lint/detector/api/Detector.java | 6 |
2 files changed, 51 insertions, 33 deletions
diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/JavaVisitor.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/JavaVisitor.java index fc7e03b..1dbf915 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/JavaVisitor.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/JavaVisitor.java @@ -65,6 +65,7 @@ import lombok.ast.EmptyStatement; import lombok.ast.EnumConstant; import lombok.ast.EnumDeclaration; import lombok.ast.EnumTypeBody; +import lombok.ast.Expression; import lombok.ast.ExpressionStatement; import lombok.ast.FloatingPointLiteral; import lombok.ast.For; @@ -1114,43 +1115,60 @@ public class JavaVisitor { } @Override - public boolean visitVariableReference(@NonNull VariableReference node) { + public boolean visitSelect(Select node) { if (mVisitResources) { - String identifier = node.astIdentifier().getDescription(); - if (identifier.equals(R_CLASS) && - node.getParent() instanceof Select && - node.getParent().getParent() instanceof Select) { - Select parentSelect = (Select) node.getParent(); - Select grandParentSelect = (Select) parentSelect.getParent(); - String type = parentSelect.astIdentifier().astValue(); - String name = grandParentSelect.astIdentifier().astValue(); - - for (VisitingDetector v : mResourceFieldDetectors) { - JavaScanner detector = v.getJavaScanner(); - detector.visitResourceReference(mContext, v.getVisitor(), node, - type, name, false /* isFramework */); + // R.type.name + if (node.astOperand() instanceof Select) { + Select select = (Select) node.astOperand(); + if (select.astOperand() instanceof VariableReference) { + VariableReference reference = (VariableReference) select.astOperand(); + if (reference.astIdentifier().astValue().equals(R_CLASS)) { + String type = select.astIdentifier().astValue(); + String name = node.astIdentifier().astValue(); + + // R -could- be referenced locally and really have been + // imported as "import android.R;" in the import statements, + // but this is not recommended (and in fact there's a specific + // lint rule warning against it) + boolean isFramework = false; + + for (VisitingDetector v : mResourceFieldDetectors) { + JavaScanner detector = v.getJavaScanner(); + detector.visitResourceReference(mContext, v.getVisitor(), + node, type, name, isFramework); + } + + return super.visitSelect(node); + } } - } else if (identifier.equals(ANDROID_PKG) - && node.getParent() instanceof Select) { - Select parentSelect = (Select) node.getParent(); - if (parentSelect.astIdentifier().astValue().equals(R_CLASS) - && parentSelect.getParent() instanceof Select - && parentSelect.getParent().getParent() instanceof Select) { - Select p2 = (Select) parentSelect.getParent(); - Select p3 = (Select) p2.getParent(); - String type = p2.astIdentifier().astValue(); - String name = p3.astIdentifier().astValue(); - - for (VisitingDetector v : mResourceFieldDetectors) { - JavaScanner detector = v.getJavaScanner(); - detector.visitResourceReference(mContext, v.getVisitor(), node, - type, name, true /* isFramework */); + } + + // Arbitrary packages -- android.R.type.name, foo.bar.R.type.name + if (node.astIdentifier().astValue().equals(R_CLASS)) { + Node parent = node.getParent(); + if (parent instanceof Select) { + Node grandParent = parent.getParent(); + if (grandParent instanceof Select) { + Select select = (Select) grandParent; + String name = select.astIdentifier().astValue(); + Expression typeOperand = select.astOperand(); + if (typeOperand instanceof Select) { + Select typeSelect = (Select) typeOperand; + String type = typeSelect.astIdentifier().astValue(); + boolean isFramework = + node.astIdentifier().astValue().equals(ANDROID_PKG); + for (VisitingDetector v : mResourceFieldDetectors) { + JavaScanner detector = v.getJavaScanner(); + detector.visitResourceReference(mContext, v.getVisitor(), + node, type, name, isFramework); + } + } } } } } - return super.visitVariableReference(node); + return super.visitSelect(node); } @Override diff --git a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Detector.java b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Detector.java index 82ac76a..c795606 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Detector.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Detector.java @@ -33,7 +33,6 @@ import java.util.List; import lombok.ast.AstVisitor; import lombok.ast.MethodInvocation; -import lombok.ast.VariableReference; /** * A detector is able to find a particular problem. It might also be thought of as enforcing @@ -188,7 +187,7 @@ public abstract class Detector { void visitResourceReference( @NonNull JavaContext context, @Nullable AstVisitor visitor, - @NonNull VariableReference node, + @NonNull lombok.ast.Node node, @NonNull String type, @NonNull String name, boolean isFramework); @@ -444,7 +443,8 @@ public abstract class Detector { @SuppressWarnings("javadoc") public void visitResourceReference(@NonNull JavaContext context, @Nullable AstVisitor visitor, - @NonNull VariableReference node, @NonNull String type, @NonNull String name, boolean isFramework) { + @NonNull lombok.ast.Node node, @NonNull String type, @NonNull String name, + boolean isFramework) { } // ---- Dummy implementations to make implementing a ClassScanner easier: ---- |