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.recoderext;
012:
013: import recoder.java.*;
014: import recoder.java.declaration.ParameterDeclaration;
015: import recoder.java.statement.JavaStatement;
016:
017: /**
018: * A shortcut-statement for a method body.
019: */
020:
021: public class CatchAllStatement extends JavaStatement implements
022: StatementContainer, ParameterContainer {
023:
024: /**
025: * the ast parent
026: */
027: private StatementContainer astParent;
028:
029: protected StatementBlock body;
030: protected ParameterDeclaration paramdecl;
031:
032: /**
033: * Construct a method body shortcut
034: * @param r the ParameterDeclaration of the catch clause
035: * @param body the StatementBlock representing the catch clause's body
036: */
037: public CatchAllStatement(ParameterDeclaration r, StatementBlock body) {
038: this .body = body;
039: paramdecl = r;
040: makeParentRoleValid();
041: }
042:
043: public NonTerminalProgramElement getASTParent() {
044: return astParent;
045: }
046:
047: public StatementContainer getStatementContainer() {
048: return astParent;
049: }
050:
051: public int getStatementCount() {
052: return (body == null) ? 0 : 1;
053: }
054:
055: public Statement getStatementAt(int i) {
056: return (i == 0) ? body : null;
057: }
058:
059: public int getParameterDeclarationCount() {
060: return (paramdecl == null) ? 0 : 1;
061: }
062:
063: public ParameterDeclaration getParameterDeclarationAt(int i) {
064: return (i == 0) ? paramdecl : null;
065: }
066:
067: public void setStatementContainer(StatementContainer parent) {
068: astParent = parent;
069: }
070:
071: /**
072: * Finds the source element that occurs first in the source.
073: * @return the last source element in the syntactical representation of
074: * this element, may be equals to this element.
075: */
076:
077: public SourceElement getFirstElement() {
078: return getChildAt(0).getFirstElement();
079: }
080:
081: /**
082: * Finds the source element that occurs last in the source.
083: * @return the last source element in the syntactical representation of
084: * this element, may be equals to this element.
085: */
086:
087: public SourceElement getLastElement() {
088: return getChildAt(getChildCount() - 1).getLastElement();
089: }
090:
091: /**
092: * Returns the number of children of this node.
093: * @return an int giving the number of children of this node
094: */
095:
096: public int getChildCount() {
097: int result = 0;
098: if (body != null)
099: result++;
100: if (paramdecl != null)
101: result++;
102: return result;
103: }
104:
105: /**
106: * Returns the child at the specified index in this node's "virtual"
107: * child array
108: * @param index an index into this node's "virtual" child array
109: * @return the program element at the given position
110: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
111: * of bounds
112: */
113:
114: public ProgramElement getChildAt(int index) {
115: if (paramdecl != null) {
116: if (index == 0)
117: return paramdecl;
118: index--;
119: }
120: if (body != null) {
121: if (index == 0)
122: return body;
123: index--;
124: }
125: throw new ArrayIndexOutOfBoundsException();
126: }
127:
128: /**
129: * Replace a single child in the current node.
130: * The child to replace is matched by identity and hence must be known
131: * exactly. The replacement element can be null - in that case, the child
132: * is effectively removed.
133: * The parent role of the new child is validated, while the
134: * parent link of the replaced child is left untouched.
135: * @param p the old child.
136: * @param q the new child.
137: * @return true if a replacement has occured, false otherwise.
138: * @exception ClassCastException if the new child cannot take over
139: * the role of the old one.
140: */
141:
142: public boolean replaceChild(ProgramElement p, ProgramElement q) {
143:
144: return false;
145: }
146:
147: /**
148: * Ensures that each child has "this" as syntactical parent.
149: */
150: public void makeParentRoleValid() {
151: super .makeParentRoleValid();
152: if (body != null) {
153: body.setStatementContainer(this );
154: }
155: if (paramdecl != null) {
156: paramdecl.setParameterContainer(this );
157: }
158: }
159:
160: public int getChildPositionCode(ProgramElement child) {
161: if (paramdecl == child) {
162: return 0;
163: }
164: if (body == child) {
165: return 1;
166: }
167: return -1;
168: }
169:
170: //don't think we need it
171: public void accept(SourceVisitor v) {
172: throw new IllegalStateException("Not implemented in "
173: + "CatchAllStatement");
174: }
175:
176: //don't think we need it
177: public Object deepClone() {
178: throw new IllegalStateException("Not implemented in "
179: + "CatchAllStatement");
180: }
181:
182: public String getName() {
183: return "catchAll" + "(" + paramdecl + ") {" + body + " }";
184: }
185:
186: }
|