Skip to content

Commit

Permalink
Unify printing exception stack-traces to String
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 21, 2023
1 parent 3d92057 commit eb364b2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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++;
Expand All @@ -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;
Expand Down
15 changes: 4 additions & 11 deletions org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit eb364b2

Please sign in to comment.