Fixes default method resolution (#159)
[jpf-core.git] / src / main / gov / nasa / jpf / vm / FunctionObjectFactory.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 /**
21  * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
22  */
23 public class FunctionObjectFactory {
24   
25   public int getFunctionObject(int bsIdx, ThreadInfo ti, ClassInfo fiClassInfo, String samUniqueName, BootstrapMethodInfo bmi, 
26                                          String[] freeVariableTypeNames, Object[] freeVariableValues) {
27     
28     ClassLoaderInfo cli = bmi.enclosingClass.getClassLoaderInfo();
29     
30     ClassInfo funcObjType = cli.getResolvedFuncObjType(bsIdx, fiClassInfo, samUniqueName, bmi, freeVariableTypeNames);
31     
32     funcObjType.registerClass(ti);
33
34     Heap heap = ti.getHeap();
35     ElementInfo ei = heap.newObject(funcObjType, ti);
36     
37     setFuncObjFields(ei, bmi, freeVariableTypeNames, freeVariableValues);
38     
39     return ei.getObjectRef();
40   }
41   
42   public void setFuncObjFields(ElementInfo funcObj, BootstrapMethodInfo bmi, String[] freeVarTypeNames, Object[] freeVarValues) {
43     Fields fields = funcObj.getFields();
44     
45     for(int i = 0; i<freeVarTypeNames.length; i++) {
46       String typeName = freeVarTypeNames[i];
47       if (typeName.equals("byte")) {
48         fields.setByteValue(i, (Byte)freeVarValues[i]);
49       } else if (typeName.equals("char")) {
50         fields.setCharValue(i, (Character)freeVarValues[i]);
51       } else if (typeName.equals("short")) {
52         fields.setShortValue(i, (Short)freeVarValues[i]);
53       } else if (typeName.equals("int")) {
54         fields.setIntValue(i, (Integer)freeVarValues[i]);
55       } else if (typeName.equals("float")) {
56         fields.setFloatValue(i, (Float)freeVarValues[i]);
57       } else if (typeName.equals("long")) {
58         fields.setLongValue(i, (Long)freeVarValues[i]);
59       } else if (typeName.equals("double")) {
60         fields.setDoubleValue(i, (Double)freeVarValues[i]);
61       } else if (typeName.equals("boolean")) {
62         fields.setBooleanValue(i, (Boolean)freeVarValues[i]);
63       } else {
64         if(freeVarValues[i] == null) {
65           fields.setReferenceValue(i, MJIEnv.NULL); 
66         } else {
67           int val = ((ElementInfo)freeVarValues[i]).getObjectRef();
68           fields.setReferenceValue(i, val);
69         }
70       }
71     }
72   }
73 }