Preparing for tracking the object creation etc.
[jpf-core.git] / src / peers / gov / nasa / jpf / vm / JPF_java_lang_Throwable.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.annotation.MJI;
21 import gov.nasa.jpf.vm.ClassInfo;
22 import gov.nasa.jpf.vm.MJIEnv;
23 import gov.nasa.jpf.vm.NativePeer;
24 import gov.nasa.jpf.vm.ThreadInfo;
25
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28
29
30 /**
31  * MJI NativePeer class for java.lang.Throwable library abstraction
32  */
33 public class JPF_java_lang_Throwable extends NativePeer {    
34   /**
35    * return array of StackTraceElement elements from the snapshot stored in the Throwable
36    */
37   @MJI
38   public int createStackTrace_____3Ljava_lang_StackTraceElement_2 (MJIEnv env, int objref) {
39     int aref = env.getReferenceField(objref, "snapshot");
40     int[] snap = env.getIntArrayObject(aref);
41     
42     return env.getThreadInfo().createStackTraceElements(snap);
43   }
44   
45   @MJI
46   public int fillInStackTrace____Ljava_lang_Throwable_2 (MJIEnv env, int objref) {
47     ThreadInfo ti = env.getThreadInfo();
48     int[] snap = ti.getSnapshot(objref);
49     
50     int aref = env.newIntArray(snap);
51     env.setReferenceField(objref, "snapshot", aref);
52     
53     return objref;
54   }
55     
56   // native because we don't want to waste states
57   @MJI
58   public void printStackTrace____V (MJIEnv env, int objRef) {
59     env.getThreadInfo().printStackTrace(objRef);
60   }
61   
62   // a helper method to get a string representation of the stacktrace
63   @MJI
64   public int getStackTraceAsString____Ljava_lang_String_2 (MJIEnv env, int objRef) {
65     ThreadInfo ti = env.getThreadInfo();
66     StringWriter sw = new StringWriter();
67     PrintWriter pw = new PrintWriter(sw);
68     
69     ti.printStackTrace(pw, objRef);
70     String stackTrace = sw.toString();
71     pw.close();
72     
73     return env.newString(stackTrace);
74   }
75   
76   @MJI
77   public int toString____Ljava_lang_String_2 (MJIEnv env, int objRef){
78     ClassInfo ci = env.getClassInfo(objRef);
79     int msgRef = env.getReferenceField(objRef, "detailMessage");
80     
81     String s = ci.getName();
82     if (msgRef != MJIEnv.NULL){
83       s += ": " + env.getStringObject(msgRef);
84     }
85     
86     return env.newString(s);
87   }
88 }