Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/TryStatement.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 /**
16 * Try/catch/finally statement. Node type is {@link Token#TRY}.<p>
17 *
18 * <pre><i>TryStatement</i> :
19 * <b>try</b> Block Catch
20 * <b>try</b> Block Finally
21 * <b>try</b> Block Catch Finally
22 * <i>Catch</i> :
23 * <b>catch</b> ( <i><b>Identifier</b></i> ) Block
24 * <i>Finally</i> :
25 * <b>finally</b> Block</pre>
26 */
27 public class TryStatement extends AstNode {
28
29 private static final List<CatchClause> NO_CATCHES =
30 Collections.unmodifiableList(new ArrayList<CatchClause>());
31
32 private AstNode tryBlock;
33 private List<CatchClause> catchClauses;
34 private AstNode finallyBlock;
35 private int finallyPosition = -1;
36
37 {
38 type = Token.TRY;
39 }
40
41 public TryStatement() {
42 }
43
44 public TryStatement(int pos) {
45 super(pos);
46 }
47
48 public TryStatement(int pos, int len) {
49 super(pos, len);
50 }
51
52 public AstNode getTryBlock() {
53 return tryBlock;
54 }
55
56 /**
57 * Sets try block. Also sets its parent to this node.
58 * @throws IllegalArgumentException} if {@code tryBlock} is {@code null}
59 */
60 public void setTryBlock(AstNode tryBlock) {
61 assertNotNull(tryBlock);
62 this.tryBlock = tryBlock;
63 tryBlock.setParent(this);
64 }
65
66 /**
67 * Returns list of {@link CatchClause} nodes. If there are no catch
68 * clauses, returns an immutable empty list.
69 */
70 public List<CatchClause> getCatchClauses() {
71 return catchClauses != null ? catchClauses : NO_CATCHES;
72 }
73
74 /**
75 * Sets list of {@link CatchClause} nodes. Also sets their parents
76 * to this node. May be {@code null}. Replaces any existing catch
77 * clauses for this node.
78 */
79 public void setCatchClauses(List<CatchClause> catchClauses) {
80 if (catchClauses == null) {
81 this.catchClauses = null;
82 } else {
83 if (this.catchClauses != null)
84 this.catchClauses.clear();
85 for (CatchClause cc : catchClauses) {
86 addCatchClause(cc);
87 }
88 }
89 }
90
91 /**
92 * Add a catch-clause to the end of the list, and sets its parent to
93 * this node.
94 * @throws IllegalArgumentException} if {@code clause} is {@code null}
95 */
96 public void addCatchClause(CatchClause clause) {
97 assertNotNull(clause);
98 if (catchClauses == null) {
99 catchClauses = new ArrayList<CatchClause>();
100 }
101 catchClauses.add(clause);
102 clause.setParent(this);
103 }
104
105 /**
106 * Returns finally block, or {@code null} if not present
107 */
108 public AstNode getFinallyBlock() {
109 return finallyBlock;
110 }
111
112 /**
113 * Sets finally block, and sets its parent to this node.
114 * May be {@code null}.
115 */
116 public void setFinallyBlock(AstNode finallyBlock) {
117 this.finallyBlock = finallyBlock;
118 if (finallyBlock != null)
119 finallyBlock.setParent(this);
120 }
121
122 /**
123 * Returns position of {@code finally} keyword, if present, or -1
124 */
125 public int getFinallyPosition() {
126 return finallyPosition;
127 }
128
129 /**
130 * Sets position of {@code finally} keyword, if present, or -1
131 */
132 public void setFinallyPosition(int finallyPosition) {
133 this.finallyPosition = finallyPosition;
134 }
135
136 @Override
137 public String toSource(int depth) {
138 StringBuilder sb = new StringBuilder(250);
139 sb.append(makeIndent(depth));
140 sb.append("try ");
141 sb.append(tryBlock.toSource(depth).trim());
142 for (CatchClause cc : getCatchClauses()) {
143 sb.append(cc.toSource(depth));
144 }
145 if (finallyBlock != null) {
146 sb.append(" finally ");
147 sb.append(finallyBlock.toSource(depth));
148 }
149 return sb.toString();
150 }
151
152 /**
153 * Visits this node, then the try-block, then any catch clauses,
154 * and then any finally block.
155 */
156 @Override
157 public void visit(NodeVisitor v) {
158 if (v.visit(this)) {
159 tryBlock.visit(v);
160 for (CatchClause cc : getCatchClauses()) {
161 cc.visit(v);
162 }
163 if (finallyBlock != null) {
164 finallyBlock.visit(v);
165 }
166 }
167 }
168 }