Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(820)

Unified Diff: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java

Issue 22710009: Report INSTANCE_ACCESS_TO_STATIC_MEMBER. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
diff --git a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
index c460d86df84b71d78ab6bac31541e657fa38262b..64d193ef094162b0322d64671a12fcb0d7b9a5e7 100644
--- a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
+++ b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
@@ -25,6 +25,7 @@ import com.google.dart.engine.ast.CatchClause;
import com.google.dart.engine.ast.ClassDeclaration;
import com.google.dart.engine.ast.ClassMember;
import com.google.dart.engine.ast.ClassTypeAlias;
+import com.google.dart.engine.ast.Comment;
import com.google.dart.engine.ast.CommentReference;
import com.google.dart.engine.ast.CompilationUnit;
import com.google.dart.engine.ast.ConditionalExpression;
@@ -232,6 +233,12 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
/**
* This is set to {@code true} iff the visitor is currently visiting children nodes of an
+ * {@link Comment}.
+ */
+ private boolean isInComment;
+
+ /**
+ * This is set to {@code true} iff the visitor is currently visiting children nodes of an
* {@link InstanceCreationExpression}.
*/
private boolean isInConstInstanceCreation;
@@ -467,6 +474,16 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
@Override
+ public Void visitComment(Comment node) {
+ isInComment = true;
+ try {
+ return super.visitComment(node);
+ } finally {
+ isInComment = false;
+ }
+ }
+
+ @Override
public Void visitConditionalExpression(ConditionalExpression node) {
checkForNonBoolCondition(node.getCondition());
return super.visitConditionalExpression(node);
@@ -731,7 +748,10 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
@Override
public Void visitMethodInvocation(MethodInvocation node) {
- checkForStaticAccessToInstanceMember(node.getTarget(), node.getMethodName());
+ Expression target = node.getRealTarget();
+ SimpleIdentifier methodName = node.getMethodName();
+ checkForStaticAccessToInstanceMember(target, methodName);
+ checkForInstanceAccessToStaticMember(target, methodName);
return super.visitMethodInvocation(node);
}
@@ -761,6 +781,7 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
public Void visitPrefixedIdentifier(PrefixedIdentifier node) {
if (!(node.getParent() instanceof Annotation)) {
checkForStaticAccessToInstanceMember(node.getPrefix(), node.getIdentifier());
+ checkForInstanceAccessToStaticMember(node.getPrefix(), node.getIdentifier());
}
return super.visitPrefixedIdentifier(node);
}
@@ -777,7 +798,9 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
@Override
public Void visitPropertyAccess(PropertyAccess node) {
Expression target = node.getRealTarget();
- checkForStaticAccessToInstanceMember(target, node.getPropertyName());
+ SimpleIdentifier propertyName = node.getPropertyName();
+ checkForStaticAccessToInstanceMember(target, propertyName);
+ checkForInstanceAccessToStaticMember(target, propertyName);
return super.visitPropertyAccess(node);
}
@@ -3006,6 +3029,50 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
/**
+ * This checks that if the given "target" is not a type reference then the "name" is reference to
+ * a instance member.
+ *
+ * @param target the target of the name access to evaluate
+ * @param name the accessed name to evaluate
+ * @return {@code true} if and only if an error code is generated on the passed node
+ * @see StaticTypeWarningCode#INSTANCE_ACCESS_TO_STATIC_MEMBER
+ */
+ private boolean checkForInstanceAccessToStaticMember(Expression target, SimpleIdentifier name) {
+ // OK, no target
+ if (target == null) {
+ return false;
+ }
+ // OK, in comment
+ if (isInComment) {
+ return false;
+ }
+ // prepare member Element
+ Element element = name.getElement();
+ if (!(element instanceof ExecutableElement)) {
+ return false;
+ }
+ ExecutableElement executableElement = (ExecutableElement) element;
+ // OK, top-level element
+ if (!(executableElement.getEnclosingElement() instanceof ClassElement)) {
+ return false;
+ }
+ // OK, instance member
+ if (!executableElement.isStatic()) {
+ return false;
+ }
+ // OK, target is a type
+ if (isTypeReference(target)) {
+ return false;
+ }
+ // report problem
+ errorReporter.reportError(
+ StaticTypeWarningCode.INSTANCE_ACCESS_TO_STATIC_MEMBER,
+ name,
+ name.getName());
+ return true;
+ }
+
+ /**
* This verifies that an 'int' can be assigned to the parameter corresponding to the given
* expression. This is used for prefix and postfix expressions where the argument value is
* implicit.

Powered by Google App Engine
This is Rietveld 408576698