Fixes default method resolution (#159)
[jpf-core.git] / src / main / gov / nasa / jpf / vm / ReferenceFieldInfo.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 gov.nasa.jpf.JPFException;
21
22
23 /**
24  * field info for object fields
25  */
26 public class ReferenceFieldInfo extends SingleSlotFieldInfo {
27   int init;  //  = MJIEnv.NULL; // not required for MJIEnv.NULL = 0
28   
29   String sInit; // <2do> pcm - just a temporary quirk to init from string literals
30                 // check if there are other non-object reference inits
31
32   public ReferenceFieldInfo (String name, String type, int modifiers) {
33     super(name, type, modifiers);
34   }
35
36   @Override
37   public String valueToString (Fields f) {
38     int i = f.getIntValue(storageOffset);
39     if (i == MJIEnv.NULL) {
40       return "null";
41     } else {
42       return (VM.getVM().getHeap().get(i)).toString();
43     }
44   }
45
46   @Override
47   public boolean isReference () {
48     return true;
49   }
50
51   @Override
52   public Class<? extends ChoiceGenerator<?>> getChoiceGeneratorType() {
53     return ReferenceChoiceGenerator.class;
54   }
55
56   @Override
57   public boolean isArrayField () {
58     return ci.isArray;
59   }
60
61   @Override
62   public void setConstantValue (Object constValue){
63     // <2do> pcm - check what other constants we might encounter, this is most
64     // probably not just used for Strings.
65     // Besides the type issue, there is an even bigger problem with identities.
66     // For instance, all String refs initialized via the same string literal
67     // inside a single classfile are in fact refering to the same object. This
68     // means we have to keep a registry (hashtab) with string-literal created
69     // String objects per ClassInfo, and use this when we assign or init
70     // String references.
71     // For the sake of progress, we ignore this for now, but have to come back
72     // to it because it violates the VM spec
73
74     if (constValue instanceof String){
75       cv = constValue;
76       sInit = (String)constValue;
77     } else {
78       throw new JPFException ("unsupported reference initialization: " + constValue);
79     }
80   }
81
82   @Override
83   public void initialize (ElementInfo ei, ThreadInfo ti) {
84     int ref = init;
85     if (sInit != null) {
86       VM vm = ti.getVM();
87       Heap heap = vm.getHeap();
88       ref = heap.newString(sInit, ti).getObjectRef();
89     }
90     ei.getFields().setReferenceValue( storageOffset, ref);
91   }
92
93   @Override
94   public Object getValueObject (Fields f){
95     int i = f.getIntValue(storageOffset);
96     if (i == MJIEnv.NULL) {
97       return null;
98     } else {
99       Heap heap = VM.getVM().getHeap();
100       return heap.get(i);
101     }
102   }
103 }