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.PrettyPrinter;
014: import de.uka.ilkd.key.java.ProgramElement;
015: import de.uka.ilkd.key.java.reference.TypeReference;
016: import de.uka.ilkd.key.java.visitor.Visitor;
017: import de.uka.ilkd.key.util.ExtList;
018:
019: /**
020: * Field declaration.
021: * taken from COMPOST and changed to achieve an immutable structure
022: */
023:
024: public class FieldDeclaration extends VariableDeclaration implements
025: MemberDeclaration {
026:
027: /**
028: * Field specs.
029: */
030:
031: protected final ArrayOfFieldSpecification fieldSpecs;
032:
033: /**
034: * Field declaration.
035: * @param mods a modifier mutable list.
036: * @param typeRef a type reference.
037: * @param vars a variable specification array.
038: * @param parentIsInterfaceDeclaration a boolean set true
039: * iff parent is an InterfaceDeclaration
040: */
041:
042: public FieldDeclaration(Modifier[] mods, TypeReference typeRef,
043: FieldSpecification[] vars,
044: boolean parentIsInterfaceDeclaration) {
045: super (mods, typeRef, parentIsInterfaceDeclaration);
046: fieldSpecs = new ArrayOfFieldSpecification(vars);
047: }
048:
049: /**
050: * Field declaration.
051: * @param children an ExtList of children. May include:
052: * several FieldSpecification (for the field)
053: * a TypeReference (as reference to the type of the declared variable)
054: * several Modifier (taken as modifiers of the declaration),
055: * a Comment
056: * @param parentIsInterfaceDeclaration a boolean set true
057: */
058: public FieldDeclaration(ExtList children,
059: boolean parentIsInterfaceDeclaration) {
060: super (children, parentIsInterfaceDeclaration);
061: fieldSpecs = new ArrayOfFieldSpecification(
062: (FieldSpecification[]) children
063: .collect(FieldSpecification.class));
064: }
065:
066: public ArrayOfFieldSpecification getFieldSpecifications() {
067: return fieldSpecs;
068: }
069:
070: public ArrayOfVariableSpecification getVariables() {
071: return fieldSpecs;
072: }
073:
074: /**
075: * Returns the number of children of this node.
076: * @return an int giving the number of children of this node
077: */
078:
079: public int getChildCount() {
080: int result = 0;
081: if (modArray != null)
082: result += modArray.size();
083: if (typeReference != null)
084: result++;
085: if (fieldSpecs != null)
086: result += fieldSpecs.size();
087: return result;
088: }
089:
090: /**
091: * Returns the child at the specified index in this node's "virtual"
092: * child array
093: * @param index an index into this node's "virtual" child array
094: * @return the program element at the given position
095: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
096: * of bounds
097: */
098: public ProgramElement getChildAt(int index) {
099: int len;
100: if (modArray != null) {
101: len = modArray.size();
102: if (len > index) {
103: return modArray.getModifier(index);
104: }
105: index -= len;
106: }
107: if (typeReference != null) {
108: if (index == 0)
109: return typeReference;
110: index--;
111: }
112: if (fieldSpecs != null) {
113: return fieldSpecs.getFieldSpecification(index);
114: }
115: throw new ArrayIndexOutOfBoundsException();
116: }
117:
118: /**
119: * Test whether the declaration is final. Fields of interfaces are
120: * always final.
121: */
122:
123: public boolean isFinal() {
124: return parentIsInterfaceDeclaration || super .isFinal();
125: }
126:
127: /**
128: * Test whether the declaration is private.
129: */
130:
131: public boolean isPrivate() {
132: return super .isPrivate();
133: }
134:
135: /**
136: * Test whether the declaration is protected.
137: */
138:
139: public boolean isProtected() {
140: return super .isProtected();
141: }
142:
143: /**
144: * Test whether the declaration is public. Fields of interfaces
145: * are always public.
146: */
147:
148: public boolean isPublic() {
149: return parentIsInterfaceDeclaration || super .isPublic();
150: }
151:
152: /**
153: * Test whether the declaration is static. Fields of interfaces
154: * are always static.
155: */
156:
157: public boolean isStatic() {
158: return parentIsInterfaceDeclaration || super .isStatic();
159: }
160:
161: /**
162: * Test whether the declaration is transient.
163: */
164:
165: public boolean isTransient() {
166: return !parentIsInterfaceDeclaration && super .isTransient();
167: }
168:
169: /**
170: * Test whether the declaration is strict FP.
171: */
172:
173: public boolean isStrictFp() {
174: return super .isStrictFp();
175: }
176:
177: /** calls the corresponding method of a visitor in order to
178: * perform some action/transformation on this element
179: * @param v the Visitor
180: */
181: public void visit(Visitor v) {
182: v.performActionOnFieldDeclaration(this );
183: }
184:
185: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
186: p.printFieldDeclaration(this);
187: }
188: }
|