1
2
3
4 package net.sourceforge.pmd.lang.plsql.symboltable;
5
6 import net.sourceforge.pmd.lang.ast.Node;
7 import net.sourceforge.pmd.lang.plsql.ast.ASTExpression;
8 import net.sourceforge.pmd.lang.plsql.ast.ASTName;
9 import net.sourceforge.pmd.lang.plsql.ast.ASTPrimaryExpression;
10 import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
11 import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
12
13 public class PLSQLNameOccurrence implements NameOccurrence {
14
15 private PLSQLNode location;
16 private String image;
17 private PLSQLNameOccurrence qualifiedName;
18
19 private boolean isMethodOrConstructorInvocation;
20 private int argumentCount;
21
22 private final static String THIS = "this";
23 private final static String SUPER = "super";
24
25 public PLSQLNameOccurrence(PLSQLNode location, String image) {
26 this.location = location;
27 this.image = image;
28 }
29
30 public void setIsMethodOrConstructorInvocation() {
31 isMethodOrConstructorInvocation = true;
32 }
33
34 public void setArgumentCount(int count) {
35 argumentCount = count;
36 }
37
38 public int getArgumentCount() {
39 return argumentCount;
40 }
41
42 public boolean isMethodOrConstructorInvocation() {
43 return isMethodOrConstructorInvocation;
44 }
45
46 public void setNameWhichThisQualifies(PLSQLNameOccurrence qualifiedName) {
47 this.qualifiedName = qualifiedName;
48 }
49
50 public PLSQLNameOccurrence getNameForWhichThisIsAQualifier() {
51 return qualifiedName;
52 }
53
54 public boolean isPartOfQualifiedName() {
55 return qualifiedName != null;
56 }
57
58 public PLSQLNode getLocation() {
59 return location;
60 }
61
62 public boolean isOnRightHandSide() {
63 Node node = location.jjtGetParent().jjtGetParent().jjtGetParent();
64 return node instanceof ASTExpression && node.jjtGetNumChildren() == 3;
65 }
66
67
68 public boolean isOnLeftHandSide() {
69
70 Node primaryExpression;
71 if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
72 primaryExpression = location.jjtGetParent().jjtGetParent();
73 } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
74 primaryExpression = location.jjtGetParent().jjtGetParent().jjtGetParent();
75 } else {
76 throw new RuntimeException("Found a NameOccurrence that didn't have an ASTPrimaryExpression as parent or grandparent. "
77 + " Node = " + location.getClass().getCanonicalName()
78 + ", Parent = " + location.jjtGetParent().getClass().getCanonicalName()
79 + " and grandparent = " + location.jjtGetParent().jjtGetParent().getClass().getCanonicalName()
80 + " @ line = " + location.getBeginLine() + ", column = " + location.getBeginColumn()
81 );
82 }
83
84
85
86
87
88
89
90 if (primaryExpression.jjtGetNumChildren() <= 1) {
91 return false;
92 }
93
94
95
96
97
98
99
100 if (isPartOfQualifiedName() ) {
101 return false;
102 }
103
104
105
106
107
108
109
110 return true;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 private boolean thirdChildHasDottedName(Node primaryExpression) {
133 Node thirdChild = primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
134 return thirdChild instanceof ASTName && ((ASTName) thirdChild).getImage().indexOf('.') == -1;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public boolean isThisOrSuper() {
197 return image.equals(THIS) || image.equals(SUPER);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 @Override
220 public boolean equals(Object o) {
221 if (o instanceof PLSQLNameOccurrence) {
222 PLSQLNameOccurrence n = (PLSQLNameOccurrence) o;
223 return n.getImage().equals(getImage());
224 }
225 return false;
226 }
227
228 @Override
229 public int hashCode() {
230 return getImage().hashCode();
231 }
232
233 public String getImage() {
234 return image;
235 }
236
237 @Override
238 public String toString() {
239 return getImage() + ":" + location.getBeginLine() + ":" + location.getClass() + (this.isMethodOrConstructorInvocation() ? "(method call)" : "");
240 }
241 }