Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.0-JSR-3
-
Fix Version/s: 1.5.2
-
Component/s: None
-
Labels:None
-
Environment:Groovy jsr-03, java version "1.5.0_05", winXP
-
Number of attachments :
Description
Try this:
class Foo {
private _xxx
public void setBar(b) {
println "before setting"
_xxx = b
println "after setting"
}
}
def x = new Foo()
x.bar = 1
x.bar = 2
When run, the output is:
C:\Temp>groovy settertest.groovy
before setting
after setting
before setting
after setting
Caught: groovy.lang.GroovyRuntimeException: Cannot read property: bar
even though the property bar was never read. If you add e.g.
println "goodbye"
as the last line of the script, the exception does not occur.
Issue Links
| This issue duplicates: | ||||
| GROOVY-2244 | Write-only JavaBeans properties cannot be set via the '=' operator |
|
|
|
this is a well known issue.
imagine:
def foo() {
x
}
x is returned here
your example is put into a method by groovy. This means x.bar=2 is transformed into:
x.bar=2
return x.bar
so it's no wonder that you will get this error message