1
2
3 package net.sourceforge.pmd.ast;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 public class ASTTryStatement extends SimpleNode {
9
10 private boolean hasCatch;
11 private boolean hasFinally;
12
13
14 public ASTTryStatement(int id) {
15 super(id);
16 }
17
18 public ASTTryStatement(JavaParser p, int id) {
19 super(p, id);
20 }
21
22 public void setHasCatch() {
23 hasCatch = true;
24 }
25
26 public void setHasFinally() {
27 hasFinally = true;
28 }
29
30 public boolean hasCatch() {
31 return hasCatch;
32 }
33
34 public boolean hasFinally() {
35 return hasFinally;
36 }
37
38 public ASTBlock getFinallyBlock() {
39 if (!hasFinally) {
40 throw new RuntimeException("ASTTryStatement.getFinallyBlock() called, but no finally statement exists");
41 }
42 return (ASTBlock) jjtGetChild(jjtGetNumChildren() - 1);
43 }
44
45 /***
46 * Call hasCatch() before you call this method
47 */
48 public List getCatchBlocks() {
49 int numChildren = jjtGetNumChildren();
50 if (hasFinally)
51 numChildren--;
52 List blocks = new ArrayList();
53 for (int i = 1; i < numChildren; i += 2) {
54 blocks.add(new ASTCatch((ASTFormalParameter) jjtGetChild(i + 0), (ASTBlock) jjtGetChild(i + 1)));
55 }
56 return blocks;
57 }
58
59 /***
60 * Accept the visitor. *
61 */
62 public Object jjtAccept(JavaParserVisitor visitor, Object data) {
63 return visitor.visit(this, data);
64 }
65
66 public void dump(String prefix) {
67 String x = "";
68 if (hasCatch) {
69 x += ":(has catch)";
70 }
71 if (hasFinally) {
72 if (x == "") {
73 x += ":";
74 }
75 x += "(has finally)";
76 }
77 System.out.println(toString(prefix) + x);
78 dumpChildren(prefix);
79 }
80
81 }