001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: import org.apache.bcel.classfile.*;
022:
023: /**
024: * type, name and attribute information of a field.
025: */
026: public abstract class FieldInfo {
027: /**
028: * Name of the field.
029: */
030: protected final String name;
031:
032: /**
033: * Type of the field.
034: */
035: protected final String type;
036:
037: /**
038: * Class the field belongs to.
039: */
040: protected final ClassInfo ci;
041:
042: /**
043: * Static field.
044: */
045: protected final boolean isStatic;
046:
047: /**
048: * the (possibly null) initializer value for this field.
049: */
050: protected final ConstantValue cv;
051:
052: /**
053: * what is the order of declaration of this field
054: */
055: int fieldIndex;
056:
057: /**
058: * where in the corresponding Fields object do we store the value
059: * (note this works because of the wonderful single inheritance)
060: */
061: int storageOffset;
062:
063: /**
064: * high 16 bit: non-propagation relevant field attributes
065: * low 16 bit: object attribute propagation mask
066: */
067: int attributes = ElementInfo.ATTR_PROP_MASK;
068:
069: protected FieldInfo(String name, String type, boolean isStatic,
070: ConstantValue cv, ClassInfo ci, int idx, int off) {
071: this .name = name;
072: this .type = type;
073: this .isStatic = isStatic;
074: this .ci = ci;
075: this .cv = cv;
076: this .fieldIndex = idx;
077: this .storageOffset = off;
078: }
079:
080: public static FieldInfo create(Field f, ClassInfo ci, int idx,
081: int off) {
082: return create(f.getName(), f.getSignature(), f.isStatic(), f
083: .getConstantValue(), ci, idx, off);
084: }
085:
086: /**
087: * create the correct kind of FieldInfo with respect to <code>type</code>
088: *
089: * @param name the name of the field
090: * @param type the type of the field
091: * @param isStatic the staticness of teh field
092: * @param cv the constant initializer for the field (possibly null)
093: * @param ci the class of the field
094: * @return the right kind of FieldInfo
095: */
096: public static FieldInfo create(String name, String type,
097: boolean isStatic, ConstantValue cv, ClassInfo ci, int idx,
098: int off) {
099: switch (Types.getBaseType(type)) {
100: case Types.T_CHAR:
101: case Types.T_BYTE:
102: case Types.T_INT:
103: case Types.T_SHORT:
104: case Types.T_BOOLEAN:
105: return new IntegerFieldInfo(name, type, isStatic, cv, ci,
106: idx, off);
107:
108: case Types.T_FLOAT:
109: return new FloatFieldInfo(name, type, isStatic, cv, ci,
110: idx, off);
111:
112: case Types.T_DOUBLE:
113: return new DoubleFieldInfo(name, type, isStatic, cv, ci,
114: idx, off);
115:
116: case Types.T_LONG:
117: return new LongFieldInfo(name, type, isStatic, cv, ci, idx,
118: off);
119:
120: case Types.T_ARRAY:
121: case Types.T_REFERENCE:
122: return new ReferenceFieldInfo(name, type, isStatic, cv, ci,
123: idx, off);
124:
125: default:
126: throw new InternalError("bad base type! " + type + " "
127: + Types.getBaseType(type));
128: }
129: }
130:
131: public abstract String valueToString(Fields f);
132:
133: /**
134: * Returns the class that this field is associated with.
135: */
136: public ClassInfo getClassInfo() {
137: return ci;
138: }
139:
140: public ConstantValue getConstantValue() {
141: return cv;
142: }
143:
144: public int getFieldIndex() {
145: return fieldIndex;
146: }
147:
148: public boolean isReference() {
149: return false;
150: }
151:
152: public boolean isArrayField() {
153: return false;
154: }
155:
156: /**
157: * is this a static field? Counter productive to the current class struct,
158: * but at some point we want to get rid of the Dynamic/Static branch (it's
159: * really just a field attribute)
160: */
161: public boolean isStatic() {
162: return isStatic;
163: }
164:
165: /**
166: * Returns the name of the field.
167: */
168: public String getName() {
169: return name;
170: }
171:
172: /**
173: * @return the storage size of this field, @see Types.getTypeSize
174: */
175: public int getStorageSize() {
176: return Types.getTypeSize(type);
177: }
178:
179: /**
180: * Returns the type of the field.
181: */
182: public String getType() {
183: return type;
184: }
185:
186: /**
187: * initialize the corresponding data in the provided Fields instance
188: */
189: public abstract void initialize(Fields f);
190:
191: /**
192: * Returns a string representation of the field.
193: */
194: public String toString() {
195: StringBuffer sb = new StringBuffer();
196:
197: if (isStatic) {
198: sb.append("static ");
199: }
200:
201: sb.append(Types.getTypeName(type));
202: sb.append(" ");
203: sb.append(ci.getName());
204: sb.append(".");
205: sb.append(name);
206:
207: return sb.toString();
208: }
209:
210: void setAttributes(int a) {
211: attributes = a;
212: }
213:
214: public int getAttributes() {
215: return attributes;
216: }
217:
218: public int getStorageOffset() {
219: return storageOffset;
220: }
221: }
|