Fixes default method resolution (#159)
[jpf-core.git] / src / main / gov / nasa / jpf / vm / OVStatics.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package gov.nasa.jpf.vm;
19
20 import java.util.Iterator;
21
22 import gov.nasa.jpf.Config;
23 import gov.nasa.jpf.util.IntTable;
24 import gov.nasa.jpf.util.ObjVector;
25
26 /**
27  * Statics implementation that uses a simple ObjVector as the underlying container.
28  * 
29  * The ids used to retrieve ElementInfos are dense and search global, computation is based 
30  * on the assumption that each ClassLoader can only define one class per binary class name
31  */
32 public class OVStatics implements Statics {
33
34   static class OVMemento implements Memento<Statics> {
35     ObjVector.Snapshot<ElementInfo> eiSnap;
36     
37     OVMemento (OVStatics statics){
38       statics.elementInfos.process( ElementInfo.storer);
39       eiSnap = statics.elementInfos.getSnapshot();
40     }
41     
42     @Override
43     public Statics restore(Statics inSitu) {
44       OVStatics statics = (OVStatics) inSitu;
45       statics.elementInfos.restore(eiSnap);
46       statics.elementInfos.process( ElementInfo.restorer);
47       
48       return statics;
49     }
50   }
51   
52   protected ObjVector<ElementInfo> elementInfos;
53   
54   // search global class ids (for this ClassLoader only)
55   // NOTE this is per instance so that each one is as dense as possible, but since
56   // it is search global it does NOT have to be restored and we can copy the reference when cloning
57   protected int nextId;
58   protected IntTable<String> ids;
59   
60   
61   //--- construction
62   
63   public OVStatics (Config conf) {
64     elementInfos = new ObjVector<ElementInfo>();
65     
66     nextId = 0;
67     ids = new IntTable<String>();
68   }
69   
70   protected int computeId (ClassInfo ci) {
71     String clsName = ci.getName();
72     IntTable.Entry<String> e = ids.get(clsName);
73     if (e == null) {
74       int id = nextId++;
75       ids.put( clsName, id);
76       return id;
77       
78     } else {
79       return e.val;
80     }
81   }
82   
83   protected StaticElementInfo createStaticElementInfo (int id, ClassInfo ci, ThreadInfo ti, ElementInfo eiClsObj) {
84     Fields   f = ci.createStaticFields();
85     Monitor  m = new Monitor();
86
87     StaticElementInfo ei = new StaticElementInfo( id, ci, f, m, ti, eiClsObj);
88
89     ci.initializeStaticData(ei, ti);
90
91     return ei;
92   }
93   
94   @Override
95   public StaticElementInfo newClass (ClassInfo ci, ThreadInfo ti, ElementInfo eiClsObj) {
96     assert (eiClsObj != null);
97     
98     int id = computeId( ci);
99     
100     StaticElementInfo ei = createStaticElementInfo( id, ci, ti, eiClsObj);
101     elementInfos.set(id, ei);
102     
103     return ei;
104   }
105
106   @Override
107   public StaticElementInfo newStartupClass (ClassInfo ci, ThreadInfo ti) {
108     int id = computeId( ci);
109     
110     StaticElementInfo ei = createStaticElementInfo( id, ci, ti, null);
111     elementInfos.set(id, ei);
112     
113     return ei;
114   }
115
116   
117   //--- accessors
118   
119   @Override
120   public StaticElementInfo get(int id) {
121     // the cast sucks, but otherwise we run into the Processor covariance problem 
122     return (StaticElementInfo)elementInfos.get(id);
123   }
124
125   @Override
126   public StaticElementInfo getModifiable(int id) {
127     StaticElementInfo ei = (StaticElementInfo)elementInfos.get(id);
128     
129     if (ei.isFrozen()) {
130       ei = (StaticElementInfo)ei.deepClone();
131       // freshly created ElementInfos are not frozen, so we don't have to defreeze
132       elementInfos.set(id, ei);
133     }
134     
135     return ei;
136   }
137
138   //--- housekeeping
139   
140   @Override
141   public void cleanUpDanglingReferences (Heap heap) {
142     ThreadInfo ti = ThreadInfo.getCurrentThread();
143     int tid = ti.getId();
144     boolean isThreadTermination = ti.isTerminated();
145     
146     for (ElementInfo e : this) {
147       e.cleanUp( heap, isThreadTermination, tid);
148     }
149   }
150   
151   //--- state restoration
152   
153   @Override
154   public Memento<Statics> getMemento(MementoFactory factory) {
155     return factory.getMemento(this);
156   }
157
158   @Override
159   public Memento<Statics> getMemento() {
160     return new OVMemento(this);
161   }
162   
163   @SuppressWarnings("rawtypes")
164   @Override
165   public Iterator<ElementInfo> iterator(){
166     return ((ObjVector)elementInfos).nonNullIterator();
167   }
168   
169   @Override
170   public void markRoots(Heap heap) {
171     for (StaticElementInfo ei : liveStatics()){
172       ei.markStaticRoot(heap);
173     }
174   }
175
176   @SuppressWarnings("rawtypes")
177   @Override
178   public Iterable<StaticElementInfo> liveStatics() {
179     return (Iterable)elementInfos.elements();
180   }
181
182   @Override
183   public int size() {
184     return elementInfos.length();
185   }
186 }