42
42
43
43
/**
44
44
* Support for translating exceptions between the HotSpot heap and libjvmci heap.
45
+ *
46
+ * Successfully translated exceptions are wrapped in a TranslatedException instance.
47
+ * This allows callers to distiguish between a translated exception and an error
48
+ * that arose during translation.
45
49
*/
46
50
@ SuppressWarnings ("serial" )
47
- final class TranslatedException extends Exception {
51
+ public final class TranslatedException extends Exception {
48
52
49
53
/**
50
54
* The value returned by {@link #encodeThrowable(Throwable)} when encoding
@@ -61,15 +65,18 @@ final class TranslatedException extends Exception {
61
65
maybeFailClinit ();
62
66
try {
63
67
FALLBACK_ENCODED_THROWABLE_BYTES =
64
- encodeThrowable (new TranslatedException ("error during encoding" ,
65
- "<unknown>" ), false );
68
+ encodeThrowable (translationFailure ("error during encoding" ), false );
66
69
FALLBACK_ENCODED_OUTOFMEMORYERROR_BYTES =
67
- encodeThrowable (new OutOfMemoryError ( ), false );
70
+ encodeThrowable (translationFailure ( "OutOfMemoryError during encoding" ), false );
68
71
} catch (IOException e ) {
69
72
throw new InternalError (e );
70
73
}
71
74
}
72
75
76
+ private static InternalError translationFailure (String messageFormat , Object ... messageArgs ) {
77
+ return new InternalError (messageFormat .formatted (messageArgs ));
78
+ }
79
+
73
80
/**
74
81
* Helper to test exception translation.
75
82
*/
@@ -86,14 +93,8 @@ private static void maybeFailClinit() {
86
93
}
87
94
}
88
95
89
- /**
90
- * Class name of exception that could not be instantiated.
91
- */
92
- private String originalExceptionClassName ;
93
-
94
- private TranslatedException (String message , String originalExceptionClassName ) {
95
- super (message );
96
- this .originalExceptionClassName = originalExceptionClassName ;
96
+ TranslatedException (Throwable translated ) {
97
+ super (translated );
97
98
}
98
99
99
100
/**
@@ -106,18 +107,6 @@ public Throwable fillInStackTrace() {
106
107
return this ;
107
108
}
108
109
109
- @ Override
110
- public String toString () {
111
- String s ;
112
- if (originalExceptionClassName .equals (TranslatedException .class .getName ())) {
113
- s = getClass ().getName ();
114
- } else {
115
- s = getClass ().getName () + "[" + originalExceptionClassName + "]" ;
116
- }
117
- String message = getMessage ();
118
- return (message != null ) ? (s + ": " + message ) : s ;
119
- }
120
-
121
110
/**
122
111
* Prints a stack trace for {@code throwable} if the system property
123
112
* {@code "jdk.internal.vm.TranslatedException.debug"} is true.
@@ -163,7 +152,7 @@ private static Throwable create(String className, String message, Throwable caus
163
152
return initCause ((Throwable ) cons .newInstance (message ), cause , debug );
164
153
} catch (Throwable translationFailure ) {
165
154
debugPrintStackTrace (translationFailure , debug );
166
- return initCause (new TranslatedException ( message , className ), cause , debug );
155
+ return initCause (translationFailure ( "%s [%s]" , message , className ), cause , debug );
167
156
}
168
157
}
169
158
@@ -308,11 +297,10 @@ static Throwable decodeThrowable(byte[] encodedThrowable, boolean debug) {
308
297
throwable .setStackTrace (stackTrace );
309
298
cause = throwable ;
310
299
}
311
- return throwable ;
300
+ return new TranslatedException ( throwable ) ;
312
301
} catch (Throwable translationFailure ) {
313
302
debugPrintStackTrace (translationFailure , debug );
314
- return new TranslatedException ("Error decoding exception: " + encodedThrowable ,
315
- translationFailure .getClass ().getName ());
303
+ return translationFailure ("error decoding exception: %s" , encodedThrowable );
316
304
}
317
305
}
318
306
}
0 commit comments