groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mirco <mirch...@gmail.com>
Subject groovy 3.0.0-beta-1 issue: public fields in closures
Date Sun, 19 May 2019 19:46:11 GMT
Hi,
I'm not sure if this is a known issue, however I've spotted a regression in
the first beta of version 3.0.0
The problem is related to closures. The compiler complains where inside a
closure one try to access a public field without a getter.
It is very simple to reproduce with this script:

"""
import groovy.transform.CompileStatic
@CompileStatic
class Foo { public float field1 = 1; }
@CompileStatic
class Main {
    static public void main(String... args) {
    var cl = {
            Foo foo = new Foo()
            assert foo.field1 == 1
        }
    }
}
Main.main()
"""

The error logged is quite generic:
/path/to/test.groovy: -1: Access to Foo#foo is forbidden @ line -1, column
-1.

It could be passed unnoticed because in "pure" groovy the modifier "public"
is usually omitted to have automatic property getter/setter for free.
However it is more evident when one try to instantiate an object with a
public field from an external java library.
For example a common scenario is when lambdas/closures are used in place of
Runnable objects as in JavaFx:
Platform.runLater( () -> { ... obj.field1 ... } )
In such cases the code doesn't work anymore in beta-1 and the possible
workaround is the old-fashioned pre-Java8 code style
Platform.runLater( new Runnable() { @Override public void run() { ...
obj.field1 ... } } )

Up until alpha-4 everything works correctly so I suspect the bug is
introduced by this commit:
https://github.com/apache/groovy/commit/d8731a7f9ed0f6d9e6c123f371ef0aca1f5a231f#diff-5d9e284e820e6d63ae34f331cb0afb2d

but it is just an educated guess based on timing and not on a real analysis.


Kind regards,

M

Mime
View raw message