openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.73k stars 419 forks source link

ReloadableJava21Parser.parseInputsToCompilerAst() causing exception. #5216

Open jimbethancourt opened 1 month ago

jimbethancourt commented 1 month ago

What version of OpenRewrite are you using?

I am using the org.openrewrite.recipe:rewrite-recipe-bom:3.4.0 BOM in RefactorFirst. A user reported the stacktrace below. Unfortunately I don't have any context about the line of code that was encountered that led to the issue. Thankfully the parser continued parsing.

How are you running OpenRewrite?

I am calling the OpenRewrite Java parser from the RefactorFirst codebase-graph-builder module.

What is the smallest, simplest way to reproduce the problem?

Unfortunately I'm not sure, but hopefully the stacktrace below will help.

What is the full stack trace of any errors you encountered?

[INFO] Analyzing Cycles
org.openrewrite.java.JavaParsingException: Failed symbol entering or attribution
at org.openrewrite.java.isolated. ReloadableJava21Parser.parseInputsToCompilerAst(ReloadableJava21Parser.java:244) at org.openrewrite.java.isolated. ReloadableJava21Parser.parseInputs (ReloadableJava21Parser.java:173)
at org.openrewrite.java.Java21Parser.parseInputs(Java21Parser.java:39)
at org.openrewrite.Parser.parse(Parser.java:59)
at org.hjug.graphbuilder.JavaGraphBuilder.processWithOpenRewrite(JavaGraphBuilder.java:76)
at org.hjug.graphbuilder.JavaGraphBuilder.getClassReferences (JavaGraphBuilder.java:38)
at org.hjug.cbc.CycleRanker.generateClassReferencesGraph(CycleRanker.java:30)
at org.hjug.cbc.CycleRanker.performCycleAnalysis (CycleRanker.java:40)
at org.hjug.refactorfirst.report.SimpleHtmlReport.generateReport (SimpleHtmlReport.java:235)
at org.hjug.refactorfirst.report.SimpleHtmlReport.generateReport (SimpleHtmlReport.java:144)
at org.hjug.refactorfirst.report.SimpleHtmlReport.execute(SimpleHtmlReport.java:112)
at org.hjug.mavenreport.RefactorFirstHtmlReport.execute(RefactorFirstHtmlReport.java:61)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:370)
at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:351)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:171)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:163)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:299)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:963)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:296)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.codehaus.plexus.classworlds.launcher. Launcher. LaunchEnhanced (Launcher.java:283)
at org.codehaus.plexus.classworlds.launcher. Launcher.launch(Launcher.java:226)
at org.codehaus.plexus.classworlds.launcher. Launcher.mainWithExitCode(Launcher.java:407)
at org.codehaus.plexus.classworlds.launcher. Launcher.main(Launcher.java:348)
Caused by: java.lang.AssertionError
at com.sun.tools.javac.util.Assert.error(Assert.java:155)
at com.sun.tools.javac.util.Assert.checkNonNull(Assert.java:62)
at com.sun.tools.javac.util.ListBuffer.append(ListBuffer.java:127)
at com.sun.tools.javac.comp.Attr$1.visitYield(Attr.java:1637)
at com.sun.tools.javac.tree.JCTree$JCYield.accept(JCTree.java:1677) at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50) at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:58)
at com.sun.tools.javac.comp.Attr.lambda$visitSwitchExpression$7(Attr.java:1644)
at com.sun.tools.javac.comp.Attr.handleSwitch(Attr.java:1850)
at com.sun.tools.javac.comp.Attr.visitSwitchExpression(Attr.java:1629)
at com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1399)
at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662)
at com.sun.tools.javac.comp.Attr.visitReturn(Attr.java:2475)
at com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1728)
at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662)
at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:736)
at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:755)
at com.sun.tools.javac.comp.Attr.lambda$visitSwitch$6(Attr.java:1606)
at com.sun.tools.javac.comp.Attr.handleSwitch(Attr.java:1850)
at com.sun.tools.javac.comp.Attr.visitSwitch(Attr.java:1605)
at com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:1299) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662) at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:736) at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:755) at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1439) at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree. e.java:1092) at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662)
at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:736)
at com.sun.tools.javac.comp.Attr.visitMethodDef (Attr.java:1229)
at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:916)
at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:662)
at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:736)
at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:5655)
at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5543)
at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5367)
at com.sun.tools.javac.comp.Attr.attrib(Attr.java:5306)
at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1359)
at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1332)
at org.openrewrite.java.isolated. ReloadableJava21Parser.parseInputsToCompilerAst(ReloadableJava21Parser.java:240) ... 33 more

Are you interested in contributing a fix to OpenRewrite?

Possibly

timtebeek commented 1 month ago

Thanks for the report @jimbethancourt ! it looks to be failing on a visitYield inside visitSwitchExpression. Now we do have tests around the usage of yield, as seen here and in the same file: https://github.com/openrewrite/rewrite/blob/e1c09ab2dbb07f99d813e1908e6df75900ea9256/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/SwitchExpressionTest.java#L96-L115

The question then is what kind of pattern was used here, and whether we can turn that into a new unit test and ultimately parser fix. Any additional details you can provide would be helpful!

jimbethancourt commented 1 month ago

I'll reach out to the user and ask them to enable debugging