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.declaration;
012:
013: import de.uka.ilkd.key.java.*;
014: import de.uka.ilkd.key.java.declaration.modifier.Static;
015: import de.uka.ilkd.key.java.visitor.Visitor;
016: import de.uka.ilkd.key.util.ExtList;
017:
018: /**
019: * Class initializer.
020: * @author <TT>AutoDoc</TT>
021: */
022:
023: public class ClassInitializer extends JavaDeclaration implements
024: MemberDeclaration, StatementContainer {
025:
026: /**
027: * Body.
028: */
029:
030: protected final StatementBlock body;
031:
032: /**
033: * Class initializer.
034: */
035:
036: public ClassInitializer() {
037: super ((Modifier[]) null);
038: body = null;
039: }
040:
041: /**
042: * Class initializer.
043: * @param modifier a static.
044: * @param body a statement block.
045: */
046:
047: public ClassInitializer(Static modifier, StatementBlock body) {
048: super (new Modifier[] { modifier });
049: this .body = body;
050: }
051:
052: /**
053: * Class initializer.
054: * @param children list with all children. May include: a
055: * StatementBlock (taken as body of the ClassInitialiyer),
056: * several Modifier (taken as modifiers of the declaration), a Comment
057: */
058: public ClassInitializer(ExtList children) {
059: super (children);
060: body = (StatementBlock) children.get(StatementBlock.class);
061: }
062:
063: /**
064: * Get body.
065: * @return the statement block.
066: */
067:
068: public StatementBlock getBody() {
069: return body;
070: }
071:
072: /**
073: * Get the number of statements in this container.
074: * @return the number of statements.
075: */
076:
077: public int getStatementCount() {
078: return (body != null) ? 1 : 0;
079: }
080:
081: /*
082: Return the statement at the specified index in this node's
083: "virtual" statement array.
084: @param index an index for a statement.
085: @return the statement with the given index.
086: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
087: of bounds.
088: */
089:
090: public Statement getStatementAt(int index) {
091: if (body != null && index == 0) {
092: return body;
093: }
094: throw new ArrayIndexOutOfBoundsException();
095: }
096:
097: /**
098: * Returns the number of children of this node.
099: * @return an int giving the number of children of this node
100: */
101:
102: public int getChildCount() {
103: int result = 0;
104: if (modArray != null)
105: result += modArray.size();
106: if (body != null)
107: result++;
108: return result;
109: }
110:
111: /**
112: * Returns the child at the specified index in this node's "virtual"
113: * child array
114: * @param index an index into this node's "virtual" child array
115: * @return the program element at the given position
116: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
117: * of bounds
118: */
119:
120: public ProgramElement getChildAt(int index) {
121: int len;
122: if (modArray != null) {
123: len = modArray.size();
124: if (len > index) {
125: return modArray.getModifier(index);
126: }
127: index -= len;
128: }
129: if (body != null) {
130: if (index == 0)
131: return body;
132: }
133: throw new ArrayIndexOutOfBoundsException();
134: }
135:
136: /** A binary class initializer does not occur. */
137:
138: public boolean isBinary() {
139: return false;
140: }
141:
142: /**
143: * Initializers are never public.
144: */
145:
146: public boolean isPublic() {
147: return false;
148: }
149:
150: /**
151: * Initializers are never protected.
152: */
153:
154: public boolean isProtected() {
155: return false;
156: }
157:
158: /**
159: * Initializers are never private (at least not explicitly).
160: */
161:
162: public boolean isPrivate() {
163: return false;
164: }
165:
166: /**
167: * Initializers are never strictfp.
168: */
169:
170: public boolean isStrictFp() {
171: return false;
172: }
173:
174: /**
175: * Test whether the declaration is static.
176: */
177:
178: public boolean isStatic() {
179: return modArray != null && modArray.size() != 0;
180: }
181:
182: public SourceElement getLastElement() {
183: return body;
184: }
185:
186: /** calls the corresponding method of a visitor in order to
187: * perform some action/transformation on this element
188: * @param v the Visitor
189: */
190: public void visit(Visitor v) {
191: v.performActionOnClassInitializer(this );
192: }
193:
194: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
195: p.printClassInitializer(this);
196: }
197: }
|