History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-1150
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Paul King
Reporter: Antti Karanta
Votes: 1
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
groovy

Strange behavior on property setter

Created: 21/Nov/05 06:05 AM   Updated: 08/Feb/08 01:26 AM
Component/s: None
Affects Version/s: 1.0-JSR-3
Fix Version/s: 1.5.2

Time Tracking:
Not Specified

Environment: Groovy jsr-03, java version "1.5.0_05", winXP
Issue Links:
Duplicate
 


 Description  « Hide
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.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Jochen Theodorou - 21/Nov/05 08:22 AM
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


Peter Niederwieser - 12/May/07 03:52 PM
The transformation "x.bar = 2; return x.bar" is incorrect.¹ Instead it should be "return (x.bar = 2)", which would solve this issue.

¹ It's even more likely to have unwanted side-effects when a method is involved, for example in "x.bar().foo = 2".


Andres Almiray - 27/Nov/07 01:47 PM
FYI this bug is still around.

Paul King - 06/Jan/08 01:48 AM
This was fixed with GROOVY-2244. In fact I think this was a duplcate issue.

Paul King - 08/Feb/08 01:26 AM
close off release 1.5.4