KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > FieldInfo


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import org.apache.bcel.classfile.*;
22
23
24 /**
25  * type, name and attribute information of a field.
26  */

27 public abstract class FieldInfo {
28   /**
29    * Name of the field.
30    */

31   protected final String JavaDoc name;
32
33   /**
34    * Type of the field.
35    */

36   protected final String JavaDoc type;
37
38   /**
39    * Class the field belongs to.
40    */

41   protected final ClassInfo ci;
42   
43   /**
44    * Static field.
45    */

46   protected final boolean isStatic;
47
48   /**
49    * the (possibly null) initializer value for this field.
50    */

51   protected final ConstantValue cv;
52   
53   /**
54    * what is the order of declaration of this field
55    */

56   int fieldIndex;
57   
58   /**
59    * where in the corresponding Fields object do we store the value
60    * (note this works because of the wonderful single inheritance)
61    */

62   int storageOffset;
63   
64   /**
65    * high 16 bit: non-propagation relevant field attributes
66    * low 16 bit: object attribute propagation mask
67    */

68   int attributes = ElementInfo.ATTR_PROP_MASK;
69
70   protected FieldInfo (String JavaDoc name, String JavaDoc type, boolean isStatic,
71                        ConstantValue cv, ClassInfo ci, int idx, int off) {
72     this.name = name;
73     this.type = type;
74     this.isStatic = isStatic;
75     this.ci = ci;
76     this.cv = cv;
77     this.fieldIndex = idx;
78     this.storageOffset = off;
79   }
80
81   public static FieldInfo create (Field f, ClassInfo ci, int idx, int off) {
82     return create(f.getName(), f.getSignature(), f.isStatic(),
83                   f.getConstantValue(), ci, idx, off);
84   }
85
86   /**
87    * create the correct kind of FieldInfo with respect to <code>type</code>
88    *
89    * @param name the name of the field
90    * @param type the type of the field
91    * @param isStatic the staticness of teh field
92    * @param cv the constant initializer for the field (possibly null)
93    * @param ci the class of the field
94    * @return the right kind of FieldInfo
95    */

96   public static FieldInfo create (String JavaDoc name, String JavaDoc type, boolean isStatic,
97                                   ConstantValue cv, ClassInfo ci, int idx, int off) {
98     switch (Types.getBaseType(type)) {
99     case Types.T_CHAR:
100     case Types.T_BYTE:
101     case Types.T_INT:
102     case Types.T_SHORT:
103     case Types.T_BOOLEAN:
104       return new IntegerFieldInfo(name, type, isStatic, cv, ci, idx, off);
105
106     case Types.T_FLOAT:
107       return new FloatFieldInfo(name, type, isStatic, cv, ci, idx, off);
108
109     case Types.T_DOUBLE:
110       return new DoubleFieldInfo(name, type, isStatic, cv, ci, idx, off);
111
112     case Types.T_LONG:
113       return new LongFieldInfo(name, type, isStatic, cv, ci, idx, off);
114
115     case Types.T_ARRAY:
116     case Types.T_REFERENCE:
117       return new ReferenceFieldInfo(name, type, isStatic, cv, ci, idx, off);
118
119     default:
120       throw new InternalError JavaDoc("bad base type! " + type + " " +
121                               Types.getBaseType(type));
122     }
123   }
124
125   public abstract String JavaDoc valueToString (Fields f);
126   
127   /**
128    * Returns the class that this field is associated with.
129    */

130   public ClassInfo getClassInfo () {
131     return ci;
132   }
133
134   public ConstantValue getConstantValue () {
135     return cv;
136   }
137
138   public int getFieldIndex () {
139     return fieldIndex;
140   }
141   
142   public boolean isReference () {
143     return false;
144   }
145   
146   public boolean isArrayField () {
147     return false;
148   }
149   
150   /**
151    * is this a static field? Counter productive to the current class struct,
152    * but at some point we want to get rid of the Dynamic/Static branch (it's
153    * really just a field attribute)
154    */

155   public boolean isStatic () {
156     return isStatic;
157   }
158   
159   /**
160    * Returns the name of the field.
161    */

162   public String JavaDoc getName () {
163     return name;
164   }
165
166   /**
167    * @return the storage size of this field, @see Types.getTypeSize
168    */

169   public int getStorageSize () {
170     return Types.getTypeSize(type);
171   }
172
173   /**
174    * Returns the type of the field.
175    */

176   public String JavaDoc getType () {
177     return type;
178   }
179
180   /**
181    * initialize the corresponding data in the provided Fields instance
182    */

183   public abstract void initialize (Fields f);
184   
185   /**
186    * Returns a string representation of the field.
187    */

188   public String JavaDoc toString () {
189     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
190
191     if (isStatic) {
192       sb.append("static ");
193     }
194
195     sb.append(Types.getTypeName(type));
196     sb.append(" ");
197     sb.append(ci.getName());
198     sb.append(".");
199     sb.append(name);
200
201     return sb.toString();
202   }
203   
204   void setAttributes (int a) {
205     attributes = a;
206   }
207   
208   public int getAttributes () {
209     return attributes;
210   }
211   
212   public int getStorageOffset () {
213     return storageOffset;
214   }
215 }
216
Popular Tags