2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.vm;
20 import java.lang.reflect.Modifier;
24 * type, name and attribute information of a field.
26 public abstract class FieldInfo extends InfoObject implements GenericSignatureHolder {
28 //--- FieldInfo attributes
29 // don't break transitions on get/putXX insns of this field, even if shared
30 static final int NEVER_BREAK = 0x10000;
32 // always break on this field's access if object is shared
33 // (ignored if NEVER_BREAK is set)
34 static final int BREAK_SHARED = 0x20000;
36 // those might relate to sticky ElementInto.ATTR_*
37 protected int attributes;
40 protected final String name;
41 protected String type; // lazy initialized fully qualified type name as per JLS 6.7 ("int", "x.Y[]")
42 protected final String signature; // "I", "[Lx/Y;" etc.
43 protected int storageSize;
45 protected ClassInfo ci; // class this field belongs to
46 protected int fieldIndex; // declaration ordinal
48 // where in the corresponding Fields object do we store the value
49 // (note this works because of the wonderful single inheritance)
50 protected int storageOffset;
52 // optional initializer for this field, can't be final because it is set from
53 // classfile field_info attributes (i.e. after construction)
56 protected String genericSignature;
58 protected int modifiers;
60 public static FieldInfo create (String name, String signature, int modifiers){
61 switch(signature.charAt(0)){
63 return new BooleanFieldInfo(name, modifiers);
65 return new ByteFieldInfo(name, modifiers);
67 return new ShortFieldInfo(name, modifiers);
69 return new CharFieldInfo(name, modifiers);
71 return new IntegerFieldInfo(name, modifiers);
73 return new LongFieldInfo(name, modifiers);
75 return new FloatFieldInfo(name, modifiers);
77 return new DoubleFieldInfo(name, modifiers);
79 return new ReferenceFieldInfo(name, signature, modifiers);
83 protected FieldInfo(String name, String signature, int modifiers) {
85 this.signature = signature;
86 this.modifiers = modifiers;
89 protected void linkToClass (ClassInfo ci, int idx, int off){
91 this.fieldIndex = idx;
92 this.storageOffset = off;
95 // those are set subsequently from classfile attributes
96 public void setConstantValue(Object constValue){
100 public abstract String valueToString (Fields f);
102 public boolean is1SlotField(){
105 public boolean is2SlotField(){
109 public boolean isBooleanField() {
112 public boolean isByteField() {
115 public boolean isCharField() {
118 public boolean isShortField() {
121 public boolean isIntField() {
124 public boolean isLongField() {
127 public boolean isFloatField(){
130 public boolean isDoubleField(){
134 public boolean isNumericField(){
138 public boolean isFloatingPointField(){
142 public boolean isReference () {
146 public boolean isArrayField () {
151 * Returns the class that this field is associated with.
153 public ClassInfo getClassInfo () {
157 public Object getConstantValue () {
161 public abstract Object getValueObject (Fields data);
163 public int getModifiers() {
167 public int getFieldIndex () {
172 * is this a static field? Counter productive to the current class struct,
173 * but at some point we want to get rid of the Dynamic/Static branch (it's
174 * really just a field attribute)
176 public boolean isStatic () {
177 return (modifiers & Modifier.STATIC) != 0;
181 * is this field declared `final'?
183 public boolean isFinal () {
184 return (modifiers & Modifier.FINAL) != 0;
187 public boolean isVolatile () {
188 return (modifiers & Modifier.VOLATILE) != 0;
191 public boolean isTransient () {
192 return (modifiers & Modifier.TRANSIENT) != 0;
195 public boolean isPublic () {
196 return (modifiers & Modifier.PUBLIC) != 0;
199 public boolean isPrivate () {
200 return (modifiers & Modifier.PRIVATE) != 0;
203 public boolean isProtected () {
204 return (modifiers & Modifier.PROTECTED) != 0;
207 public boolean isPackagePrivate() {
208 return (modifiers & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE)) == 0;
212 * Returns the name of the field.
214 public String getName () {
219 * @return the storage size of this field, @see Types.getTypeSize
221 public int getStorageSize () {
226 * Returns the type of the field as a fully qualified type name according to JLS 6.7
229 public String getType () {
231 type = Types.getTypeName(signature);
236 public byte getTypeCode (){
237 return Types.getTypeCode(signature);
240 public String getSignature(){
245 public String getGenericSignature() {
246 return genericSignature;
250 public void setGenericSignature(String sig){
251 genericSignature = sig;
254 public ClassInfo getTypeClassInfo () {
255 return ClassLoaderInfo.getCurrentResolvedClassInfo(getType());
258 public Class<? extends ChoiceGenerator<?>> getChoiceGeneratorType (){
263 * pushClinit the corresponding data in the provided Fields instance
265 public abstract void initialize (ElementInfo ei, ThreadInfo ti);
269 * Returns a string representation of the field.
272 public String toString () {
273 StringBuilder sb = new StringBuilder();
276 sb.append("static ");
282 //sb.append(Types.getTypeName(type));
283 sb.append(getType());
285 if (ci != null){ // maybe the fieldinfo isn't linked yet
286 sb.append(ci.getName());
291 return sb.toString();
294 //--- those are the JPF internal attribute flags (not to mix with free user attrs)
296 void setAttributes (int a) {
300 public void addAttribute (int a){
304 public int getAttributes () {
308 public boolean breakShared() {
309 return ((attributes & BREAK_SHARED) != 0);
312 public boolean neverBreak() {
313 return ((attributes & NEVER_BREAK) != 0);
316 public int getStorageOffset () {
317 return storageOffset;
320 public String getFullName() {
321 return ci.getName() + '.' + name;
325 * Creates a field for a given class, by cloning this FieldInfo
326 * and reseting the class that the field belongs to
328 public FieldInfo getInstanceFor(ClassInfo ci) {
332 clone = (FieldInfo)clone();
334 } catch (CloneNotSupportedException cnsx){
335 cnsx.printStackTrace();