From 463f24506db8fae2b7ca9605ddbd997d3793e8f4 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 13 Feb 2019 19:19:15 +0300 Subject: [PATCH] Fix Throwable.toString --- .../teavm/classlib/java/lang/TThrowable.java | 18 +++++++++++++----- .../classlib/java/lang/ThrowableTest.java | 6 ++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java index cb19834e0..7a28fad4b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TThrowable.java @@ -134,8 +134,11 @@ public class TThrowable extends RuntimeException { @Remove public native Class getClass0(); - @Remove - public native String toString0(); + @Rename("toString") + public String toString0() { + String message = getLocalizedMessage(); + return getClass().getName() + (message != null ? ": " + message : ""); + } public TThrowable initCause(TThrowable cause) { if (this.cause != this && this.cause != null) { @@ -155,9 +158,9 @@ public class TThrowable extends RuntimeException { public void printStackTrace(PrintStream stream) { stream.print(getClass().getName()); - String message = getMessage(); + String message = getLocalizedMessage(); if (message != null) { - stream.print(": " + getMessage()); + stream.print(": " + message); } stream.println(); if (stackTrace != null) { @@ -173,7 +176,12 @@ public class TThrowable extends RuntimeException { } public void printStackTrace(PrintWriter stream) { - stream.println(getClass().getName() + ": " + getMessage()); + stream.print(getClass().getName()); + String message = getLocalizedMessage(); + if (message != null) { + stream.print(": " + message); + } + stream.println(); if (stackTrace != null) { for (TStackTraceElement element : stackTrace) { stream.print(" at "); diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ThrowableTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ThrowableTest.java index 5eed36eef..1404ed866 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/ThrowableTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ThrowableTest.java @@ -29,4 +29,10 @@ public class ThrowableTest { assertTrue(e.getCause() instanceof RuntimeException); assertEquals("OK", e.getCause().getMessage()); } + + @Test + public void toStringWorks() { + assertEquals("java.lang.RuntimeException: fail", new RuntimeException("fail").toString()); + assertEquals("java.lang.RuntimeException", new RuntimeException().toString()); + } }