author | jbachorik |
Tue, 21 Jan 2014 13:04:55 +0100 | |
changeset 22353 | d09e3ff5fd63 |
parent 9513 | 1079ae7ada52 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
import java.io.*; |
|
25 |
import java.util.*; |
|
26 |
||
27 |
/* |
|
28 |
* @test |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
29 |
* @bug 4202914 4363318 6991528 6998871 |
2 | 30 |
* @summary Basic test of serialization of stack trace information |
31 |
* @author Josh Bloch |
|
32 |
*/ |
|
33 |
||
34 |
public class StackTraceSerialization { |
|
35 |
public static void main(String args[]) throws Exception { |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
36 |
testWithSetStackTrace(); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
37 |
testWithFillInStackTrace(); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
38 |
} |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
39 |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
40 |
private static void testWithSetStackTrace() { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
41 |
StackTraceElement[] stackTrace = {new StackTraceElement("foo", "bar", "baz", -1)}; |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
42 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
43 |
Throwable t = new TestThrowable(true, false); // Immutable and empty stack |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
44 |
assertEmptyStackTrace(t); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
45 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
46 |
// Verify fillInStackTrace is now a no-op. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
47 |
t.fillInStackTrace(); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
48 |
assertEmptyStackTrace(t); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
49 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
50 |
// Verify setStackTrace is now a no-op. |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
51 |
t.setStackTrace(stackTrace); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
52 |
assertEmptyStackTrace(t); |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
53 |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
54 |
// Verify null-handling |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
55 |
try { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
56 |
t.setStackTrace(null); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
57 |
throw new RuntimeException("No NPE on a null stack trace."); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
58 |
} catch(NullPointerException npe) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
59 |
assertEmptyStackTrace(t); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
60 |
} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
61 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
62 |
try { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
63 |
t.setStackTrace(new StackTraceElement[]{null}); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
64 |
throw new RuntimeException("No NPE on a null stack trace element."); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
65 |
} catch(NullPointerException npe) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
66 |
assertEmptyStackTrace(t); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
67 |
} |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
68 |
|
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
69 |
if (!equal(t, reconstitute(t))) |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
70 |
throw new RuntimeException("Unequal Throwables with set stacktrace"); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
71 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
72 |
Throwable t2 = new Throwable(); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
73 |
t2.setStackTrace(stackTrace); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
74 |
if (!equal(t2, reconstitute(t2))) |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
75 |
throw new RuntimeException("Unequal Throwables with set stacktrace"); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
76 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
77 |
} |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
78 |
|
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
79 |
private static class TestThrowable extends Throwable { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
80 |
public TestThrowable(boolean enableSuppression, |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
81 |
boolean writableStackTrace) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
82 |
super("the medium", null, |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
83 |
enableSuppression, |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
84 |
writableStackTrace); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
85 |
} |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
86 |
} |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
87 |
|
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
88 |
private static void assertEmptyStackTrace(Throwable t) { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
89 |
if (t.getStackTrace().length != 0) |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
90 |
throw new AssertionError("Nonempty stacktrace."); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
91 |
} |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
92 |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
93 |
private static void testWithFillInStackTrace() { |
2 | 94 |
Throwable original = null; |
95 |
try { |
|
96 |
a(); |
|
97 |
} catch(HighLevelException e) { |
|
98 |
original = e; |
|
99 |
} |
|
100 |
||
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
101 |
if (!equal(original, reconstitute(original))) |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
102 |
throw new RuntimeException("Unequal Throwables with filled-in stacktrace"); |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
103 |
} |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
104 |
|
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
105 |
/** |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
106 |
* Serialize the argument and return the deserialized result. |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
107 |
*/ |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
108 |
private static Throwable reconstitute(Throwable t) { |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
109 |
Throwable result = null; |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
110 |
try(ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
111 |
ObjectOutputStream out = new ObjectOutputStream(bout)) { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
112 |
out.writeObject(t); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
113 |
out.flush(); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
114 |
try(ByteArrayInputStream bin = |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
115 |
new ByteArrayInputStream(bout.toByteArray()); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
116 |
ObjectInputStream in = new ObjectInputStream(bin)) { |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
117 |
result = (Throwable) in.readObject(); |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
118 |
} |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
119 |
} catch(IOException | ClassNotFoundException e) { |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
7186
diff
changeset
|
120 |
throw new RuntimeException(e); |
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
121 |
} |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
122 |
return result; |
2 | 123 |
} |
124 |
||
125 |
/** |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
126 |
* Returns true if e1 and e2 have equal stack traces and their |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
127 |
* causes are recursively equal (by the same definition) and their |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
128 |
* suppressed exception information is equals. Returns false or |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
129 |
* throws NullPointerExeption otherwise. |
2 | 130 |
*/ |
131 |
private static boolean equal(Throwable t1, Throwable t2) { |
|
7186
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
132 |
return t1==t2 || |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
133 |
(Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
134 |
equal(t1.getCause(), t2.getCause()) && |
7f5fe8985263
6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents:
5506
diff
changeset
|
135 |
Objects.equals(t1.getSuppressed(), t2.getSuppressed())); |
2 | 136 |
} |
137 |
||
138 |
static void a() throws HighLevelException { |
|
139 |
try { |
|
140 |
b(); |
|
141 |
} catch(MidLevelException e) { |
|
142 |
throw new HighLevelException(e); |
|
143 |
} |
|
144 |
} |
|
145 |
static void b() throws MidLevelException { |
|
146 |
c(); |
|
147 |
} |
|
148 |
static void c() throws MidLevelException { |
|
149 |
try { |
|
150 |
d(); |
|
151 |
} catch(LowLevelException e) { |
|
152 |
throw new MidLevelException(e); |
|
153 |
} |
|
154 |
} |
|
155 |
static void d() throws LowLevelException { |
|
156 |
e(); |
|
157 |
} |
|
158 |
static void e() throws LowLevelException { |
|
159 |
throw new LowLevelException(); |
|
160 |
} |
|
161 |
||
162 |
private static final String OUR_CLASS = StackTraceSerialization.class.getName(); |
|
163 |
private static final String OUR_FILE_NAME = "StackTraceSerialization.java"; |
|
164 |
||
165 |
private static void check(StackTraceElement e, String methodName, int n) { |
|
166 |
if (!e.getClassName().equals(OUR_CLASS)) |
|
167 |
throw new RuntimeException("Class: " + e); |
|
168 |
if (!e.getMethodName().equals(methodName)) |
|
169 |
throw new RuntimeException("Method name: " + e); |
|
170 |
if (!e.getFileName().equals(OUR_FILE_NAME)) |
|
171 |
throw new RuntimeException("File name: " + e); |
|
172 |
if (e.getLineNumber() != n) |
|
173 |
throw new RuntimeException("Line number: " + e); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
class HighLevelException extends Exception { |
|
178 |
HighLevelException(Throwable cause) { super(cause); } |
|
179 |
} |
|
180 |
||
181 |
class MidLevelException extends Exception { |
|
182 |
MidLevelException(Throwable cause) { super(cause); } |
|
183 |
} |
|
184 |
||
185 |
class LowLevelException extends Exception { |
|
186 |
} |