Scott Murphy Heiberg created GROOVY-12658:
---------------------------------------------
Summary: Packed closures read and write owner properties as
fields, skipping getters and setters
Key: GROOVY-12658
URL: https://issues.apache.org/jira/browse/GROOVY-12658
Project: Groovy
Issue Type: Bug
Affects Versions: 6.0.0, 7.0.0-alpha-1
Reporter: Scott Murphy Heiberg
A closure packed by closure packing (GEP-27, via {{@PackedClosures}} or
{{-Dgroovy.target.closure.pack=true}}) has its body moved into a method of the
owner class. There, a property of the owner compiles to direct field access, so
the getter and setter are no longer called. The closure class it replaces calls
them.
{code:groovy}
@groovy.transform.CompileStatic // or @groovy.transform.PackedClosures without
@CompileStatic
@groovy.transform.PackedClosures
class Box {
int gets, sets
BigDecimal e = 1
BigDecimal getE() { gets++; e }
void setE(BigDecimal v) { sets++; e = v }
def m() { def r = { -> e++ }(); [r, this.@e, gets, sets] }
}
println new Box().m()
{code}
*Actual:* {{[1, 2, 0, 0]}}
*Expected:* {{[1, 2, 1, 1]}}, as without packing.
The same happens for reads, assignments, compound assignments, prefix and
postfix forms, and both {{e}} and {{this.e}}. It also affects a subclass
override of a generated accessor, static properties, {{is}} getters, and
(dynamic) an owner that overrides {{getProperty}}.
{{AsmClassGenerator.visitPropertyExpression}} resolves {{this.name}} to the
declared field in any method of the class; the hoisted body is such a method,
so it bypasses the accessors.
Verified on 6.0.0 and master.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)