1 /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
2 package net.sourceforge.pmd.ast;
3
4 import net.sourceforge.pmd.symboltable.Scope;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 public class SimpleNode implements Node {
10 protected Node parent;
11 protected Node[] children;
12 protected int id;
13 protected JavaParser parser;
14 private String image;
15 private int beginLine = -1;
16 private int endLine;
17 private int beginColumn = -1;
18 private int endColumn;
19 private Scope scope;
20
21 public SimpleNode(int i) {
22 id = i;
23 }
24
25 public SimpleNode(JavaParser p, int i) {
26 this(i);
27 parser = p;
28 }
29
30 public void jjtOpen() {
31 if (beginLine == -1 && parser.token.next != null) {
32 beginLine = parser.token.next.beginLine;
33 beginColumn = parser.token.next.beginColumn;
34 }
35 }
36
37 public void jjtClose() {
38 if (beginLine == -1 && (children == null || children.length == 0)) {
39 beginColumn = parser.token.beginColumn;
40 }
41 if (beginLine == -1) {
42 beginLine = parser.token.beginLine;
43 }
44 endLine = parser.token.endLine;
45 endColumn = parser.token.endColumn;
46 }
47
48 public void setScope(Scope scope) {
49 this.scope = scope;
50 }
51
52 public Scope getScope() {
53 if (scope == null) {
54 return ((SimpleNode) parent).getScope();
55 }
56 return scope;
57 }
58
59 public int getBeginLine() {
60 return beginLine;
61 }
62
63 public void testingOnly__setBeginLine(int i) {
64 this.beginLine = i;
65 }
66
67 public void testingOnly__setBeginColumn(int i) {
68 this.beginColumn = i;
69 }
70
71 public int getBeginColumn() {
72 if (beginColumn != -1) {
73 return beginColumn;
74 } else {
75 if ((children != null) && (children.length > 0)) {
76 return ((SimpleNode) children[0]).getBeginColumn();
77 } else {
78 throw new RuntimeException("Unable to determine begining line of Node.");
79 }
80 }
81 }
82
83 public String getImage() {
84 return image;
85 }
86
87 public void setImage(String image) {
88 this.image = image;
89 }
90
91 public int getEndLine() {
92 return endLine;
93 }
94
95 public int getEndColumn() {
96 return endColumn;
97 }
98
99 /***
100 * Traverses up the tree to find the first parent instance of type parentType
101 * @param parentType class which you want to find.
102 * @return Node of type parentType. Returns null if none found.
103 */
104 public Node getFirstParentOfType(Class parentType) {
105 Node parentNode = jjtGetParent();
106
107 while (parentNode != null && parentNode.getClass() != parentType) {
108 parentNode = parentNode.jjtGetParent();
109 }
110
111 return parentNode;
112 }
113
114 /***
115 * Traverses up the tree to find all of the parent instances of type parentType
116 * @param parentType classes which you want to find.
117 * @return List of parentType instances found.
118 */
119 public List getParentsOfType(Class parentType) {
120 List parents = new ArrayList();
121 Node parentNode = jjtGetParent();
122
123 while (parentNode != null) {
124 if (parentNode.getClass() == parentType) {
125 parents.add(parentNode);
126 }
127
128 parentNode = parentNode.jjtGetParent();
129 }
130
131 return parents;
132 }
133
134 public List findChildrenOfType(Class targetType) {
135 List list = new ArrayList();
136 findChildrenOfType(targetType, list);
137 return list;
138 }
139
140 public void findChildrenOfType(Class targetType, List results) {
141 findChildrenOfType(this, targetType, results, true);
142 }
143
144 public void findChildrenOfType(Class targetType, List results, boolean descendIntoNestedClasses) {
145 this.findChildrenOfType(this, targetType, results, descendIntoNestedClasses);
146 }
147
148 private void findChildrenOfType(Node node, Class targetType, List results, boolean descendIntoNestedClasses) {
149 if (node.getClass().equals(targetType)) {
150 results.add(node);
151 }
152 if (node.getClass().equals(ASTNestedClassDeclaration.class) && !descendIntoNestedClasses) {
153 return;
154 }
155 if (node.getClass().equals(ASTClassBodyDeclaration.class) && ((ASTClassBodyDeclaration)node).isAnonymousInnerClass() && !descendIntoNestedClasses) {
156 return;
157 }
158 for (int i = 0; i < node.jjtGetNumChildren(); i++) {
159 Node child = node.jjtGetChild(i);
160 if (child.jjtGetNumChildren() > 0) {
161 findChildrenOfType(child, targetType, results, descendIntoNestedClasses);
162 } else {
163 if (child.getClass().equals(targetType)) {
164 results.add(child);
165 }
166 }
167 }
168 }
169
170 public void jjtSetParent(Node n) {
171 parent = n;
172 }
173
174 public Node jjtGetParent() {
175 return parent;
176 }
177
178 public void jjtAddChild(Node n, int i) {
179 if (children == null) {
180 children = new Node[i + 1];
181 } else if (i >= children.length) {
182 Node c[] = new Node[i + 1];
183 System.arraycopy(children, 0, c, 0, children.length);
184 children = c;
185 }
186 children[i] = n;
187 }
188
189 public Node jjtGetChild(int i) {
190 return children[i];
191 }
192
193 public int jjtGetNumChildren() {
194 return (children == null) ? 0 : children.length;
195 }
196
197 /*** Accept the visitor. **/
198 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
199 return visitor.visit(this, data);
200 }
201
202 /*** Accept the visitor. **/
203 public Object childrenAccept(JavaParserVisitor visitor, Object data) {
204 if (children != null) {
205 for (int i = 0; i < children.length; ++i) {
206 children[i].jjtAccept(visitor, data);
207 }
208 }
209 return data;
210 }
211
212 /* You can override these two methods in subclasses of SimpleNode to
213 customize the way the node appears when the tree is dumped. If
214 your output uses more than one line you should override
215 toString(String), otherwise overriding toString() is probably all
216 you need to do. */
217
218 public String toString() {
219 return JavaParserTreeConstants.jjtNodeName[id];
220 }
221
222 public String toString(String prefix) {
223 return prefix + toString();
224 }
225
226 /* Override this method if you want to customize how the node dumps
227 out its children. */
228 public void dump(String prefix) {
229 System.out.println(toString(prefix));
230 dumpChildren(prefix);
231 }
232
233 protected void dumpChildren(String prefix) {
234 if (children != null) {
235 for (int i = 0; i < children.length; ++i) {
236 SimpleNode n = (SimpleNode) children[i];
237 if (n != null) {
238 n.dump(prefix + " ");
239 }
240 }
241 }
242 }
243
244 }
245
This page was automatically generated by Maven