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.ASTPrimaryExpression;
9 import net.sourceforge.pmd.lang.plsql.ast.PLSQLNode;
10 import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
11
12 public class PLSQLNameOccurrence implements NameOccurrence {
13
14 private PLSQLNode location;
15 private String image;
16 private PLSQLNameOccurrence qualifiedName;
17
18 private boolean isMethodOrConstructorInvocation;
19 private int argumentCount;
20
21 private final static String THIS = "this";
22 private final static String SUPER = "super";
23
24 public PLSQLNameOccurrence(PLSQLNode location, String image) {
25 this.location = location;
26 this.image = image;
27 }
28
29 public void setIsMethodOrConstructorInvocation() {
30 isMethodOrConstructorInvocation = true;
31 }
32
33 public void setArgumentCount(int count) {
34 argumentCount = count;
35 }
36
37 public int getArgumentCount() {
38 return argumentCount;
39 }
40
41 public boolean isMethodOrConstructorInvocation() {
42 return isMethodOrConstructorInvocation;
43 }
44
45 public void setNameWhichThisQualifies(PLSQLNameOccurrence qualifiedName) {
46 this.qualifiedName = qualifiedName;
47 }
48
49 public PLSQLNameOccurrence getNameForWhichThisIsAQualifier() {
50 return qualifiedName;
51 }
52
53 public boolean isPartOfQualifiedName() {
54 return qualifiedName != null;
55 }
56
57 public PLSQLNode getLocation() {
58 return location;
59 }
60
61 public boolean isOnRightHandSide() {
62 Node node = location.jjtGetParent().jjtGetParent().jjtGetParent();
63 return node instanceof ASTExpression && node.jjtGetNumChildren() == 3;
64 }
65
66 public boolean isOnLeftHandSide() {
67
68 Node primaryExpression;
69 if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
70 primaryExpression = location.jjtGetParent().jjtGetParent();
71 } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
72 primaryExpression = location.jjtGetParent().jjtGetParent().jjtGetParent();
73 } else {
74 throw new RuntimeException(
75 "Found a NameOccurrence that didn't have an ASTPrimaryExpression as parent or grandparent. "
76 + " Node = " + location.getClass().getCanonicalName() + ", Parent = "
77 + location.jjtGetParent().getClass().getCanonicalName() + " and grandparent = "
78 + location.jjtGetParent().jjtGetParent().getClass().getCanonicalName() + " @ line = "
79 + location.getBeginLine() + ", column = " + location.getBeginColumn());
80 }
81
82
83
84
85
86 if (primaryExpression.jjtGetNumChildren() <= 1) {
87 return false;
88 }
89
90
91
92
93
94
95 if (isPartOfQualifiedName() ) {
96 return false;
97 }
98
99
100
101
102
103 return true;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 public boolean isThisOrSuper() {
173 return image.equals(THIS) || image.equals(SUPER);
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 @Override
191 public boolean equals(Object o) {
192 if (o instanceof PLSQLNameOccurrence) {
193 PLSQLNameOccurrence n = (PLSQLNameOccurrence) o;
194 return n.getImage().equals(getImage());
195 }
196 return false;
197 }
198
199 @Override
200 public int hashCode() {
201 return getImage().hashCode();
202 }
203
204 public String getImage() {
205 return image;
206 }
207
208 @Override
209 public String toString() {
210 return getImage() + ":" + location.getBeginLine() + ":" + location.getClass()
211 + (this.isMethodOrConstructorInvocation() ? "(method call)" : "");
212 }
213 }