groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mirco <>
Subject groovy 3.0.0-beta-1 issue: public fields in closures
Date Sun, 19 May 2019 19:46:11 GMT
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
class Foo { public float field1 = 1; }
class Main {
    static public void main(String... args) {
    var cl = {
            Foo foo = new Foo()
            assert foo.field1 == 1

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

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:

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

Kind regards,


View raw message