2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
25 * barebones model class for java.lang.Throwable
27 * the main reason for having one is to defer the stacktrace creation, which
28 * we don't want if all an application does is intercepting a gazillion of
29 * exceptions like NoSuchMethodException
31 public class Throwable {
33 int[] snapshot; // this is an internal array of { globalMethodId, pcOffset } pairs
35 protected Throwable cause; // in case this is a wrapper exception (like InvocationTargetException)
37 protected String detailMessage; // where we keep the context specific info
39 protected StackTraceElement[] stackTrace; // only set on demand, if getStackTrace() is called
42 try { // Use Class.forName() instead of new StackTraceElement() since the latter creates garbage.
43 Class.forName("java.lang.StackTraceElement"); // Force this class to load here instead of in createStackTrace().
44 } catch (ClassNotFoundException e) {
45 throw new NoClassDefFoundError("java.lang.StackTraceElement");
51 public Throwable (String msg) {
57 public Throwable (String msg, Throwable xCause) {
63 public Throwable (Throwable xCause) {
68 detailMessage = xCause.toString();
72 public String getMessage() {
76 public String getLocalizedMessage() {
80 public Throwable getCause() {
82 return null; // quirky
88 // this sets 'snapshot' from the native side
89 public native Throwable fillInStackTrace();
91 // and this turns 'snapshot' into an array of StackTraceElement objects
92 private native StackTraceElement[] createStackTrace();
94 public StackTraceElement[] getStackTrace() {
95 if (stackTrace == null){
96 stackTrace = createStackTrace();
102 public void setStackTrace (StackTraceElement[] st) {
106 public synchronized Throwable initCause (Throwable xCause) {
108 throw new IllegalArgumentException("self-causation not permitted");
112 throw new IllegalStateException("cannot overwrite cause");
121 public native String toString();
123 public native void printStackTrace ();
125 // <2do> and lots more of printStackTraces..
127 private native String getStackTraceAsString();
129 public void printStackTrace (PrintStream ps){
130 String s = getStackTraceAsString();
134 public void printStackTrace (PrintWriter pw){
135 String s = getStackTraceAsString();
139 int getStackTraceDepth(){
140 return (snapshot.length / 2); // snapshot stores the methodId and pc for each StackFrame