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 gov.nasa.jpf.JPFException;
21 import gov.nasa.jpf.util.HashData;
24 * A specialized version of ElementInfo that is used for static fields. Each
25 * registered ClassInfo instance has its own StaticElementInfo instance
27 public final class StaticElementInfo extends ElementInfo {
29 // this is kind of dangerous - make sure these flags are still unused in ElementInfo
30 static final int ATTR_COR_CHANGED = 0x100000;
31 static final int ATTR_STATUS_CHANGED = 0x200000;
33 static final int ATTR_ANY_CHANGED = ElementInfo.ATTR_ANY_CHANGED | ATTR_COR_CHANGED | ATTR_STATUS_CHANGED;
35 protected int classObjectRef = MJIEnv.NULL;
36 protected int status = ClassInfo.UNINITIALIZED;
39 public StaticElementInfo () {
42 public StaticElementInfo (int id, ClassInfo ci, Fields f, Monitor m, ThreadInfo ti, ElementInfo eiClsObj) {
43 super(id, ci, f, m, ti);
45 // startup classes don't have a class object yet
46 if (eiClsObj != null) {
47 classObjectRef = eiClsObj.getObjectRef();
50 ti.getScheduler().initializeClassSharedness(ti, this);
54 public ElementInfo getModifiableInstance() {
58 Statics statics = ci.getStatics();
59 return statics.getModifiable( objRef);
64 public boolean isObject(){
69 public boolean isArray(){
74 public boolean hasFinalizer() {
79 protected int getNumberOfFieldsOrElements(){
80 // static fields can't be arrays, those are always heap objects
81 return ci.getNumberOfStaticFields();
85 public boolean hasChanged() {
86 return (attributes & ATTR_ANY_CHANGED) != 0;
90 public void markUnchanged() {
91 attributes &= ~ATTR_ANY_CHANGED;
95 public void hash(HashData hd) {
97 hd.add(classObjectRef);
102 public boolean equals(Object o) {
103 if (super.equals(o) && o instanceof StaticElementInfo) {
104 StaticElementInfo other = (StaticElementInfo) o;
106 if (classObjectRef != other.classObjectRef) {
109 if (status != other.status) {
122 public boolean isShared() {
123 // static fields are always thread global
128 public int getStatus() {
132 void setStatus (int newStatus) {
135 if (status != newStatus) {
137 attributes |= ATTR_STATUS_CHANGED;
143 protected FieldInfo getDeclaredFieldInfo (String clsBase, String fname) {
144 ClassInfo ci = ClassLoaderInfo.getCurrentResolvedClassInfo(clsBase); // <2do> should use CL argument
145 FieldInfo fi = ci.getDeclaredStaticField(fname);
148 throw new JPFException("class " + ci.getName() +
149 " has no static field " + fname);
155 public FieldInfo getFieldInfo (String fname) {
156 ClassInfo ci = getClassInfo();
157 return ci.getStaticField(fname);
160 protected void checkFieldInfo (FieldInfo fi) {
161 if (getClassInfo() != fi.getClassInfo()) {
162 throw new JPFException("wrong static FieldInfo : " + fi.getName()
163 + " , no such field in class " + getClassInfo().getName());
168 public int getNumberOfFields () {
169 return getClassInfo().getNumberOfStaticFields();
173 public FieldInfo getFieldInfo (int fieldIndex) {
174 return getClassInfo().getStaticField(fieldIndex);
178 * gc mark all objects stored in static reference fields
180 void markStaticRoot (Heap heap) {
181 ClassInfo ci = getClassInfo();
182 int n = ci.getNumberOfStaticFields();
184 for (int i=0; i<n; i++) {
185 FieldInfo fi = ci.getStaticField(i);
186 if (fi.isReference()) {
187 int objref = fields.getIntValue(fi.getStorageOffset());
188 heap.markStaticRoot(objref);
192 // don't forget the class object itself (which is not a field)
193 heap.markStaticRoot(classObjectRef);
196 public int getClassObjectRef () {
197 return classObjectRef;
200 public void setClassObjectRef(int r) {
204 attributes |= ATTR_COR_CHANGED;
208 public String toString() {
209 return getClassInfo().getName(); // don't append objRef (useless and misleading for statics)
212 protected ElementInfo getReferencedElementInfo (FieldInfo fi){
213 assert fi.isReference();
214 Heap heap = VM.getVM().getHeap();
215 return heap.get(getIntField(fi));