openrewrite / openrewrite/rewrite

"java.lang.IllegalArgumentException: Could not parse as Java" While using Jackson in JavaTemplate

Open
#5,772 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

I am using

  • OpenRewrite v8.57.0
  • Maven/Gradle plugin v6.13
  • rewrite-core,rewrite-java v8.57.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>6.13.0</version>
        <configuration>
          <activeRecipes>
            tech.goksi.bug.recipe.ExampleRecipe
          </activeRecipes>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>tech.goksi</groupId>
            <artifactId>rewrite-bug-reproduce</artifactId>
            <version>1.0-SNAPSHOT</version>
          </dependency>
        </dependencies>
      </plugin>

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

I create reproducible example here.
Everything you have to do is to clone a repo, run mvn clean install and after that run mvn rewrite:run

What did you expect to see?

public class ExampleMigration {
  public ObjectMapperSupplier objectMapperSupplier() {
    ObjectMapper objectMapper = new ObjectMapper();

    return () -> objectMapper;
  }

 public ObjectMapper getter(ObjectMapperSupplier supplier) {
   return supplier.get();
  }
}

What did you see instead?

No changes

public class ExampleMigration {
  public ObjectMapperSupplier objectMapperSupplier() {
    ObjectMapper objectMapper = new ObjectMapper();

    return () -> objectMapper;
  }
}

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

org.openrewrite.internal.RecipeRunException: java.lang.IllegalArgumentException: Could not parse as Java:
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.goksi.bug.util.ObjectMapperSupplier;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
class Template {
/*__TEMPLATE__*/public ObjectMapper getter(ObjectMapperSupplier supplier) {return supplier.get();}/*__TEMPLATE_STOP__*/
}
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:281)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:311)
    at org.openrewrite.java.JavaVisitor.visitClassDeclaration (JavaVisitor.java:486)
    at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration (JavaTemplateJavaExtension.java:201)
    at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration (JavaTemplateJavaExtension.java:56)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava (J.java:1385)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:154)
    at org.openrewrite.java.JavaTemplate.apply (JavaTemplate.java:120)
    at tech.goksi.bug.recipe.ExampleRecipe$ExampleRecipeVisitor.visitClassDeclaration (ExampleRecipe.java:55)
    at tech.goksi.bug.recipe.ExampleRecipe$ExampleRecipeVisitor.visitClassDeclaration (ExampleRecipe.java:37)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava (J.java:1385)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:311)
    at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$9 (JavaVisitor.java:499)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:244)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:267)
    at org.openrewrite.java.JavaVisitor.visitCompilationUnit (JavaVisitor.java:499)
    at org.openrewrite.java.JavaIsoVisitor.visitCompilationUnit (JavaIsoVisitor.java:113)
    at org.openrewrite.java.JavaIsoVisitor.visitCompilationUnit (JavaIsoVisitor.java:30)
    at org.openrewrite.java.tree.J$CompilationUnit.acceptJava (J.java:1667)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:154)
    at org.openrewrite.Preconditions$Check.visit (Preconditions.java:175)
    at org.openrewrite.Preconditions$Check.visit (Preconditions.java:145)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$7 (RecipeRunCycle.java:210)
    at io.micrometer.core.instrument.AbstractTimer.recordCallable (AbstractTimer.java:178)
    at org.openrewrite.table.RecipeRunStats.recordEdit (RecipeRunStats.java:74)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$8 (RecipeRunCycle.java:206)
    at org.openrewrite.scheduling.RecipeStack.reduce (RecipeStack.java:60)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$9 (RecipeRunCycle.java:179)
    at org.openrewrite.internal.InMemoryLargeSourceSet.lambda$edit$0 (InMemoryLargeSourceSet.java:83)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:244)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:267)
    at org.openrewrite.internal.InMemoryLargeSourceSet.edit (InMemoryLargeSourceSet.java:82)
    at org.openrewrite.scheduling.RecipeRunCycle.editSources (RecipeRunCycle.java:177)
    at org.openrewrite.RecipeScheduler.runRecipeCycles (RecipeScheduler.java:84)
    at org.openrewrite.RecipeScheduler.scheduleRun (RecipeScheduler.java:41)
    at org.openrewrite.Recipe.run (Recipe.java:441)
    at org.openrewrite.Recipe.run (Recipe.java:437)
    at org.openrewrite.Recipe.run (Recipe.java:433)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo.runRecipe (AbstractRewriteBaseRunMojo.java:244)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo.listResults (AbstractRewriteBaseRunMojo.java:155)
    at org.openrewrite.maven.AbstractRewriteRunMojo.execute (AbstractRewriteRunMojo.java:66)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:41)
Caused by: java.lang.IllegalArgumentException: Could not parse as Java:
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.goksi.bug.util.ObjectMapperSupplier;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
class Template {
/*__TEMPLATE__*/public ObjectMapper getter(ObjectMapperSupplier supplier) {return supplier.get();}/*__TEMPLATE_STOP__*/
}
    at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13 (JavaTemplateParser.java:279)
    at java.util.Optional.orElseThrow (Optional.java:290)
    at org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate (JavaTemplateParser.java:279)
    at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9 (JavaTemplateParser.java:178)
    at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14 (JavaTemplateParser.java:302)
    at org.openrewrite.java.internal.template.JavaTemplateParser.cache (JavaTemplateParser.java:323)
    at org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree (JavaTemplateParser.java:302)
    at org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements (JavaTemplateParser.java:173)
    at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock (JavaTemplateJavaExtension.java:84)
    at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock (JavaTemplateJavaExtension.java:56)
    at org.openrewrite.java.tree.J$Block.acceptJava (J.java:848)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:311)
    at org.openrewrite.java.JavaVisitor.visitClassDeclaration (JavaVisitor.java:486)
    at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration (JavaTemplateJavaExtension.java:201)
    at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration (JavaTemplateJavaExtension.java:56)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava (J.java:1385)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:154)
    at org.openrewrite.java.JavaTemplate.apply (JavaTemplate.java:120)
    at tech.goksi.bug.recipe.ExampleRecipe$ExampleRecipeVisitor.visitClassDeclaration (ExampleRecipe.java:55)
    at tech.goksi.bug.recipe.ExampleRecipe$ExampleRecipeVisitor.visitClassDeclaration (ExampleRecipe.java:37)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava (J.java:1385)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:311)
    at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$9 (JavaVisitor.java:499)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:244)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:267)
    at org.openrewrite.java.JavaVisitor.visitCompilationUnit (JavaVisitor.java:499)
    at org.openrewrite.java.JavaIsoVisitor.visitCompilationUnit (JavaIsoVisitor.java:113)
    at org.openrewrite.java.JavaIsoVisitor.visitCompilationUnit (JavaIsoVisitor.java:30)
    at org.openrewrite.java.tree.J$CompilationUnit.acceptJava (J.java:1667)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:154)
    at org.openrewrite.Preconditions$Check.visit (Preconditions.java:175)
    at org.openrewrite.Preconditions$Check.visit (Preconditions.java:145)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$7 (RecipeRunCycle.java:210)
    at io.micrometer.core.instrument.AbstractTimer.recordCallable (AbstractTimer.java:178)
    at org.openrewrite.table.RecipeRunStats.recordEdit (RecipeRunStats.java:74)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$8 (RecipeRunCycle.java:206)
    at org.openrewrite.scheduling.RecipeStack.reduce (RecipeStack.java:60)
    at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$9 (RecipeRunCycle.java:179)
    at org.openrewrite.internal.InMemoryLargeSourceSet.lambda$edit$0 (InMemoryLargeSourceSet.java:83)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:244)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:267)
    at org.openrewrite.internal.InMemoryLargeSourceSet.edit (InMemoryLargeSourceSet.java:82)
    at org.openrewrite.scheduling.RecipeRunCycle.editSources (RecipeRunCycle.java:177)
    at org.openrewrite.RecipeScheduler.runRecipeCycles (RecipeScheduler.java:84)
    at org.openrewrite.RecipeScheduler.scheduleRun (RecipeScheduler.java:41)
    at org.openrewrite.Recipe.run (Recipe.java:441)
    at org.openrewrite.Recipe.run (Recipe.java:437)
    at org.openrewrite.Recipe.run (Recipe.java:433)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo.runRecipe (AbstractRewriteBaseRunMojo.java:244)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo.listResults (AbstractRewriteBaseRunMojo.java:155)
    at org.openrewrite.maven.AbstractRewriteRunMojo.execute (AbstractRewriteRunMojo.java:66)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:41)
[ERROR] The recipe produced an error. Please report this to the recipe author.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.032 s
[INFO] Finished at: 2025-07-18T14:35:22+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.13.0:run (default-cli) on project rewrite-bug-reproduce: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.13.0:run failed: Error while visiting src\main\java\tech\goksi\bug\ExampleMigration.java: java.lang.IllegalArgumentException: Could not parse as Java:
[ERROR] import com.fasterxml.jackson.databind.ObjectMapper;
[ERROR] import tech.goksi.bug.util.ObjectMapperSupplier;
[ERROR] import org.openrewrite.java.internal.template.__M__;
[ERROR] import org.openrewrite.java.internal.template.__P__;
[ERROR] class Template {
[ERROR] /*__TEMPLATE__*/public ObjectMapper getter(ObjectMapperSupplier supplier) {return supplier.get();}/*__TEMPLATE_STOP__*/
[ERROR] }
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:279)
[ERROR]   java.util.Optional.orElseThrow(Optional.java:290)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:279)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:178)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14(JavaTemplateParser.java:302)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:323)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:302)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:173)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:84)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:56)
[ERROR]   org.openrewrite.java.tree.J$Block.acceptJava(J.java:848)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:60)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
[ERROR]   org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:486)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration(JavaTemplateJavaExtension.java:201)
[ERROR]   ...
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.13.0:run (default-cli) on project rewrite-bug-reproduce: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.13.0:run failed: Error while visiting src\main\java\tech\goksi\bug\ExampleMigration.java: java.lang.IllegalArgumentException: Could not parse as Java:
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.goksi.bug.util.ObjectMapperSupplier;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
class Template {
/*__TEMPLATE__*/public ObjectMapper getter(ObjectMapperSupplier supplier) {return supplier.get();}/*__TEMPLATE_STOP__*/
}
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:279)
  java.util.Optional.orElseThrow(Optional.java:290)
  org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:279)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:178)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14(JavaTemplateParser.java:302)
  org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:323)
  org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:302)
  org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:173)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:84)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:56)
  org.openrewrite.java.tree.J$Block.acceptJava(J.java:848)
  org.openrewrite.java.tree.J.accept(J.java:60)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
  org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:486)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration(JavaTemplateJavaExtension.java:201)
  ...
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:333)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:41)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.13.0:run failed: Error while visiting src\main\java\tech\goksi\bug\ExampleMigration.java: java.lang.IllegalArgumentException: Could not parse as Java:
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.goksi.bug.util.ObjectMapperSupplier;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
class Template {
/*__TEMPLATE__*/public ObjectMapper getter(ObjectMapperSupplier supplier) {return supplier.get();}/*__TEMPLATE_STOP__*/
}
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:279)
  java.util.Optional.orElseThrow(Optional.java:290)
  org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:279)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:178)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14(JavaTemplateParser.java:302)
  org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:323)
  org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:302)
  org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:173)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:84)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:56)
  org.openrewrite.java.tree.J$Block.acceptJava(J.java:848)
  org.openrewrite.java.tree.J.accept(J.java:60)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
  org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:486)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration(JavaTemplateJavaExtension.java:201)
  ...
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:133)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:41)
Caused by: java.lang.RuntimeException: Error while visiting src\main\java\tech\goksi\bug\ExampleMigration.java: java.lang.IllegalArgumentException: Could not parse as Java:
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.goksi.bug.util.ObjectMapperSupplier;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
class Template {
/*__TEMPLATE__*/public ObjectMapper getter(ObjectMapperSupplier supplier) {return supplier.get();}/*__TEMPLATE_STOP__*/
}
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:279)
  java.util.Optional.orElseThrow(Optional.java:290)
  org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:279)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:178)
  org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14(JavaTemplateParser.java:302)
  org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:323)
  org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:302)
  org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:173)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:84)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitBlock(JavaTemplateJavaExtension.java:56)
  org.openrewrite.java.tree.J$Block.acceptJava(J.java:848)
  org.openrewrite.java.tree.J.accept(J.java:60)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
  org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:486)
  org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitClassDeclaration(JavaTemplateJavaExtension.java:201)
  ...
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer$1.lambda$preVisit$0 (AbstractRewriteBaseRunMojo.java:381)
    at java.util.Optional.ifPresent (Optional.java:159)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer$1.preVisit (AbstractRewriteBaseRunMojo.java:378)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer$1.preVisit (AbstractRewriteBaseRunMojo.java:374)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo_ResultsContainer_1_JavaVisitor.preVisit (AbstractRewriteBaseRunMojo_ResultsContainer_1_JavaVisitor.zig:102)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:242)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:311)
    at org.openrewrite.java.JavaVisitor.visitClassDeclaration (JavaVisitor.java:486)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava (J.java:1385)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:311)
    at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$9 (JavaVisitor.java:499)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:244)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:267)
    at org.openrewrite.java.JavaVisitor.visitCompilationUnit (JavaVisitor.java:499)
    at org.openrewrite.java.tree.J$CompilationUnit.acceptJava (J.java:1667)
    at org.openrewrite.java.tree.J.accept (J.java:60)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:245)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer.getRecipeErrors (AbstractRewriteBaseRunMojo.java:385)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer.getFirstException (AbstractRewriteBaseRunMojo.java:365)
    at org.openrewrite.maven.AbstractRewriteRunMojo.execute (AbstractRewriteRunMojo.java:68)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:41)

Are you interested in contributing a fix to OpenRewrite?

Yes

Additional info

One funny thing i encountered during debugging is that if I step into ReloadableJava8ParserVisitor#scan (the one that is throwing NPE) and then step out with debugger, it is no longer throwing npe. If i resume code after that everything is working as expected

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure in the linked rewrite-bug-reproduce project with mvn clean install followed by mvn rewrite:run. Start in JavaTemplateParser.java at compileTemplate, then trace the call from JavaTemplate.java and ExampleRecipe.java. Done means the template applies without a parse exception and produces the expected getter method.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.