openrewrite / openrewrite/rewrite
Unable to add an annotation via KotlinTemplate
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 570
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
Context
KotlinTemplate reuses JavaTemplateJavaExtension and AnnotationTemplateGenerator, which copies parts of the original tree and tries to parse it as java. In the example below, kotlin tree can not be parsed as java.
What version of OpenRewrite are you using?
I am using
- OpenRewrite v8.60.1
- rewrite-kotlin
What is the smallest, simplest way to reproduce the problem?
Unit test
@Test
void addAnnotation() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new KotlinVisitor<>() {
@Override
public J visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
return KotlinTemplate.builder("@SuppressWarnings")
.build()
.apply(getCursor(), multiVariable.getCoordinates().replaceAnnotations());
}
})),
kotlin(
"""
class Test {
fun foo() {
val b1 = 1 == 2
}
}
""",
"""
class Test {
fun foo() {
@SuppressWarnings val b1 = 1 == 2
}
}
"""
));
}
What did you expect to see?
Annotation added to the variable:
@SuppressWarnings val b1 = 1 == 2
What did you see instead?
Failure (see stacktrace) and warnings about invalid kotlin
/710847645588375/openRewriteFile.kt:2:6: error: unexpected tokens (use ';' to separate expressions on the same line)
int $variable;}}
^
/710847645588375/openRewriteFile.kt:2:15: error: expecting an element
int $variable;}}
^
/710847645588375/openRewriteFile.kt:3:1: error: expected annotation identifier after '@'
@interface $Placeholder {}
What is the full stack trace of any errors you encountered?
Stacktrace
Failed to run recipe at Cursor{VariableDeclarations->JRightPadded(element=final b1 = 1 == 2, after=Space(comments=<0 comments>, whitespace=<empty>))->Block->MethodDeclaration->JRightPadded(element=MethodDeclaration{Test{name=foo,return=kotlin.Unit,parameters=[]}}, after=Space(comments=<0 comments>, whitespace=<empty>))->Block->ClassDeclaration->CompilationUnit->root}
java.lang.AssertionError: Failed to run recipe at Cursor{VariableDeclarations->JRightPadded(element=final b1 = 1 == 2, after=Space(comments=<0 comments>, whitespace=<empty>))->Block->MethodDeclaration->JRightPadded(element=MethodDeclaration{Test{name=foo,return=kotlin.Unit,parameters=[]}}, after=Space(comments=<0 comments>, whitespace=<empty>))->Block->ClassDeclaration->CompilationUnit->root}
at org.openrewrite.test.RewriteTest.lambda$defaultExecutionContext$14(RewriteTest.java:645)
at org.openrewrite.test.RewriteTest$$Lambda/0x0000000800285ba8.accept(Unknown Source)
at org.openrewrite.scheduling.RecipeRunCycle.handleError(RecipeRunCycle.java:291)
at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$8(RecipeRunCycle.java:230)
at org.openrewrite.scheduling.RecipeRunCycle$$Lambda/0x00000008007bd780.apply(Unknown Source)
at org.openrewrite.scheduling.RecipeStack.reduce(RecipeStack.java:60)
at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$9(RecipeRunCycle.java:179)
at org.openrewrite.scheduling.RecipeRunCycle$$Lambda/0x00000008007bd2d0.apply(Unknown Source)
at org.openrewrite.internal.InMemoryLargeSourceSet.lambda$edit$0(InMemoryLargeSourceSet.java:86)
at org.openrewrite.internal.InMemoryLargeSourceSet$$Lambda/0x00000008007bd528.apply(Unknown Source)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:245)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:269)
at org.openrewrite.internal.InMemoryLargeSourceSet.edit(InMemoryLargeSourceSet.java:85)
at org.openrewrite.RecipeScheduler$$Lambda/0x00000008007bcb90.apply(Unknown Source)
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:442)
at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:383)
at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)
at org.openrewrite.kotlin.KotlinTemplateTest.addAnnotation(KotlinTemplateTest.java:103)
at java.base/java.lang.invoke.LambdaForm$DMH/0x0000000800254000.invokeVirtual(LambdaForm$DMH)
at java.base/java.lang.invoke.LambdaForm$MH/0x0000000800138800.invoke(LambdaForm$MH)
at java.base/java.lang.invoke.Invokers$Holder.invokeExact_MT(Invokers$Holder)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: org.openrewrite.internal.RecipeRunException: java.lang.IllegalArgumentException: Could not parse as Java:
class Test{fun foo(){/*__TEMPLATE_cfcc2025-6662__*/@SuppressWarnings
int $variable;}}
@interface $Placeholder {}
at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:281)
at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:154)
at org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:121)
at org.openrewrite.kotlin.KotlinTemplateTest$3.visitVariableDeclarations(KotlinTemplateTest.java:109)
at org.openrewrite.kotlin.KotlinTemplateTest$3.visitVariableDeclarations(KotlinTemplateTest.java:104)
at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:6166)
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.visitRightPadded(JavaVisitor.java:1310)
at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:392)
at org.openrewrite.java.JavaVisitor$$Lambda/0x0000000800728658.apply(Unknown Source)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:245)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:269)
at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:391)
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.visitMethodDeclaration(JavaVisitor.java:839)
at org.openrewrite.java.tree.J$MethodDeclaration.acceptJava(J.java:4036)
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.visitRightPadded(JavaVisitor.java:1310)
at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:392)
at org.openrewrite.java.JavaVisitor$$Lambda/0x0000000800728658.apply(Unknown Source)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:245)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:269)
at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:391)
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:474)
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.kotlin.KotlinVisitor.lambda$visitCompilationUnit$2(KotlinVisitor.java:57)
at org.openrewrite.kotlin.KotlinVisitor$$Lambda/0x0000000800721000.apply(Unknown Source)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:245)
at org.openrewrite.internal.ListUtils.map(ListUtils.java:269)
at org.openrewrite.kotlin.KotlinVisitor.visitCompilationUnit(KotlinVisitor.java:56)
at org.openrewrite.kotlin.tree.K$CompilationUnit.acceptKotlin(K.java:249)
at org.openrewrite.kotlin.tree.K.accept(K.java:57)
at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:154)
at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$7(RecipeRunCycle.java:210)
at org.openrewrite.scheduling.RecipeRunCycle$$Lambda/0x00000008007bd9b0.call(Unknown Source)
at io.micrometer.core.instrument.AbstractTimer.recordCallable(AbstractTimer.java:147)
at org.openrewrite.table.RecipeRunStats.recordEdit(RecipeRunStats.java:74)
at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$8(RecipeRunCycle.java:206)
... 23 more
Caused by: java.lang.IllegalArgumentException: Could not parse as Java:
class Test{fun foo(){/*__TEMPLATE_cfcc2025-6662__*/@SuppressWarnings
int $variable;}}
@interface $Placeholder {}
at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:284)
at org.openrewrite.java.internal.template.JavaTemplateParser$$Lambda/0x0000000800800460.get(Unknown Source)
at java.base/java.util.Optional.orElseThrow(Optional.java:403)
at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$14(JavaTemplateParser.java:284)
at org.openrewrite.java.internal.template.JavaTemplateParser$$Lambda/0x0000000800800240.get(Unknown Source)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:283)
at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseAnnotations$10(JavaTemplateParser.java:234)
at org.openrewrite.java.internal.template.JavaTemplateParser$$Lambda/0x00000008007d1578.get(Unknown Source)
at org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:337)
at org.openrewrite.java.internal.template.JavaTemplateParser.parseAnnotations(JavaTemplateParser.java:231)
at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitVariableDeclarations(JavaTemplateJavaExtension.java:490)
at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitVariableDeclarations(JavaTemplateJavaExtension.java:56)
at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:6166)
at org.openrewrite.java.tree.J.accept(J.java:60)
at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
... 75 more
Are you interested in contributing a fix to OpenRewrite?
Yes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with KotlinTemplateTest.addAnnotation, which reproduces the failure through KotlinTemplate.apply() and reports the JavaTemplateJavaExtension and AnnotationTemplateGenerator path. Trace how the Kotlin tree is prepared for annotation parsing, then run the test to verify that @SuppressWarnings is added to the variable without Java parsing errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100