KickJava   Java API By Example, From Geeks To Geeks.

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


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 import gov.nasa.jpf.JPFException;
23
24
25 /**
26  * field info for object fields
27  */

28 public class ReferenceFieldInfo extends FieldInfo {
29   int init=-1;
30   String JavaDoc sInit; // <2do> pcm - just a temporary quirk to init from string literals
31
// check if there are other non-object reference inits
32

33   public ReferenceFieldInfo (String JavaDoc name, String JavaDoc type, boolean isStatic,
34                              ConstantValue cv, ClassInfo ci, int idx, int off) {
35     super(name, type, isStatic, cv, ci, idx, off);
36     init = computeInitValue(cv);
37   }
38
39   public String JavaDoc valueToString (Fields f) {
40     int i = f.getIntValue(storageOffset);
41     if (i == -1) {
42       return "null";
43     } else {
44       return DynamicArea.getHeap().get(i).toString();
45     }
46   }
47   
48   public boolean isReference () {
49     return true;
50   }
51   
52   public boolean isArrayField () {
53     return ci.isArray;
54   }
55   
56   int computeInitValue (ConstantValue cv) {
57     // <2do> pcm - check what other constants we might encounter, this is most
58
// probably not just used for Strings.
59
// Besides the type issue, there is an even bigger problem with identities.
60
// For instance, all String refs initialized via the same string literal
61
// inside a single classfile are in fact refering to the same object. This
62
// means we have to keep a registry (hashtab) with string-literal created
63
// String objects per ClassInfo, and use this when we assign or init
64
// String references.
65
// For the sake of progress, we ignore this for now, but have to come back
66
// to it because it violates the VM spec
67

68     if (cv == null) return -1;
69     
70     // here the mess starts
71
//DynamicArea heap = DynamicArea.getHeap();
72
String JavaDoc s = cv.toString();
73     
74     if (s.charAt(0) == '"') {
75       s = s.substring(1,s.length()-1); // chop off the double quotes
76
sInit = s;
77       
78       //init = heap.newString(s, null); // turn literal into a string object
79
// but how do we pin it down?
80
} else {
81       throw new JPFException ("unsupported reference initialization: " + s);
82     }
83     
84     return -1;
85   }
86   
87   public void initialize (Fields f) {
88     int ref = init;
89     if (sInit != null) {
90       ref = DynamicArea.getHeap().newString(sInit, null);
91     }
92     f.setReferenceValue(storageOffset, ref);
93   }
94 }
95
Popular Tags