Skip to content

Commit 1443706

Browse files
Groovy: fix parsing of @immutable @tostring (#7487)
1 parent 6d97537 commit 1443706

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,9 @@ public J.Annotation visitAnnotation(AnnotationNode annotation, RewriteGroovyClas
27872787
Space prefix = sourceBefore("@");
27882788
NameTree annotationType = visitTypeTree(annotation.getClassNode());
27892789
JContainer<Expression> arguments = null;
2790-
if (!annotation.getMembers().isEmpty()) {
2790+
// AST transforms like @Immutable can attach synthetic members to other annotations
2791+
// (e.g. @ToString) that don't appear in source — only parse arguments if "(" is actually next.
2792+
if (!annotation.getMembers().isEmpty() && sourceStartsWith("(")) {
27912793
arguments = JContainer.build(
27922794
sourceBefore("("),
27932795
annotation.getMembers().entrySet().stream()

rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/AnnotationTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ void synchronizedAnnotation() {
276276
groovy(
277277
"""
278278
package org.dummy
279-
279+
280280
import groovy.transform.Synchronized
281-
281+
282282
class Foo {
283-
283+
284284
@Synchronized
285285
void bar() {
286286
println('Hello World')
@@ -290,4 +290,22 @@ void bar() {
290290
)
291291
);
292292
}
293+
294+
@Test
295+
void immutableAndToString() {
296+
rewriteRun(
297+
groovy(
298+
"""
299+
import groovy.transform.Immutable
300+
import groovy.transform.ToString
301+
302+
@Immutable @ToString
303+
class A {
304+
void foo() {
305+
}
306+
}
307+
"""
308+
)
309+
);
310+
}
293311
}

0 commit comments

Comments
 (0)