Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / vm / ExceptionInfo.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.io.PrintWriter;
21
22 /**
23  * helper class to store context of an exception
24  */
25 public class ExceptionInfo {
26   ElementInfo  ei;
27   ThreadInfo ti;
28   
29   ExceptionInfo (ThreadInfo xThread, ElementInfo xEi) {
30     ti = xThread;
31     ei = xEi;
32   }
33   
34   public ElementInfo getException() {
35     return ei;
36   }
37   
38   public int getExceptionReference () {
39     return ei.getObjectRef();
40   }
41   
42   public String getExceptionClassname() {
43     return ei.getClassInfo().getName();
44   }
45   
46   public String getDetails() {
47     StringBuilder sb = new StringBuilder();
48     sb.append(getExceptionClassname());
49     
50     int msgRef = ei.getReferenceField("detailMessage");
51     if (msgRef != MJIEnv.NULL){
52       ElementInfo eiMsg = ti.getElementInfo(msgRef);
53       sb.append(" : ");
54       sb.append(eiMsg.asString());
55     }
56       
57     return sb.toString();
58   }
59   
60   public String getCauseClassname() {
61     int causeRef = ei.getReferenceField("cause");
62     if (causeRef != MJIEnv.NULL){
63       ElementInfo eiCause = ti.getElementInfo(causeRef);
64       return eiCause.getClassInfo().getName();
65     }
66     
67     return null;
68   }
69   public String getCauseDetails() {
70     int causeRef = ei.getReferenceField("cause");
71     if (causeRef != MJIEnv.NULL){
72       ElementInfo eiCause = ti.getElementInfo(causeRef);
73       int msgRef = eiCause.getReferenceField("detailMessage");
74       if (msgRef != MJIEnv.NULL){
75         ElementInfo eiMsg = ti.getElementInfo(msgRef);
76         return eiMsg.asString();
77       }
78     }
79
80     return null;
81   }
82
83   
84   public ThreadInfo getThread() {
85     return ti;
86   }
87   
88   public void printOn (PrintWriter pw){
89     ti.printStackTrace(pw, ei.getObjectRef());
90   }
91 }