Fix Throwable.toString

This commit is contained in:
Alexey Andreev 2019-02-13 19:19:15 +03:00
parent acc48f4532
commit 463f24506d
2 changed files with 19 additions and 5 deletions

View File

@ -134,8 +134,11 @@ public class TThrowable extends RuntimeException {
@Remove @Remove
public native Class<?> getClass0(); public native Class<?> getClass0();
@Remove @Rename("toString")
public native String toString0(); public String toString0() {
String message = getLocalizedMessage();
return getClass().getName() + (message != null ? ": " + message : "");
}
public TThrowable initCause(TThrowable cause) { public TThrowable initCause(TThrowable cause) {
if (this.cause != this && this.cause != null) { if (this.cause != this && this.cause != null) {
@ -155,9 +158,9 @@ public class TThrowable extends RuntimeException {
public void printStackTrace(PrintStream stream) { public void printStackTrace(PrintStream stream) {
stream.print(getClass().getName()); stream.print(getClass().getName());
String message = getMessage(); String message = getLocalizedMessage();
if (message != null) { if (message != null) {
stream.print(": " + getMessage()); stream.print(": " + message);
} }
stream.println(); stream.println();
if (stackTrace != null) { if (stackTrace != null) {
@ -173,7 +176,12 @@ public class TThrowable extends RuntimeException {
} }
public void printStackTrace(PrintWriter stream) { 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) { if (stackTrace != null) {
for (TStackTraceElement element : stackTrace) { for (TStackTraceElement element : stackTrace) {
stream.print(" at "); stream.print(" at ");

View File

@ -29,4 +29,10 @@ public class ThrowableTest {
assertTrue(e.getCause() instanceof RuntimeException); assertTrue(e.getCause() instanceof RuntimeException);
assertEquals("OK", e.getCause().getMessage()); 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());
}
} }