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.Declaration;
014: import de.uka.ilkd.key.java.JavaNonTerminalProgramElement;
015: import de.uka.ilkd.key.java.declaration.modifier.*;
016: import de.uka.ilkd.key.util.ExtList;
017:
018: /**
019: * Java declaration.
020: * taken from COMPOST and changed to achieve an immutable structure
021: */
022:
023: public abstract class JavaDeclaration extends
024: JavaNonTerminalProgramElement implements Declaration {
025:
026: /**
027: * Modifiers.
028: * caches the wrapper for the modifiers. The wrapper is needed to get access
029: * to the array without hurting immutabilitiy */
030: protected final ArrayOfModifier modArray;
031:
032: /**
033: * Java declaration.
034: */
035: public JavaDeclaration() {
036: modArray = null;
037: }
038:
039: public JavaDeclaration(Modifier[] mods) {
040: modArray = new ArrayOfModifier(mods);
041: }
042:
043: public JavaDeclaration(ArrayOfModifier mods) {
044: modArray = mods;
045: }
046:
047: /**
048: * Constructor for the transformation of COMPOST ASTs to KeY.
049: * @param children the children of this AST element as KeY classes. May
050: * include: several Modifier (taken as modifiers of the declaration),
051: * a Comment
052: */
053: public JavaDeclaration(ExtList children) {
054: super (children);
055: modArray = new ArrayOfModifier((Modifier[]) children
056: .collect(Modifier.class));
057: }
058:
059: /**
060: * Get modifiers.
061: * @return the modifier array wrapper.
062: */
063:
064: public ArrayOfModifier getModifiers() {
065: return modArray;
066: }
067:
068: /**
069: * Returns a Public, Protected, or Private Modifier, if there
070: * is one, null otherwise. A return value of null can usually be
071: * interpreted as package visibility.
072: */
073:
074: public VisibilityModifier getVisibilityModifier() {
075: if (modArray == null) {
076: return null;
077: }
078: for (int i = modArray.size() - 1; i >= 0; i -= 1) {
079: Modifier m = modArray.getModifier(i);
080: if (m instanceof VisibilityModifier) {
081: return (VisibilityModifier) m;
082: }
083: }
084: return null;
085: }
086:
087: private boolean containsModifier(Class type) {
088: int s = (modArray == null) ? 0 : modArray.size();
089: for (int i = 0; i < s; i += 1) {
090: if (type.isInstance(modArray.getModifier(i))) {
091: return true;
092: }
093: }
094: return false;
095: }
096:
097: /**
098: * Test whether the declaration is abstract.
099: */
100: protected boolean isAbstract() {
101: return containsModifier(Abstract.class);
102: }
103:
104: /**
105: * Test whether the declaration is private.
106: */
107:
108: protected boolean isPrivate() {
109: return containsModifier(Private.class);
110: }
111:
112: /**
113: * Test whether the declaration is protected.
114: */
115:
116: protected boolean isProtected() {
117: return containsModifier(Protected.class);
118: }
119:
120: /**
121: * Test whether the declaration is public.
122: */
123:
124: protected boolean isPublic() {
125: return containsModifier(Public.class);
126: }
127:
128: /**
129: * Test whether the declaration is static.
130: */
131:
132: protected boolean isStatic() {
133: return containsModifier(Static.class);
134: }
135:
136: /**
137: * Test whether the declaration is transient.
138: */
139:
140: protected boolean isTransient() {
141: return containsModifier(Transient.class);
142: }
143:
144: /**
145: * Test whether the declaration is model (the jml modifier is meant).
146: */
147:
148: protected boolean isModel() {
149: return containsModifier(Model.class);
150: }
151:
152: /**
153: * Test whether the declaration is volatile.
154: */
155:
156: protected boolean isVolatile() {
157: return containsModifier(Volatile.class);
158: }
159:
160: /**
161: * Test whether the declaration is strictfp.
162: */
163:
164: protected boolean isStrictFp() {
165: return containsModifier(StrictFp.class);
166: }
167:
168: /**
169: * Test whether the declaration is final.
170: */
171:
172: protected boolean isFinal() {
173: return containsModifier(Final.class);
174: }
175:
176: /**
177: * Test whether the declaration is native.
178: */
179:
180: protected boolean isNative() {
181: return containsModifier(Native.class);
182: }
183:
184: /**
185: * Test whether the declaration is synchronized.
186: */
187:
188: protected boolean isSynchronized() {
189: return containsModifier(Synchronized.class);
190: }
191:
192: }
|