1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.symboltable; |
5 |
| |
6 |
| import net.sourceforge.pmd.ast.ASTAssignmentOperator; |
7 |
| import net.sourceforge.pmd.ast.ASTExpression; |
8 |
| import net.sourceforge.pmd.ast.ASTName; |
9 |
| import net.sourceforge.pmd.ast.ASTPostfixExpression; |
10 |
| import net.sourceforge.pmd.ast.ASTPreDecrementExpression; |
11 |
| import net.sourceforge.pmd.ast.ASTPreIncrementExpression; |
12 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
13 |
| import net.sourceforge.pmd.ast.ASTPrimaryPrefix; |
14 |
| import net.sourceforge.pmd.ast.ASTStatementExpression; |
15 |
| import net.sourceforge.pmd.ast.Node; |
16 |
| import net.sourceforge.pmd.ast.SimpleNode; |
17 |
| |
18 |
| public class NameOccurrence { |
19 |
| |
20 |
| private SimpleNode location; |
21 |
| private String image; |
22 |
| private NameOccurrence qualifiedName; |
23 |
| |
24 |
| private boolean isMethodOrConstructorInvocation; |
25 |
| private int argumentCount; |
26 |
| |
27 |
2527
| public NameOccurrence(SimpleNode location, String image) {
|
28 |
2527
| this.location = location;
|
29 |
2527
| this.image = image;
|
30 |
| } |
31 |
| |
32 |
896
| public void setIsMethodOrConstructorInvocation() {
|
33 |
896
| isMethodOrConstructorInvocation = true;
|
34 |
| } |
35 |
| |
36 |
896
| public void setArgumentCount(int count) {
|
37 |
896
| argumentCount = count;
|
38 |
| } |
39 |
| |
40 |
255
| public int getArgumentCount() {
|
41 |
255
| return argumentCount;
|
42 |
| } |
43 |
| |
44 |
7898
| public boolean isMethodOrConstructorInvocation() {
|
45 |
7898
| return isMethodOrConstructorInvocation;
|
46 |
| } |
47 |
| |
48 |
710
| public void setNameWhichThisQualifies(NameOccurrence qualifiedName) {
|
49 |
710
| this.qualifiedName = qualifiedName;
|
50 |
| } |
51 |
| |
52 |
51
| public NameOccurrence getNameForWhichThisIsAQualifier() {
|
53 |
51
| return qualifiedName;
|
54 |
| } |
55 |
| |
56 |
101
| public boolean isPartOfQualifiedName() {
|
57 |
101
| return qualifiedName != null;
|
58 |
| } |
59 |
| |
60 |
3552
| public SimpleNode getLocation() {
|
61 |
3552
| return location;
|
62 |
| } |
63 |
| |
64 |
110
| public boolean isOnRightHandSide() {
|
65 |
110
| SimpleNode node = (SimpleNode) location.jjtGetParent().jjtGetParent().jjtGetParent();
|
66 |
110
| return node instanceof ASTExpression && node.jjtGetNumChildren() == 3;
|
67 |
| } |
68 |
| |
69 |
| |
70 |
268
| public boolean isOnLeftHandSide() {
|
71 |
| |
72 |
268
| SimpleNode primaryExpression;
|
73 |
268
| if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
|
74 |
5
| primaryExpression = (SimpleNode) location.jjtGetParent().jjtGetParent();
|
75 |
263
| } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
|
76 |
263
| primaryExpression = (SimpleNode) location.jjtGetParent().jjtGetParent().jjtGetParent();
|
77 |
| } else { |
78 |
0
| 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());
|
79 |
| } |
80 |
| |
81 |
268
| if (isStandAlonePostfix(primaryExpression)) {
|
82 |
40
| return true;
|
83 |
| } |
84 |
| |
85 |
228
| if (primaryExpression.jjtGetNumChildren() <= 1) {
|
86 |
41
| return false;
|
87 |
| } |
88 |
| |
89 |
187
| if (!(primaryExpression.jjtGetChild(1) instanceof ASTAssignmentOperator)) {
|
90 |
112
| return false;
|
91 |
| } |
92 |
| |
93 |
75
| if (isPartOfQualifiedName() ) {
|
94 |
4
| return false;
|
95 |
| } |
96 |
| |
97 |
71
| if (isCompoundAssignment(primaryExpression)) {
|
98 |
6
| return false;
|
99 |
| } |
100 |
| |
101 |
65
| return true;
|
102 |
| } |
103 |
| |
104 |
71
| private boolean isCompoundAssignment(SimpleNode primaryExpression) {
|
105 |
71
| return ((ASTAssignmentOperator) (primaryExpression.jjtGetChild(1))).isCompound();
|
106 |
| } |
107 |
| |
108 |
268
| private boolean isStandAlonePostfix(SimpleNode primaryExpression) {
|
109 |
268
| if (!(primaryExpression instanceof ASTPostfixExpression) || !(primaryExpression.jjtGetParent() instanceof ASTStatementExpression)) {
|
110 |
227
| return false;
|
111 |
| } |
112 |
| |
113 |
41
| ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression) primaryExpression.jjtGetChild(0)).jjtGetChild(0);
|
114 |
41
| if (pf.usesThisModifier()) {
|
115 |
1
| return true;
|
116 |
| } |
117 |
| |
118 |
40
| return thirdChildHasDottedName(primaryExpression);
|
119 |
| } |
120 |
| |
121 |
40
| private boolean thirdChildHasDottedName(SimpleNode primaryExpression) {
|
122 |
40
| Node thirdChild = primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
123 |
40
| return thirdChild instanceof ASTName && ((ASTName) thirdChild).getImage().indexOf('.') == -1;
|
124 |
| } |
125 |
| |
126 |
23
| public boolean isSelfAssignment() {
|
127 |
23
| Node l = location;
|
128 |
23
| while (true) {
|
129 |
24
| Node p = l.jjtGetParent();
|
130 |
24
| Node gp = p.jjtGetParent();
|
131 |
24
| Node node = gp.jjtGetParent();
|
132 |
24
| if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
|
133 |
9
| return true;
|
134 |
| } |
135 |
| |
136 |
15
| if (node instanceof ASTStatementExpression) {
|
137 |
6
| ASTStatementExpression exp = (ASTStatementExpression) node;
|
138 |
6
| if (exp.jjtGetNumChildren() >= 2 && exp.jjtGetChild(1) instanceof ASTAssignmentOperator) {
|
139 |
6
| ASTAssignmentOperator op = (ASTAssignmentOperator) exp.jjtGetChild(1);
|
140 |
6
| if (op.isCompound()) {
|
141 |
5
| return true;
|
142 |
| } |
143 |
| } |
144 |
| } |
145 |
| |
146 |
| |
147 |
10
| if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 &&
|
148 |
| gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1&& |
149 |
| node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && |
150 |
| node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) { |
151 |
1
| l = node;
|
152 |
1
| continue;
|
153 |
| } |
154 |
| |
155 |
9
| return false;
|
156 |
| } |
157 |
| } |
158 |
| |
159 |
9159
| public boolean isThisOrSuper() {
|
160 |
9159
| return image.equals("this") || image.equals("super");
|
161 |
| } |
162 |
| |
163 |
1
| public boolean equals(Object o) {
|
164 |
1
| NameOccurrence n = (NameOccurrence) o;
|
165 |
1
| return n.getImage().equals(getImage());
|
166 |
| } |
167 |
| |
168 |
389
| public int hashCode() {
|
169 |
389
| return getImage().hashCode();
|
170 |
| } |
171 |
| |
172 |
12132
| public String getImage() {
|
173 |
12132
| return image;
|
174 |
| } |
175 |
| |
176 |
0
| public String toString() {
|
177 |
0
| return getImage() + ":" + location.getBeginLine() + ":" + location.getClass() + (this.isMethodOrConstructorInvocation() ? "(method call)" : "");
|
178 |
| } |
179 |
| } |