Skip to content

Commit 8d0b753

Browse files
Groovy - handling of @Synchronized annotation (#6562)
* Hacky handling of @synchronized annotation * check for any annotation * Check using TypeUtils.isOfClassType
1 parent 88b185a commit 8d0b753

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,11 @@ class B { class B {
674674
body = bodyVisitor.visit(method.getCode());
675675
}
676676
} else {
677-
body = bodyVisitor.visit(method.getCode());
677+
if (annotations.stream().anyMatch(a -> TypeUtils.isOfClassType(a.getAnnotationType().getType(), "groovy.transform.Synchronized"))) {
678+
body = bodyVisitor.visit(((SynchronizedStatement) method.getCode()).getCode());
679+
} else {
680+
body = bodyVisitor.visit(method.getCode());
681+
}
678682
}
679683
}
680684

@@ -2443,7 +2447,7 @@ private JRightPadded<Statement> convertTopLevelStatement(SourceUnit unit, ASTNod
24432447

24442448
// The groovy compiler discards these annotations in favour of other transform annotations,
24452449
// so they must be parsed by hand when found in source.
2446-
private static final Class<?>[] DISCARDED_TRANSFORM_ANNOTATIONS = {Canonical.class, Immutable.class};
2450+
private static final Class<?>[] DISCARDED_TRANSFORM_ANNOTATIONS = {Canonical.class, Immutable.class, groovy.transform.Synchronized.class};
24472451

24482452
public List<J.Annotation> visitAndGetAnnotations(AnnotatedNode node, RewriteGroovyClassVisitor classVisitor) {
24492453
if (node.getAnnotations().isEmpty()) {

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,26 @@ void groovyTransformFieldFQNAnnotationOnVariableWithMethodInvocation() {
268268
)
269269
);
270270
}
271+
272+
@Issue("https://github.com/openrewrite/rewrite/issues/6319")
273+
@Test
274+
void synchronizedAnnotation() {
275+
rewriteRun(
276+
groovy(
277+
"""
278+
package org.dummy
279+
280+
import groovy.transform.Synchronized
281+
282+
class Foo {
283+
284+
@Synchronized
285+
void bar() {
286+
println('Hello World')
287+
}
288+
}
289+
"""
290+
)
291+
);
292+
}
271293
}

0 commit comments

Comments
 (0)