# HG changeset patch # User Jaroslav Tulach # Date 1380326594 -7200 # Node ID f08854c7f8b1df13ba3eaeb142cd99fad344b707 # Parent bf0b56f2dca22e8bcfca9d3457f08b71e9e2b9ab Javac uses Throwable printStackTrace method a lot diff -r bf0b56f2dca2 -r f08854c7f8b1 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java Sat Sep 28 02:03:14 2013 +0200 @@ -0,0 +1,68 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class ExceptionsTest { + @Compare public String firstLineIsTheSame() throws UnsupportedEncodingException { + MyException ex = new MyException("Hello"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(out); + ex.printStackTrace(ps); + ps.flush(); + + String s = new String(out.toByteArray(), "UTF-8"); + int newLine = s.indexOf('\n'); + return s.substring(0, newLine); + } + + @Compare public String firstLineIsTheSameWithWriter() throws UnsupportedEncodingException { + MyException ex = new MyException("Hello"); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + ex.printStackTrace(pw); + pw.flush(); + + String s = sw.toString(); + int newLine = s.indexOf('\n'); + return s.substring(0, newLine); + } + + static class MyException extends Exception { + public MyException(String message) { + super(message); + } + } + + + @Factory public static Object[] create() { + return VMTest.create(ExceptionsTest.class); + } +} diff -r bf0b56f2dca2 -r f08854c7f8b1 rt/emul/mini/pom.xml --- a/rt/emul/mini/pom.xml Sat Sep 28 01:32:59 2013 +0200 +++ b/rt/emul/mini/pom.xml Sat Sep 28 02:03:14 2013 +0200 @@ -35,9 +35,15 @@ maven-compiler-plugin 2.5.1 + 1.7 1.7 diff -r bf0b56f2dca2 -r f08854c7f8b1 rt/emul/mini/src/main/java/java/lang/Throwable.java --- a/rt/emul/mini/src/main/java/java/lang/Throwable.java Sat Sep 28 01:32:59 2013 +0200 +++ b/rt/emul/mini/src/main/java/java/lang/Throwable.java Sat Sep 28 02:03:14 2013 +0200 @@ -641,90 +641,28 @@ @JavaScriptBody(args = { }, body = "console.warn(this.toString());") public native void printStackTrace(); -// /** -// * Prints this throwable and its backtrace to the specified print stream. -// * -// * @param s {@code PrintStream} to use for output -// */ -// public void printStackTrace(PrintStream s) { -// printStackTrace(new WrappedPrintStream(s)); -// } -// -// private void printStackTrace(PrintStreamOrWriter s) { -// // Guard against malicious overrides of Throwable.equals by -// // using a Set with identity equality semantics. -//// Set dejaVu = -//// Collections.newSetFromMap(new IdentityHashMap()); -//// dejaVu.add(this); -// -// synchronized (s.lock()) { -// // Print our stack trace -// s.println(this); -// StackTraceElement[] trace = getOurStackTrace(); -// for (StackTraceElement traceElement : trace) -// s.println("\tat " + traceElement); -// -// // Print suppressed exceptions, if any -//// for (Throwable se : getSuppressed()) -//// se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu); -// -// // Print cause, if any -// Throwable ourCause = getCause(); -//// if (ourCause != null) -//// ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu); -// } -// } -// -// /** -// * Print our stack trace as an enclosed exception for the specified -// * stack trace. -// */ -// private void printEnclosedStackTrace(PrintStreamOrWriter s, -// StackTraceElement[] enclosingTrace, -// String caption, -// String prefix, -// Object dejaVu) { -// assert Thread.holdsLock(s.lock()); -// { -// // Compute number of frames in common between this and enclosing trace -// StackTraceElement[] trace = getOurStackTrace(); -// int m = trace.length - 1; -// int n = enclosingTrace.length - 1; -// while (m >= 0 && n >=0 && trace[m].equals(enclosingTrace[n])) { -// m--; n--; -// } -// int framesInCommon = trace.length - 1 - m; -// -// // Print our stack trace -// s.println(prefix + caption + this); -// for (int i = 0; i <= m; i++) -// s.println(prefix + "\tat " + trace[i]); -// if (framesInCommon != 0) -// s.println(prefix + "\t... " + framesInCommon + " more"); -// -// // Print suppressed exceptions, if any -// for (Throwable se : getSuppressed()) -// se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, -// prefix +"\t", dejaVu); -// -// // Print cause, if any -// Throwable ourCause = getCause(); -// if (ourCause != null) -// ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu); -// } -// } -// -// /** -// * Prints this throwable and its backtrace to the specified -// * print writer. -// * -// * @param s {@code PrintWriter} to use for output -// * @since JDK1.1 -// */ -// public void printStackTrace(PrintWriter s) { -// printStackTrace(new WrappedPrintWriter(s)); -// } -// + /** + * Prints this throwable and its backtrace to the specified print stream. + * + * @param s {@code PrintStream} to use for output + */ + public void printStackTrace(PrintStream s) { + s.print(getClass().getName()); + s.print(": "); + s.println(getMessage()); + } + + /** + * Prints this throwable and its backtrace to the specified + * print writer. + * + * @param s {@code PrintWriter} to use for output + * @since JDK1.1 + */ + public void printStackTrace(PrintWriter s) { + s.append(getClass().getName()).append(": ").println(getMessage()); + } + // /** // * Wrapper class for PrintStream and PrintWriter to enable a single // * implementation of printStackTrace.