Initial import
[jpf-core.git] / src / peers / gov / nasa / jpf / vm / JPF_gov_nasa_jpf_SerializationConstructor.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
19 package gov.nasa.jpf.vm;
20
21 import gov.nasa.jpf.annotation.MJI;
22
23
24 public class JPF_gov_nasa_jpf_SerializationConstructor extends NativePeer {
25
26   /**
27    * create a new instance, but only call the ctor of the first
28    * non-serializable superclass
29    */
30   @MJI
31   public int newInstance___3Ljava_lang_Object_2__Ljava_lang_Object_2 (MJIEnv env, int mthRef, int argsRef) {
32     ThreadInfo ti = env.getThreadInfo();
33     DirectCallStackFrame frame = ti.getReturnedDirectCall();
34     
35     int superCtorRef = env.getReferenceField(mthRef, "firstNonSerializableCtor"); 
36     MethodInfo miCtor = JPF_java_lang_reflect_Constructor.getMethodInfo(env,superCtorRef);
37
38     if (frame == null){ // first time
39       int clsRef = env.getReferenceField(mthRef, "mdc");
40       ClassInfo ci = env.getReferredClassInfo( clsRef);
41
42       if (ci.isAbstract()){
43         env.throwException("java.lang.InstantiationException");
44         return MJIEnv.NULL;
45       }
46
47       int objRef = env.newObjectOfUncheckedClass(ci);
48       frame = miCtor.createDirectCallStackFrame(ti, 1); 
49       frame.setReferenceArgument( 0, objRef, null);
50       frame.setLocalReferenceVariable(0, objRef); // (1) we store the reference as a local var for retrieval during reexec      
51       ti.pushFrame(frame);
52       
53       // check for & push required clinits
54       ci.initializeClass(ti);
55       env.repeatInvocation();
56       return MJIEnv.NULL;
57       
58     } else { // re-execution, 
59       int objRef = frame.getLocalVariable(0); // that's the object ref we stored in (1)
60       return objRef;
61     }
62   }
63 }