| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
public void setTryBlock(AstNode tryBlock) { |
| 61 |
assertNotNull(tryBlock); |
| 62 |
this.tryBlock = tryBlock; |
| 63 |
tryBlock.setParent(this); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public List<CatchClause> getCatchClauses() { |
| 71 |
return catchClauses != null ? catchClauses : NO_CATCHES; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 107 |
|
| 108 |
public AstNode getFinallyBlock() { |
| 109 |
return finallyBlock; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void setFinallyBlock(AstNode finallyBlock) { |
| 117 |
this.finallyBlock = finallyBlock; |
| 118 |
if (finallyBlock != null) |
| 119 |
finallyBlock.setParent(this); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public int getFinallyPosition() { |
| 126 |
return finallyPosition; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 154 |
|
| 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 |
} |