diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java index ecfb33f85a8..08d80ee3a14 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java @@ -46,7 +46,6 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringReader; -import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -574,12 +573,7 @@ public void logCommandLineArguments(String[] commandLineArguments) { * @param e the given exception to log */ public void logException(Exception e) { - StringWriter writer = new StringWriter(); - PrintWriter printWriter = new PrintWriter(writer); - e.printStackTrace(printWriter); - printWriter.flush(); - printWriter.close(); - final String stackTrace = writer.toString(); + final String stackTrace = Util.getStackTrace(e).toString(); if ((this.tagBits & Logger.XML) != 0) { LineNumberReader reader = new LineNumberReader(new StringReader(stackTrace)); String line; diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java index e7f89c084c6..13f821b58b4 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java @@ -81,13 +81,11 @@ package org.eclipse.jdt.internal.compiler.problem; import java.io.CharConversionException; -import java.io.PrintWriter; -import java.io.StringWriter; import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Set; import java.util.Map; +import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -1455,18 +1453,10 @@ public void cannotReadSource(CompilationUnitDeclaration unit, AbortCompilationUn 0); return; } - StringWriter stringWriter = new StringWriter(); - PrintWriter writer = new PrintWriter(stringWriter); if (verbose) { - abortException.exception.printStackTrace(writer); - System.err.println(stringWriter.toString()); - stringWriter = new StringWriter(); - writer = new PrintWriter(stringWriter); - } - writer.print(abortException.exception.getClass().getName()); - writer.print(':'); - writer.print(abortException.exception.getMessage()); - String exceptionTrace = stringWriter.toString(); + System.err.println(Util.getStackTrace(abortException.exception)); + } + String exceptionTrace = abortException.exception.getClass().getName() + ':' + abortException.exception.getMessage(); String[] arguments = new String[]{ fileName, exceptionTrace }; this.handle( IProblem.CannotReadSource, diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/Util.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/Util.java index 27c21a53009..20790159b19 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/Util.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/Util.java @@ -23,7 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; -import java.io.StringWriter; +import java.io.Writer; import java.net.URI; import java.nio.ByteBuffer; import java.nio.CharBuffer; @@ -558,9 +558,7 @@ private static boolean startsWith(byte[] a, byte[] start) { * @return one line summary for an exception */ public static String getExceptionSummary(Throwable exception) { - StringWriter stringWriter = new StringWriter(); - exception.printStackTrace(new PrintWriter(stringWriter)); - StringBuffer buffer = stringWriter.getBuffer(); + CharSequence buffer = getStackTrace(exception); StringBuffer exceptionBuffer = new StringBuffer(50); exceptionBuffer.append(exception.toString()); // only keep leading frame portion of the trace (i.e. line no. 2 from the stacktrace) @@ -569,7 +567,7 @@ public static String getExceptionSummary(Throwable exception) { case '\n': case '\r' : if (line2Start > 0) { - exceptionBuffer.append(' ').append(buffer.substring(line2Start, i)); + exceptionBuffer.append(' ').append(buffer.subSequence(line2Start, i)); break lookupLine2; } lineSep++; @@ -588,6 +586,30 @@ public static String getExceptionSummary(Throwable exception) { return exceptionBuffer.toString(); } + public static CharSequence getStackTrace(Throwable exception) { + StringBuilder builder = new StringBuilder(); + exception.printStackTrace(new PrintWriter(new Writer() { + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + builder.append(cbuf, off, len); + } + + @Override + public void write(String str, int off, int len) throws IOException { + builder.append(str, off, len); + } + + @Override + public void flush() throws IOException { // nothing to do + } + + @Override + public void close() throws IOException { // nothing to do + } + })); + return builder; + } + public static int getLineNumber(int position, int[] lineEnds, int g, int d) { if (lineEnds == null) return 1; diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java index dc1406048b7..6128a610bd0 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java @@ -16,7 +16,6 @@ package org.eclipse.jdt.core.dom; import java.io.PrintWriter; -import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -1136,11 +1135,8 @@ private ASTNode internalCreateAST(IProgressMonitor monitor) { } } catch(JavaModelException e) { // an error occured accessing the java element - StringWriter stringWriter = new StringWriter(); - try (PrintWriter writer = new PrintWriter(stringWriter)) { - e.printStackTrace(writer); - } - throw new IllegalStateException(String.valueOf(stringWriter.getBuffer())); + CharSequence stackTrace = org.eclipse.jdt.internal.compiler.util.Util.getStackTrace(e); + throw new IllegalStateException(stackTrace.toString()); } } } @@ -1206,11 +1202,8 @@ private ASTNode internalCreateAST(IProgressMonitor monitor) { sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), fileNameString, this.typeRoot); } catch(JavaModelException e) { // an error occured accessing the java element - StringWriter stringWriter = new StringWriter(); - try (PrintWriter writer = new PrintWriter(stringWriter)) { - e.printStackTrace(writer); - } - throw new IllegalStateException(String.valueOf(stringWriter.getBuffer())); + CharSequence stackTrace = org.eclipse.jdt.internal.compiler.util.Util.getStackTrace(e); + throw new IllegalStateException(stackTrace.toString()); } } else if (this.rawSource != null) { needToResolveBindings =