Skip to content

Fix KotlinTemplate stub generation for annotations on methods and variables#8168

Merged
MBoegers merged 4 commits into
mainfrom
cri-2670
Jul 6, 2026
Merged

Fix KotlinTemplate stub generation for annotations on methods and variables#8168
MBoegers merged 4 commits into
mainfrom
cri-2670

Conversation

@MBoegers

@MBoegers MBoegers commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

What's changed?

AnnotationTemplateGenerator hardcoded Java syntax for the dummy declarations that annotation template stubs attach to ( void $method() {} and int $variable;). This change extracts them into protected hooks — addDummyMethod(StringBuilder), addDummyVariable(StringBuilder), and the variable() helper — alongside the existing addDummyClass/addDummyAnnotationType hooks from #6522, and overrides them in KotlinAnnotationTemplateGenerator with Kotlin syntax.

What's your motivation?

Any KotlinTemplate applied with annotation coordinates (e.g. annotation.getCoordinates().replaceArguments()) on a method or property failed, because the generated stub mixed Java member syntax into a Kotlin source:

Could not parse as Java:
class Test{/*__TEMPLATE_cfcc2025-6662__*/@Example("RedundantSuppression")
 void $method() {}}
annotation class `$Placeholder` {}

Two subtleties beyond the plain syntax swap:

  • Stub fragments are concatenated on a single line, and a return ...; is appended when the enclosing function declares a return type — so the Kotlin stubs need explicit ; terminators (val `$variable`: Int = 0;), otherwise a local val annotation inside fun foo(): Int produced the unparseable val `$variable`: Int = 0return null;.
  • variable() (used to stub out locals that precede the templated annotation) emitted Java declaration order (Int y;); the Kotlin override emits val y: Int.

Base implementations keep byte-identical output, so Java and Groovy templates are unaffected.

Anything in particular you'd like reviewers to focus on?

  • ScalaAnnotationTemplateGenerator has the same latent gap (it still inherits the Java method/variable stubs), left for a follow-up since rewrite-scala has no annotation-template test coverage yet.
  • Test coverage spans the distinct stub-assembly paths: method in class, top-level function, property, class, local val (with enclosing return type, and preceded by a typed local), parameter substitution (#{any()}), and cursor-at-declaration via addAnnotation.

Checklist

…iables

AnnotationTemplateGenerator hardcoded Java syntax for the dummy method
(" void $method() {}") and dummy variable (" int $variable;") that
annotation template stubs attach to, so any KotlinTemplate applied with
annotation coordinates (e.g. replaceArguments()) on a method or property
produced a stub the Kotlin parser rejects:

    Could not parse as Java:
    class Test{/*__TEMPLATE__*/@example("RedundantSuppression")
     void $method() {}}

Extract the stubs into protected hooks (addDummyMethod, addDummyVariable,
variable()) alongside the existing addDummyClass/addDummyAnnotationType
from #6522, and override them in KotlinAnnotationTemplateGenerator with
Kotlin syntax. The stub fragments are concatenated on a single line with
an appended `return ...;` when the enclosing function has a return type,
so the Kotlin stubs carry explicit `;` terminators.

@timtebeek timtebeek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to fix this generically, thanks!

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jul 6, 2026
@MBoegers MBoegers merged commit 4ed2979 into main Jul 6, 2026
@MBoegers MBoegers deleted the cri-2670 branch July 6, 2026 10:18
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants