4242
4343/**
4444 * 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.
4549 */
4650@ SuppressWarnings ("serial" )
47- final class TranslatedException extends Exception {
51+ public final class TranslatedException extends Exception {
4852
4953 /**
5054 * The value returned by {@link #encodeThrowable(Throwable)} when encoding
@@ -61,15 +65,18 @@ final class TranslatedException extends Exception {
6165 maybeFailClinit ();
6266 try {
6367 FALLBACK_ENCODED_THROWABLE_BYTES =
64- encodeThrowable (new TranslatedException ("error during encoding" ,
65- "<unknown>" ), false );
68+ encodeThrowable (translationFailure ("error during encoding" ), false );
6669 FALLBACK_ENCODED_OUTOFMEMORYERROR_BYTES =
67- encodeThrowable (new OutOfMemoryError ( ), false );
70+ encodeThrowable (translationFailure ( "OutOfMemoryError during encoding" ), false );
6871 } catch (IOException e ) {
6972 throw new InternalError (e );
7073 }
7174 }
7275
76+ private static InternalError translationFailure (String messageFormat , Object ... messageArgs ) {
77+ return new InternalError (messageFormat .formatted (messageArgs ));
78+ }
79+
7380 /**
7481 * Helper to test exception translation.
7582 */
@@ -86,14 +93,8 @@ private static void maybeFailClinit() {
8693 }
8794 }
8895
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 );
9798 }
9899
99100 /**
@@ -106,18 +107,6 @@ public Throwable fillInStackTrace() {
106107 return this ;
107108 }
108109
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-
121110 /**
122111 * Prints a stack trace for {@code throwable} if the system property
123112 * {@code "jdk.internal.vm.TranslatedException.debug"} is true.
@@ -163,7 +152,7 @@ private static Throwable create(String className, String message, Throwable caus
163152 return initCause ((Throwable ) cons .newInstance (message ), cause , debug );
164153 } catch (Throwable translationFailure ) {
165154 debugPrintStackTrace (translationFailure , debug );
166- return initCause (new TranslatedException ( message , className ), cause , debug );
155+ return initCause (translationFailure ( "%s [%s]" , message , className ), cause , debug );
167156 }
168157 }
169158
@@ -308,11 +297,10 @@ static Throwable decodeThrowable(byte[] encodedThrowable, boolean debug) {
308297 throwable .setStackTrace (stackTrace );
309298 cause = throwable ;
310299 }
311- return throwable ;
300+ return new TranslatedException ( throwable ) ;
312301 } catch (Throwable translationFailure ) {
313302 debugPrintStackTrace (translationFailure , debug );
314- return new TranslatedException ("Error decoding exception: " + encodedThrowable ,
315- translationFailure .getClass ().getName ());
303+ return translationFailure ("error decoding exception: %s" , encodedThrowable );
316304 }
317305 }
318306}
0 commit comments