rewrite: Type attribution terminated after first Exception thrown
What version of OpenRewrite are you using?
I am using
- OpenRewrite v8.49.1
How are you running OpenRewrite?
I am running the parser from in-house CLI, separate from the regular builds. My entry-point is: I set up a JavaParser with a classpath that sometimes is incomplete (typically dependencies from sub-modules in same project are missing - unless they have been published to maven repo) Normally using the Java21 parser Deep down in the java compiler there are asserts throwing exception. Have seen this related to switch statements involve these missing types.
Navigating into the ReloadableJava21Parser, line 240 shows the call that throws exception: https://github.com/openrewrite/rewrite/blob/7ac1bc8f07d030e9861c97f7a12b21b81da0b4e7/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21Parser.java#L240
In such case, the compiler.todo still has a (potentially large) number of entries that are left unhandled.
Inspection of the attribute method shows that it itereates over the list and remove/process one element at a time. So I have experimented by wrapping the call to attribute with these lines (continuing processing even after a failure)
while (!compiler.todo.isEmpty()) {
try {
compiler.attribute(compiler.todo);
} catch (Throwable t) {
ctx.getOnError().accept(new JavaParsingException("Failed symbol entering or attribution", t));
}
}
That seems to do the trick.
Even if there are some issues with type attribution for some classes, most of them are handled correctly. I do analysis of log statements, and need type info for both the method invocation to log4j/slf4, and also the type info for the parameters. I do have a fallback processing on my side (needed that before lombok was supported) where method name is used to categorize the invocation (debug,info,warn…etc) - but that is a second best solution.
I also experimented by manually patching in the missing jar files to the classpath (not sustainable in large scale) - and the attribution was correct (without the above code patch)
Hope this is something you can have a look at even if procedure to recreate / stack trace is missing.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 22 (13 by maintainers)
Commits related to this issue
- Continue type attribution in a case of exception Since type attribution is a best-effort, we continue it even in the presence of errors. But in a current implementation for `JavaCompiler#attribute(Qu... — committed to openrewrite/rewrite by Laurens-W 2 years ago
- Continue type attribution in a case of exception Since type attribution is a best-effort, we continue it even in the presence of errors. But in a current implementation for `JavaCompiler#attribute(Qu... — committed to openrewrite/rewrite by arodionov 2 years ago
- Continue type attribution in a case of exception (#5456) Since type attribution is a best-effort, we continue it even in the presence of errors. But in a current implementation for `JavaCompiler#attr... — committed to openrewrite/rewrite by arodionov 2 years ago
Sorry for late reply. I am using this JDK installed using SdkMan | >>> | 21.0.1 | amzn | local only | 21.0.1-amzn
Here is the breakpoint in ReloadableJava21Parser, and you can see that there are still 219 items to be handled
…and here is the “bottom” of the stacktrace showing that it has to do with a switch statement, and that the java compiler is using Assert - and and exception was thrown. (From the 8.49.0 version of OpenRewrite)
com.sun.tools.javac.util.Assert.error(Assert.java:155) com.sun.tools.javac.util.Assert.checkNonNull(Assert.java:62) com.sun.tools.javac.util.ListBuffer.append(ListBuffer.java:127) com.sun.tools.javac.comp.Attr$1.visitYield(Attr.java:1637) com.sun.tools.javac.tree.JCTree$JCYield.accept(JCTree.java:1677) com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50) com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:58) com.sun.tools.javac.comp.Attr.lambda$visitSwitchExpression$7(Attr.java:1644) com.sun.tools.javac.comp.Attr.handleSwitch(Attr.java:1850) com.sun.tools.javac.comp.Attr.visitSwitchExpression(Attr.java:1629) com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1399) com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662) com.sun.tools.javac.comp.Attr.visitConditional(Attr.java:2064) com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1536) com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662) com.sun.tools.javac.comp.Attr.visitReturn(Attr.java:2475) com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1728)
I have updated to latest version, 8.51.0 - and same result.
Will try to set a breakpoint in the Assert class itself to get more context.
Sure. To start with I will collect more detailed information on compiler version and also a detailed stacktrace showing exactly where the java compiler stops. Then I will try to reproduce by digging out the relevant code. When it comes to version of Java21Parser, I am using 8.49 (March, 27)