I want that my custom annotation must be used in conjunction with Immutable annotation. I achieved this by the following code -
Annotation class -
@Retention(RetentionPolicy.SOURCE)
@Target([ElementType.TYPE])
@GroovyASTTransformationClass(classes = TestFactoryTransform)
@interface TestFactory {
}
Its transformation class -
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
public class TestFactoryTransform implements ASTTransformation {
@Override
public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
ClassNode classNode = (ClassNode) astNodes[1]
if (classNode.getAnnotations(ClassHelper.make(Immutable)).size() == 0) {
// throw ERROR
} else {
...
}
}
I have a test case to verify -
@TestFactory
@Immutable
@AnnotationCollector
@interface SampleTestFactory {}
@SampleTestFactory
class Test1 {
int f1
}
class TestFactoryTransformTest {
@Test
void testIntField() {
...
}
}
this is working fine with groovy 2.
BUT, after upgrading groovy to 3.0.13. This test compilation is failing because if
condition of TestFactoryTransform is true (Immutable annotation not found).
when I print the output of classNode.getAnnotations()
statement then with groovy 2, I am getting -
[
org.codehaus.groovy.ast.AnnotationNode@16c0bc,
org.codehaus.groovy.ast.AnnotationNode@16c0bc,
org.codehaus.groovy.ast.AnnotationNode@16c0bc
]
But with groovy 3, I am getting
[
org.codehaus.groovy.ast.AnnotationNode@390fbe99[@com.my.transform.TestFactory()],
org.codehaus.groovy.ast.AnnotationNode@587db6e0[@groovy.transform.TupleConstructor(defaults: false)],
org.codehaus.groovy.ast.AnnotationNode@5798d0f[@groovy.transform.ToString(includeSuperProperties: true, cache: true)],
org.codehaus.groovy.ast.AnnotationNode@54f967a6[@groovy.transform.EqualsAndHashCode(cache: true)],
org.codehaus.groovy.ast.AnnotationNode@7293bc6b[@groovy.transform.ImmutableBase()],
org.codehaus.groovy.ast.AnnotationNode@735697ed[@groovy.transform.ImmutableOptions()],
org.codehaus.groovy.ast.AnnotationNode@2818eebe[@groovy.transform.PropertyOptions(propertyHandler: groovy.transform.options.ImmutablePropertyHandler)],
org.codehaus.groovy.ast.AnnotationNode@68c5d616[@groovy.transform.MapConstructor(noArg: true, includeSuperProperties: true, includeFields: true)],
org.codehaus.groovy.ast.AnnotationNode@24992745[@groovy.transform.KnownImmutable()]
]
Any idea, why I am getting immutable annotation segregated with its inner annotations? And what I can do to fix the issue. Thank you!
If I'm not mistaken @Immutable
is a singular annotation in Groovy 2.5.x, but it was broken into components with Groovy 3.x, and it is now just a meta annotation based on @AnnotationCollector
as you can see in the GroovyDoc.
I don't think that there is anything to fix, you just need to update your logic to also accept the new component annotations, probably @ImmutableBase
and @KnownImmutable
.