View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3   */
4   package net.sourceforge.pmd.symboltable;
5   
6   import net.sourceforge.pmd.ast.ASTAssignmentOperator;
7   import net.sourceforge.pmd.ast.ASTPrimaryExpression;
8   import net.sourceforge.pmd.ast.SimpleNode;
9   
10  public class NameOccurrence {
11  
12      private SimpleNode location;
13      private String image;
14      private NameOccurrence qualifiedName;
15      private boolean isMethodOrConstructorInvocation;
16  
17      public NameOccurrence(SimpleNode location, String image) {
18          this.location = location;
19          this.image = image;
20      }
21  
22      public void setIsMethodOrConstructorInvocation() {
23          isMethodOrConstructorInvocation = true;
24      }
25  
26      public boolean isMethodOrConstructorInvocation() {
27          return isMethodOrConstructorInvocation;
28      }
29  
30      public void setNameWhichThisQualifies(NameOccurrence qualifiedName) {
31          this.qualifiedName = qualifiedName;
32      }
33  
34      public NameOccurrence getNameForWhichThisIsAQualifier() {
35          return qualifiedName;
36      }
37  
38      public SimpleNode getLocation() {
39        return location;
40      }
41  
42      public boolean isOnLeftHandSide() {
43          SimpleNode primaryExpression;
44          if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
45              primaryExpression = (SimpleNode) location.jjtGetParent().jjtGetParent();
46          } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
47              primaryExpression = (SimpleNode) location.jjtGetParent().jjtGetParent().jjtGetParent();
48          } else {
49              throw new RuntimeException("Found a NameOccurrence that didn't have an ASTPrimary Expression as parent or grandparent.  Parent = " + location.jjtGetParent() + " and grandparent = " + location.jjtGetParent().jjtGetParent());
50          }
51  
52          return primaryExpression.jjtGetNumChildren() > 1 && primaryExpression.jjtGetChild(1) instanceof ASTAssignmentOperator;
53      }
54  
55      public Scope getScope() {
56          return location.getScope();
57      }
58  
59  
60      public int getBeginLine() {
61          return location.getBeginLine();
62      }
63  
64      public boolean isThisOrSuper() {
65          return image.equals("this") || image.equals("super");
66      }
67  
68      public boolean equals(Object o) {
69          NameOccurrence n = (NameOccurrence) o;
70          return n.getImage().equals(getImage());
71      }
72  
73      public String getImage() {
74          return image;
75      }
76  
77      public int hashCode() {
78          return getImage().hashCode();
79      }
80  
81      public String toString() {
82          return getImage() + ":" + location.getBeginLine() + ":" + location.getClass();
83      }
84  }