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 |
2096
| public NameOccurrence(SimpleNode location, String image) {
|
28 |
2096
| this.location = location;
|
29 |
2096
| this.image = image;
|
30 |
| } |
31 |
| |
32 |
737
| public void setIsMethodOrConstructorInvocation() {
|
33 |
737
| isMethodOrConstructorInvocation = true;
|
34 |
| } |
35 |
| |
36 |
737
| public void setArgumentCount(int count) {
|
37 |
737
| argumentCount = count;
|
38 |
| } |
39 |
| |
40 |
246
| public int getArgumentCount() {
|
41 |
246
| return argumentCount;
|
42 |
| } |
43 |
| |
44 |
6454
| public boolean isMethodOrConstructorInvocation() {
|
45 |
6454
| return isMethodOrConstructorInvocation;
|
46 |
| } |
47 |
| |
48 |
624
| public void setNameWhichThisQualifies(NameOccurrence qualifiedName) {
|
49 |
624
| this.qualifiedName = qualifiedName;
|
50 |
| } |
51 |
| |
52 |
45
| public NameOccurrence getNameForWhichThisIsAQualifier() {
|
53 |
45
| return qualifiedName;
|
54 |
| } |
55 |
| |
56 |
90
| public boolean isPartOfQualifiedName() {
|
57 |
90
| return qualifiedName != null;
|
58 |
| } |
59 |
| |
60 |
2927
| public SimpleNode getLocation() {
|
61 |
2927
| return location;
|
62 |
| } |
63 |
| |
64 |
100
| public boolean isOnRightHandSide() {
|
65 |
100
| SimpleNode node = (SimpleNode) location.jjtGetParent().jjtGetParent().jjtGetParent();
|
66 |
100
| return node instanceof ASTExpression && node.jjtGetNumChildren() == 3;
|
67 |
| } |
68 |
| |
69 |
| |
70 |
233
| public boolean isOnLeftHandSide() {
|
71 |
| |
72 |
233
| SimpleNode primaryExpression;
|
73 |
233
| if (location.jjtGetParent() instanceof ASTPrimaryExpression) {
|
74 |
5
| primaryExpression = (SimpleNode) location.jjtGetParent().jjtGetParent();
|
75 |
228
| } else if (location.jjtGetParent().jjtGetParent() instanceof ASTPrimaryExpression) {
|
76 |
228
| 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 |
233
| if (isStandAlonePostfix(primaryExpression)) {
|
82 |
37
| return true;
|
83 |
| } |
84 |
| |
85 |
196
| if (primaryExpression.jjtGetNumChildren() <= 1) {
|
86 |
30
| return false;
|
87 |
| } |
88 |
| |
89 |
166
| if (!(primaryExpression.jjtGetChild(1) instanceof ASTAssignmentOperator)) {
|
90 |
102
| return false;
|
91 |
| } |
92 |
| |
93 |
64
| if (isPartOfQualifiedName() ) {
|
94 |
4
| return false;
|
95 |
| } |
96 |
| |
97 |
60
| if (isCompoundAssignment(primaryExpression)) {
|
98 |
4
| return false;
|
99 |
| } |
100 |
| |
101 |
56
| return true;
|
102 |
| } |
103 |
| |
104 |
60
| private boolean isCompoundAssignment(SimpleNode primaryExpression) {
|
105 |
60
| return ((ASTAssignmentOperator) (primaryExpression.jjtGetChild(1))).isCompound();
|
106 |
| } |
107 |
| |
108 |
233
| private boolean isStandAlonePostfix(SimpleNode primaryExpression) {
|
109 |
233
| if (!(primaryExpression instanceof ASTPostfixExpression) || !(primaryExpression.jjtGetParent() instanceof ASTStatementExpression)) {
|
110 |
195
| return false;
|
111 |
| } |
112 |
| |
113 |
38
| ASTPrimaryPrefix pf = (ASTPrimaryPrefix) ((ASTPrimaryExpression) primaryExpression.jjtGetChild(0)).jjtGetChild(0);
|
114 |
38
| if (pf.usesThisModifier()) {
|
115 |
1
| return true;
|
116 |
| } |
117 |
| |
118 |
37
| return thirdChildHasDottedName(primaryExpression);
|
119 |
| } |
120 |
| |
121 |
37
| private boolean thirdChildHasDottedName(SimpleNode primaryExpression) {
|
122 |
37
| Node thirdChild = primaryExpression.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
123 |
37
| return thirdChild instanceof ASTName && ((ASTName) thirdChild).getImage().indexOf(".") == -1;
|
124 |
| } |
125 |
| |
126 |
16
| public boolean isSelfAssignment() {
|
127 |
16
| if (location.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTPreDecrementExpression || location.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTPreIncrementExpression || location.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTPostfixExpression) {
|
128 |
7
| return true;
|
129 |
| } |
130 |
| |
131 |
9
| if (location.jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTStatementExpression) {
|
132 |
4
| ASTStatementExpression exp = (ASTStatementExpression) location.jjtGetParent().jjtGetParent().jjtGetParent();
|
133 |
4
| if (exp.jjtGetNumChildren() >= 2 && exp.jjtGetChild(1) instanceof ASTAssignmentOperator) {
|
134 |
4
| ASTAssignmentOperator op = (ASTAssignmentOperator) exp.jjtGetChild(1);
|
135 |
4
| if (op.isCompound()) {
|
136 |
3
| return true;
|
137 |
| } |
138 |
| } |
139 |
| } |
140 |
6
| return false;
|
141 |
| } |
142 |
| |
143 |
7501
| public boolean isThisOrSuper() {
|
144 |
7501
| return image.equals("this") || image.equals("super");
|
145 |
| } |
146 |
| |
147 |
1
| public boolean equals(Object o) {
|
148 |
1
| NameOccurrence n = (NameOccurrence) o;
|
149 |
1
| return n.getImage().equals(getImage());
|
150 |
| } |
151 |
| |
152 |
366
| public int hashCode() {
|
153 |
366
| return getImage().hashCode();
|
154 |
| } |
155 |
| |
156 |
9960
| public String getImage() {
|
157 |
9960
| return image;
|
158 |
| } |
159 |
| |
160 |
0
| public String toString() {
|
161 |
0
| return getImage() + ":" + location.getBeginLine() + ":" + location.getClass() + (this.isMethodOrConstructorInvocation() ? "(method call)" : "");
|
162 |
| } |
163 |
| } |