001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.java.statement;
012:
013: import de.uka.ilkd.key.java.*;
014: import de.uka.ilkd.key.java.declaration.ParameterDeclaration;
015: import de.uka.ilkd.key.java.visitor.Visitor;
016: import de.uka.ilkd.key.util.ExtList;
017:
018: /**
019: * Catch.
020: *
021: */
022: public class Catch extends BranchImp implements ParameterContainer,
023: VariableScope {
024:
025: /**
026: * Parameter.
027: */
028:
029: protected final ParameterDeclaration parameter;
030:
031: /**
032: * Body.
033: */
034:
035: protected final StatementBlock body;
036:
037: /**
038: * Catch.
039: */
040: public Catch() {
041: super ();
042: parameter = null;
043: body = null;
044: }
045:
046: /**
047: * Catch.
048: * @param e a parameter declaration.
049: * @param body a statement.
050: */
051: public Catch(ParameterDeclaration e, StatementBlock body) {
052: super ();
053: this .body = body;
054: parameter = e;
055: }
056:
057: /**
058: * Constructor for the transformation of COMPOST ASTs to KeY.
059: * @param children the children of this AST element as KeY classes.
060: * May contain: Comments,
061: * a ParameterDeclaration (declaring the catched
062: * exceptions)
063: * a StatementBlock (as the action to do when catching)
064: */
065: public Catch(ExtList children) {
066: super (children);
067: parameter = (ParameterDeclaration) children
068: .get(ParameterDeclaration.class);
069: body = (StatementBlock) children.get(StatementBlock.class);
070: }
071:
072: public SourceElement getLastElement() {
073: return (body != null) ? body.getLastElement() : this ;
074: }
075:
076: /**
077: * Returns the number of children of this node.
078: * @return an int giving the number of children of this node
079: */
080: public int getChildCount() {
081: int result = 0;
082: if (parameter != null)
083: result++;
084: if (body != null)
085: result++;
086: return result;
087: }
088:
089: /**
090: * Returns the child at the specified index in this node's "virtual"
091: * child array
092: * @param index an index into this node's "virtual" child array
093: * @return the program element at the given position
094: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
095: * of bounds
096: */
097: public ProgramElement getChildAt(int index) {
098: if (parameter != null) {
099: if (index == 0)
100: return parameter;
101: index--;
102: }
103: if (body != null) {
104: if (index == 0)
105: return body;
106: index--;
107: }
108: throw new ArrayIndexOutOfBoundsException();
109: }
110:
111: /**
112: * Get the number of statements in this container.
113: * @return the number of statements.
114: */
115: public int getStatementCount() {
116: return (body != null) ? 1 : 0;
117: }
118:
119: /*
120: Return the statement at the specified index in this node's
121: "virtual" statement array.
122: @param index an index for a statement.
123: @return the statement with the given index.
124: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
125: of bounds.
126: */
127: public Statement getStatementAt(int index) {
128: if (body != null && index == 0) {
129: return body;
130: }
131: throw new ArrayIndexOutOfBoundsException();
132: }
133:
134: /**
135: * Get the number of parameters in this container.
136: * @return the number of parameters.
137: */
138: public int getParameterDeclarationCount() {
139: return (parameter != null) ? 1 : 0;
140: }
141:
142: /*
143: Return the parameter declaration at the specified index in this node's
144: "virtual" parameter declaration array.
145: @param index an index for a parameter declaration.
146: @return the parameter declaration with the given index.
147: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
148: of bounds.
149: */
150: public ParameterDeclaration getParameterDeclarationAt(int index) {
151: if (parameter != null && index == 0) {
152: return parameter;
153: }
154: throw new ArrayIndexOutOfBoundsException();
155: }
156:
157: /**
158: * Get body.
159: * @return the statement.
160: */
161: public Statement getBody() {
162: return body;
163: }
164:
165: /**
166: * Get parameter declaration.
167: * @return the parameter declaration.
168: */
169: public ParameterDeclaration getParameterDeclaration() {
170: return parameter;
171: }
172:
173: /** calls the corresponding method of a visitor in order to
174: * perform some action/transformation on this element
175: * @param v the Visitor
176: */
177: public void visit(Visitor v) {
178: v.performActionOnCatch(this );
179: }
180:
181: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
182: p.printCatch(this);
183: }
184: }
|