Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.7.0
-
Fix Version/s: 1.7.2, 1.8-beta-1
-
Component/s: None
-
Labels:None
-
Testcase included:yes
-
Number of attachments :
Description
I cannot compile a class with @Immutable if it has a static field declared with def. I don't think the transform should touch static fields.
~$ cat ImmutableTest.groovy
@Immutable
class ImmutableTest {
static foo = {}
}
marcus@better:~$ groovyc ImmutableTest.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during canonicalization: @Immutable processor doesn't know how to handle field 'foo' of type 'java.lang.Object or def' while compiling class ImmutableTest.
@Immutable classes currently only support properties with known immutable types or types where special handling achieves immutable behavior, including:
- Strings, primitive types, wrapper types, BigInteger and BigDecimal
- enums, other @Immutable classes and known immutables (java.awt.Color)
- Cloneable classes, collections, maps and arrays, and other classes with special handling (java.util.Date)
Other restrictions apply, please see the groovydoc for @Immutable for further details
java.lang.RuntimeException: @Immutable processor doesn't know how to handle field 'foo' of type 'java.lang.Object or def' while compiling class ImmutableTest.
@Immutable classes currently only support properties with known immutable types or types where special handling achieves immutable behavior, including:
- Strings, primitive types, wrapper types, BigInteger and BigDecimal
- enums, other @Immutable classes and known immutables (java.awt.Color)
- Cloneable classes, collections, maps and arrays, and other classes with special handling (java.util.Date)
Other restrictions apply, please see the groovydoc for @Immutable for further details
at org.codehaus.groovy.transform.ImmutableASTTransformation.createConstructorStatement(ImmutableASTTransformation.java:373)
at org.codehaus.groovy.transform.ImmutableASTTransformation.createConstructorMap(ImmutableASTTransformation.java:324)
at org.codehaus.groovy.transform.ImmutableASTTransformation.createConstructor(ImmutableASTTransformation.java:310)
at org.codehaus.groovy.transform.ImmutableASTTransformation.visit(ImmutableASTTransformation.java:113)
at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:129)
at org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:173)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:957)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:517)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:495)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:472)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:456)
at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:57)
at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:170)
at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompile(FileSystemCompiler.java:138)
at org.codehaus.groovy.tools.FileSystemCompiler.main(FileSystemCompiler.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
1 error
It's not really related to field being static. Even untyped instance level fields are rejected:
@Immutable class ImmutableTest { def x = "" }@Immutable's documentation says "Fields that are enums or other @Immutable classes are allowed but for an otherwise possible mutable property type, an error is thrown.". So the behavior, to me, seems in line with the documentation.
@Immutable class ImmutableTest { def x = "" }