Javac uses Throwable printStackTrace method a lot javac
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 28 Sep 2013 02:03:14 +0200
branchjavac
changeset 1313f08854c7f8b1
parent 1312 bf0b56f2dca2
child 1315 6ce019139b13
Javac uses Throwable printStackTrace method a lot
rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java
rt/emul/mini/pom.xml
rt/emul/mini/src/main/java/java/lang/Throwable.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java	Sat Sep 28 02:03:14 2013 +0200
     1.3 @@ -0,0 +1,68 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.tck;
    1.22 +
    1.23 +import java.io.ByteArrayOutputStream;
    1.24 +import java.io.PrintStream;
    1.25 +import java.io.PrintWriter;
    1.26 +import java.io.StringWriter;
    1.27 +import java.io.UnsupportedEncodingException;
    1.28 +import org.apidesign.bck2brwsr.vmtest.Compare;
    1.29 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.30 +import org.testng.annotations.Factory;
    1.31 +
    1.32 +/**
    1.33 + *
    1.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.35 + */
    1.36 +public class ExceptionsTest {
    1.37 +    @Compare public String firstLineIsTheSame() throws UnsupportedEncodingException {
    1.38 +        MyException ex = new MyException("Hello");
    1.39 +        ByteArrayOutputStream out = new ByteArrayOutputStream();
    1.40 +        PrintStream ps = new PrintStream(out);
    1.41 +        ex.printStackTrace(ps);
    1.42 +        ps.flush();
    1.43 +        
    1.44 +        String s = new String(out.toByteArray(), "UTF-8");
    1.45 +        int newLine = s.indexOf('\n');
    1.46 +        return s.substring(0, newLine);
    1.47 +    }
    1.48 +
    1.49 +    @Compare public String firstLineIsTheSameWithWriter() throws UnsupportedEncodingException {
    1.50 +        MyException ex = new MyException("Hello");
    1.51 +        StringWriter sw = new StringWriter();
    1.52 +        PrintWriter pw = new PrintWriter(sw);
    1.53 +        ex.printStackTrace(pw);
    1.54 +        pw.flush();
    1.55 +        
    1.56 +        String s = sw.toString();
    1.57 +        int newLine = s.indexOf('\n');
    1.58 +        return s.substring(0, newLine);
    1.59 +    }
    1.60 +    
    1.61 +    static class MyException extends Exception {
    1.62 +        public MyException(String message) {
    1.63 +            super(message);
    1.64 +        }
    1.65 +    }
    1.66 +    
    1.67 +    
    1.68 +    @Factory public static Object[] create() {
    1.69 +        return VMTest.create(ExceptionsTest.class);
    1.70 +    }
    1.71 +}
     2.1 --- a/rt/emul/mini/pom.xml	Sat Sep 28 01:32:59 2013 +0200
     2.2 +++ b/rt/emul/mini/pom.xml	Sat Sep 28 02:03:14 2013 +0200
     2.3 @@ -35,9 +35,15 @@
     2.4                <artifactId>maven-compiler-plugin</artifactId>
     2.5                <version>2.5.1</version>
     2.6                <configuration>
     2.7 +                  <!-- XXX: Need because Throwable references PrintWriter, but
     2.8 +                     dangerous! We could easy compile against APIs that don't
     2.9 +                     exist. Should be replaced by ahead of compilation copying
    2.10 +                     of files that should really be visible on the classpath...
    2.11 +                     
    2.12                    <compilerArguments>
    2.13                        <bootclasspath>netbeans.ignore.jdk.bootsclasspath</bootclasspath>
    2.14                    </compilerArguments>
    2.15 +                  -->
    2.16                   <source>1.7</source>
    2.17                   <target>1.7</target>
    2.18                </configuration>
     3.1 --- a/rt/emul/mini/src/main/java/java/lang/Throwable.java	Sat Sep 28 01:32:59 2013 +0200
     3.2 +++ b/rt/emul/mini/src/main/java/java/lang/Throwable.java	Sat Sep 28 02:03:14 2013 +0200
     3.3 @@ -641,90 +641,28 @@
     3.4      @JavaScriptBody(args = {  }, body = "console.warn(this.toString());")
     3.5      public native void printStackTrace();
     3.6  
     3.7 -//    /**
     3.8 -//     * Prints this throwable and its backtrace to the specified print stream.
     3.9 -//     *
    3.10 -//     * @param s {@code PrintStream} to use for output
    3.11 -//     */
    3.12 -//    public void printStackTrace(PrintStream s) {
    3.13 -//        printStackTrace(new WrappedPrintStream(s));
    3.14 -//    }
    3.15 -//
    3.16 -//    private void printStackTrace(PrintStreamOrWriter s) {
    3.17 -//        // Guard against malicious overrides of Throwable.equals by
    3.18 -//        // using a Set with identity equality semantics.
    3.19 -////        Set<Throwable> dejaVu =
    3.20 -////            Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
    3.21 -////        dejaVu.add(this);
    3.22 -//
    3.23 -//        synchronized (s.lock()) {
    3.24 -//            // Print our stack trace
    3.25 -//            s.println(this);
    3.26 -//            StackTraceElement[] trace = getOurStackTrace();
    3.27 -//            for (StackTraceElement traceElement : trace)
    3.28 -//                s.println("\tat " + traceElement);
    3.29 -//
    3.30 -//            // Print suppressed exceptions, if any
    3.31 -////            for (Throwable se : getSuppressed())
    3.32 -////                se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
    3.33 -//
    3.34 -//            // Print cause, if any
    3.35 -//            Throwable ourCause = getCause();
    3.36 -////            if (ourCause != null)
    3.37 -////                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
    3.38 -//        }
    3.39 -//    }
    3.40 -//
    3.41 -//    /**
    3.42 -//     * Print our stack trace as an enclosed exception for the specified
    3.43 -//     * stack trace.
    3.44 -//     */
    3.45 -//    private void printEnclosedStackTrace(PrintStreamOrWriter s,
    3.46 -//                                         StackTraceElement[] enclosingTrace,
    3.47 -//                                         String caption,
    3.48 -//                                         String prefix,
    3.49 -//                                         Object dejaVu) {
    3.50 -//        assert Thread.holdsLock(s.lock());
    3.51 -//        {
    3.52 -//            // Compute number of frames in common between this and enclosing trace
    3.53 -//            StackTraceElement[] trace = getOurStackTrace();
    3.54 -//            int m = trace.length - 1;
    3.55 -//            int n = enclosingTrace.length - 1;
    3.56 -//            while (m >= 0 && n >=0 && trace[m].equals(enclosingTrace[n])) {
    3.57 -//                m--; n--;
    3.58 -//            }
    3.59 -//            int framesInCommon = trace.length - 1 - m;
    3.60 -//
    3.61 -//            // Print our stack trace
    3.62 -//            s.println(prefix + caption + this);
    3.63 -//            for (int i = 0; i <= m; i++)
    3.64 -//                s.println(prefix + "\tat " + trace[i]);
    3.65 -//            if (framesInCommon != 0)
    3.66 -//                s.println(prefix + "\t... " + framesInCommon + " more");
    3.67 -//
    3.68 -//            // Print suppressed exceptions, if any
    3.69 -//            for (Throwable se : getSuppressed())
    3.70 -//                se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION,
    3.71 -//                                           prefix +"\t", dejaVu);
    3.72 -//
    3.73 -//            // Print cause, if any
    3.74 -//            Throwable ourCause = getCause();
    3.75 -//            if (ourCause != null)
    3.76 -//                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
    3.77 -//        }
    3.78 -//    }
    3.79 -//
    3.80 -//    /**
    3.81 -//     * Prints this throwable and its backtrace to the specified
    3.82 -//     * print writer.
    3.83 -//     *
    3.84 -//     * @param s {@code PrintWriter} to use for output
    3.85 -//     * @since   JDK1.1
    3.86 -//     */
    3.87 -//    public void printStackTrace(PrintWriter s) {
    3.88 -//        printStackTrace(new WrappedPrintWriter(s));
    3.89 -//    }
    3.90 -//
    3.91 +    /**
    3.92 +     * Prints this throwable and its backtrace to the specified print stream.
    3.93 +     *
    3.94 +     * @param s {@code PrintStream} to use for output
    3.95 +     */
    3.96 +    public void printStackTrace(PrintStream s) {
    3.97 +        s.print(getClass().getName());
    3.98 +        s.print(": ");
    3.99 +        s.println(getMessage());
   3.100 +    }
   3.101 +
   3.102 +    /**
   3.103 +     * Prints this throwable and its backtrace to the specified
   3.104 +     * print writer.
   3.105 +     *
   3.106 +     * @param s {@code PrintWriter} to use for output
   3.107 +     * @since   JDK1.1
   3.108 +     */
   3.109 +    public void printStackTrace(PrintWriter s) {
   3.110 +        s.append(getClass().getName()).append(": ").println(getMessage());
   3.111 +    }
   3.112 +
   3.113  //    /**
   3.114  //     * Wrapper class for PrintStream and PrintWriter to enable a single
   3.115  //     * implementation of printStackTrace.