Skip to content

Commit

Permalink
Catch new exceptions from Method invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 6, 2024
1 parent 2042135 commit 5eb6d3f
Showing 1 changed file with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Path;
Expand Down Expand Up @@ -1670,30 +1671,37 @@ private <J2 extends J> J2 convert(Tree t) {
DocCommentTree commentTree = (DocCommentTree) getCommentTreeMethod.invoke(docCommentTable, (JCTree) t);
@SuppressWarnings("unchecked") J2 j = (J2) scan(t, formatWithCommentTree(prefix, (JCTree) t, commentTree));
return j;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
reportJavaParsingException(ex);
throw new IllegalStateException("Failed to invoke getCommentTree method", ex);
} catch (Throwable ex) {
// this SHOULD never happen, but is here simply as a diagnostic measure in the event of unexpected exceptions
StringBuilder message = new StringBuilder("Failed to convert for the following cursor stack:");
message.append("--- BEGIN PATH ---\n");

List<Tree> paths = stream(getCurrentPath().spliterator(), false).toList();
for (int i = paths.size(); i-- > 0; ) {
JCTree tree = (JCTree) paths.get(i);
if (tree instanceof JCCompilationUnit) {
message.append("JCCompilationUnit(sourceFile = ").append(((JCCompilationUnit) tree).sourcefile.getName()).append(")\n");
} else if (tree instanceof JCClassDecl) {
message.append("JCClassDecl(name = ").append(((JCClassDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else if (tree instanceof JCVariableDecl) {
message.append("JCVariableDecl(name = ").append(((JCVariableDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else {
message.append(tree.getClass().getSimpleName()).append("(line = ").append(lineNumber(tree)).append(")\n");
}
}
reportJavaParsingException(ex);
throw ex;
}
}

message.append("--- END PATH ---\n");
private void reportJavaParsingException(Throwable ex) {
// this SHOULD never happen, but is here simply as a diagnostic measure in the event of unexpected exceptions
StringBuilder message = new StringBuilder("Failed to convert for the following cursor stack:");
message.append("--- BEGIN PATH ---\n");

ctx.getOnError().accept(new JavaParsingException(message.toString(), ex));
throw ex;
List<Tree> paths = stream(getCurrentPath().spliterator(), false).toList();
for (int i = paths.size(); i-- > 0; ) {
JCTree tree = (JCTree) paths.get(i);
if (tree instanceof JCCompilationUnit) {
message.append("JCCompilationUnit(sourceFile = ").append(((JCCompilationUnit) tree).sourcefile.getName()).append(")\n");
} else if (tree instanceof JCClassDecl) {
message.append("JCClassDecl(name = ").append(((JCClassDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else if (tree instanceof JCVariableDecl) {
message.append("JCVariableDecl(name = ").append(((JCVariableDecl) tree).name).append(", line = ").append(lineNumber(tree)).append(")\n");
} else {
message.append(tree.getClass().getSimpleName()).append("(line = ").append(lineNumber(tree)).append(")\n");
}
}

message.append("--- END PATH ---\n");

ctx.getOnError().accept(new JavaParsingException(message.toString(), ex));
}

private <J2 extends J> JRightPadded<J2> convert(Tree t, Function<Tree, Space> suffix) {
Expand Down

0 comments on commit 5eb6d3f

Please sign in to comment.