KickJava   Java API By Example, From Geeks To Geeks.

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


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 gov.nasa.jpf.JPFException;
22
23 /**
24  * A specialized version of ElementInfo for use in the StaticArea. The
25  * StaticElementInfo is only used to store "static class fields" in the
26  * StaticArea. It specifically knows about the relationship amongst
27  * classes, and will recursively lookup a data member if needed.
28  *
29  * @see gov.nasa.jpf.jvm.ElementInfo
30  */

31 public class StaticElementInfo extends ElementInfo {
32   static final int storingDataLength = ElementInfo.storingDataLength +1;
33   int classObjectRef = -1;
34
35   public StaticElementInfo () {
36   }
37
38   public StaticElementInfo (Fields f, Monitor m, int classObjRef) {
39     super(f, m);
40     classObjectRef = classObjRef;
41   }
42
43   protected FieldInfo getFieldInfo (String JavaDoc clsBase, String JavaDoc fname) {
44     ClassInfo ci = getClassInfo();
45     FieldInfo fi = ci.getStaticField(clsBase, fname);
46     
47     if (fi == null) {
48       throw new JPFException("class " + ci.getName() +
49                                          " has no static field " + fname);
50     }
51     return fi;
52   }
53   
54   public int getNumberOfFields () {
55     return getClassInfo().getNumberOfStaticFields();
56   }
57   
58   public FieldInfo getFieldInfo (int fieldIndex) {
59     return getClassInfo().getStaticField(fieldIndex);
60   }
61   
62   protected ElementInfo getElementInfo (ClassInfo ci) {
63     if (ci == getClassInfo()) {
64       return this;
65     } else {
66       return ((StaticArea)area).get( ci.getName());
67     }
68   }
69   
70   /**
71    * override the ElementInfo methods - we have an additional field
72    * (for the sake of efficiency, we duplicate some code to save
73    * a call, this is a high-frequency op)
74    */

75   public int getStoringDataLength () {
76     return storingDataLength;
77   }
78
79   public void backtrackTo (ArrayOffset storing, Object JavaDoc backtrack) {
80     super.backtrackTo(storing, backtrack);
81    
82     classObjectRef = storing.get();
83   }
84
85   /**
86    * mark all our fields as static (shared) reachable. No need to set our own
87    * attributes, since we reside in the StaticArea
88    * @aspects: gc
89    */

90   void markStaticRoot () {
91     // WATCH IT! this overrides the heap object behavior in our super class.
92
// See ElementInfo.markStaticRoot() for details
93

94     DynamicArea heap = DynamicArea.getHeap();
95     ClassInfo ci = getClassInfo();
96     int n = ci.getNumberOfStaticFields();
97     
98     for (int i=0; i<n; i++) {
99       FieldInfo fi = ci.getStaticField(i);
100       if (fi.isReference()) {
101         heap.markStaticRoot(fields.getIntValue(fi.getStorageOffset()));
102       }
103     }
104     
105     // don't forget the class object itself (which is not a field)
106
heap.markStaticRoot(classObjectRef);
107   }
108       
109   public int storeDataTo (int[] buffer, int idx) {
110     idx += super.storeDataTo(buffer, idx);
111     
112     buffer[idx] = classObjectRef;
113
114     return 3;
115   }
116
117   protected Ref getRef () {
118     return new ClassRef(getIndex());
119   }
120
121   int getClassObjectRef () {
122     return classObjectRef;
123   }
124
125   public String JavaDoc toString() {
126     return getClassInfo().getName(); // don't append index (useless and misleading for statics)
127
}
128   
129   private void blowup () {
130     throw new JPFException("cannot access StaticElementInfo by index");
131   }
132 }
133
134
Popular Tags