001: /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
002: package net.sourceforge.pmd.ast;
003:
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: public abstract class SimpleNode implements Node {
009:
010: protected Node parent;
011: protected Node[] children;
012: protected int id;
013: protected JavaParser parser;
014: private String image;
015: protected int beginLine = -1;
016: protected int endLine;
017: protected int beginColumn = -1;
018: protected int endColumn;
019: private boolean discardable;
020:
021: protected Token first, last;
022:
023: public void discardIfNecessary() {
024: if (discardable) {
025: SimpleNode parent = (SimpleNode) this .jjtGetParent();
026: SimpleNode kid = (SimpleNode) this .jjtGetChild(0);
027: kid.jjtSetParent(parent);
028: parent.jjtReplaceChild(this , kid);
029: }
030: }
031:
032: public void setDiscardable() {
033: this .discardable = true;
034: }
035:
036: public void setUnDiscardable() {
037: this .discardable = false;
038: }
039:
040: public SimpleNode(int i) {
041: id = i;
042: }
043:
044: public SimpleNode(JavaParser p, int i) {
045: this (i);
046: parser = p;
047: }
048:
049: public int getBeginLine() {
050: return beginLine;
051: }
052:
053: public void testingOnly__setBeginLine(int i) {
054: this .beginLine = i;
055: }
056:
057: public void testingOnly__setBeginColumn(int i) {
058: this .beginColumn = i;
059: }
060:
061: public int getBeginColumn() {
062: if (beginColumn != -1) {
063: return beginColumn;
064: } else {
065: if ((children != null) && (children.length > 0)) {
066: return ((SimpleNode) children[0]).getBeginColumn();
067: } else {
068: throw new RuntimeException(
069: "Unable to determine begining line of Node.");
070: }
071: }
072: }
073:
074: public String getImage() {
075: return image;
076: }
077:
078: public void setImage(String image) {
079: this .image = image;
080: }
081:
082: public int getEndLine() {
083: return endLine;
084: }
085:
086: public int getEndColumn() {
087: return endColumn;
088: }
089:
090: public Node getNthParent(int n) {
091: Node result = null;
092: for (int i = 0; i < n; i++) {
093: if (result == null) {
094: result = this .jjtGetParent();
095: } else {
096: result = result.jjtGetParent();
097: }
098: }
099: return result;
100: }
101:
102: /**
103: * Traverses up the tree to find the first parent instance of type parentType
104: *
105: * @param parentType class which you want to find.
106: * @return Node of type parentType. Returns null if none found.
107: */
108: public Node getFirstParentOfType(Class parentType) {
109: Node parentNode = jjtGetParent();
110: while (parentNode != null
111: && parentNode.getClass() != parentType) {
112: parentNode = parentNode.jjtGetParent();
113: }
114: return parentNode;
115: }
116:
117: /**
118: * Traverses up the tree to find all of the parent instances of type parentType
119: *
120: * @param parentType classes which you want to find.
121: * @return List of parentType instances found.
122: */
123: public List getParentsOfType(Class parentType) {
124: List parents = new ArrayList();
125: Node parentNode = jjtGetParent();
126: while (parentNode != null) {
127: if (parentNode.getClass() == parentType) {
128: parents.add(parentNode);
129: }
130: parentNode = parentNode.jjtGetParent();
131: }
132: return parents;
133: }
134:
135: public List findChildrenOfType(Class targetType) {
136: List list = new ArrayList();
137: findChildrenOfType(targetType, list);
138: return list;
139: }
140:
141: public void findChildrenOfType(Class targetType, List results) {
142: findChildrenOfType(this , targetType, results, true);
143: }
144:
145: public void findChildrenOfType(Class targetType, List results,
146: boolean descendIntoNestedClasses) {
147: this .findChildrenOfType(this , targetType, results,
148: descendIntoNestedClasses);
149: }
150:
151: private void findChildrenOfType(Node node, Class targetType,
152: List results, boolean descendIntoNestedClasses) {
153: if (node.getClass().equals(targetType)) {
154: results.add(node);
155: }
156:
157: if (!descendIntoNestedClasses) {
158: if (node instanceof ASTClassOrInterfaceDeclaration
159: && ((ASTClassOrInterfaceDeclaration) node)
160: .isNested()) {
161: return;
162: }
163:
164: if (node instanceof ASTClassOrInterfaceBodyDeclaration
165: && ((ASTClassOrInterfaceBodyDeclaration) node)
166: .isAnonymousInnerClass()) {
167: return;
168: }
169: }
170:
171: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
172: Node child = node.jjtGetChild(i);
173: if (child.jjtGetNumChildren() > 0) {
174: findChildrenOfType(child, targetType, results,
175: descendIntoNestedClasses);
176: } else {
177: if (child.getClass().equals(targetType)) {
178: results.add(child);
179: }
180: }
181: }
182: }
183:
184: public void jjtSetParent(Node n) {
185: parent = n;
186: }
187:
188: public Node jjtGetParent() {
189: return parent;
190: }
191:
192: public void jjtReplaceChild(Node old, Node newNode) {
193: for (int i = 0; i < children.length; i++) {
194: if (children[i] == old) {
195: children[i] = newNode;
196: return;
197: }
198: }
199: throw new RuntimeException(
200: "PMD INTERNAL ERROR: SimpleNode.jjtReplaceChild called to replace a node, but couldn't find the old node");
201: }
202:
203: public void jjtAddChild(Node n, int i) {
204: if (children == null) {
205: children = new Node[i + 1];
206: } else if (i >= children.length) {
207: Node c[] = new Node[i + 1];
208: System.arraycopy(children, 0, c, 0, children.length);
209: children = c;
210: }
211: children[i] = n;
212: }
213:
214: public Node jjtGetChild(int i) {
215: return children[i];
216: }
217:
218: public int jjtGetNumChildren() {
219: return (children == null) ? 0 : children.length;
220: }
221:
222: public void jjtOpen() {
223: first = parser.getToken(1); // new
224: if (beginLine == -1 && parser.token.next != null) {
225: beginLine = parser.token.next.beginLine;
226: beginColumn = parser.token.next.beginColumn;
227: }
228: }
229:
230: public void jjtClose() {
231: last = parser.getToken(0); // new
232: if (beginLine == -1
233: && (children == null || children.length == 0)) {
234: beginColumn = parser.token.beginColumn;
235: }
236: if (beginLine == -1) {
237: beginLine = parser.token.beginLine;
238: }
239: endLine = parser.token.endLine;
240: endColumn = parser.token.endColumn;
241: }
242:
243: public Token getFirstToken() {
244: return first;
245: } // new
246:
247: public Token getLastToken() {
248: return last;
249: } // new
250:
251: public String toString(String prefix) {
252: return prefix + toString();
253: }
254:
255: /* Override this method if you want to customize how the node dumps
256: out its children. */
257: public void dump(String prefix) {
258: System.out.println(toString(prefix)
259: + (image == null ? "" : ":" + image));
260: dumpChildren(prefix);
261: }
262:
263: protected void dumpChildren(String prefix) {
264: if (children != null) {
265: for (int i = 0; i < children.length; ++i) {
266: SimpleNode n = (SimpleNode) children[i];
267: if (n != null) {
268: n.dump(prefix + " ");
269: }
270: }
271: }
272: }
273:
274: /**
275: * Traverses down the tree to find the first child instance of type childType
276: *
277: * @param childType class which you want to find.
278: * @return Node of type childType. Returns <code>null</code> if none found.
279: */
280: public Node getFirstChildOfType(Class childType) {
281: return getFirstChildOfType(childType, this );
282: }
283:
284: private Node getFirstChildOfType(Class childType, Node node) {
285: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
286: Node n = node.jjtGetChild(i);
287: if (n != null) {
288: if (n.getClass().equals(childType))
289: return n;
290: Node n2 = getFirstChildOfType(childType, n);
291: if (n2 != null)
292: return n2;
293: }
294: }
295: return null;
296: }
297:
298: /**
299: * Finds if this node contains a child of the given type.
300: * This is an utility method that uses {@link #findChildrenOfType(Class)}
301: *
302: * @param type the node type to search
303: * @return <code>true</code> if there is at lease on child of the given type and <code>false</code> in any other case
304: */
305: public final boolean containsChildOfType(Class type) {
306: return !findChildrenOfType(type).isEmpty();
307: }
308:
309: }
|