codeconsole opened a new pull request, #3201:
URL: https://github.com/apache/groovy/pull/3201
`ClosureMetaClass.getProperty`, `setProperty` and `getMetaProperty` delegate
to a `MetaClassImpl` of `groovy.lang.Closure` itself, which indexes Closure's
own private fields and properties. A closure's metaclass therefore exposed and
overwrote them:
```groovy
def c = { -> 1 }
c.metaClass.getProperty(c, 'bcw') // null instead of
MissingPropertyException
c.metaClass.getProperty(c, 'serialVersionUID') // 4368710879820278874
c.metaClass.getMetaProperty('thisType') // a MetaBeanProperty
c.metaClass.setProperty(c, 'thisType', String) // overwrites the private
field
```
The same path is reached by property access on a closure with the `TO_SELF`
resolve strategy (`c.thisType = String`). Overwriting `thisType` breaks a
closure's access to private fields of its enclosing class when the owner is a
subclass instance. The metaclass of any other subclass reports a superclass's
private fields as missing. Seen in 5.0.8 and 6.0.0 (and the read side in
4.0.33).
`getProperty` and `setProperty` now take the missing-property path for a
name that resolves to a private member of Closure, including the capitalised
form `MetaClassImpl` decapitalises (GROOVY-9618). `getMetaProperty`, and so
`hasProperty`, return null for such names. A sender of `Closure` keeps access,
as a declaring class does for the metaclass of a subclass. Public properties
(`delegate`, `owner`, `thisObject`, `resolveStrategy`, `directive`,
`maximumNumberOfParameters`, `parameterTypes`, `metaClass`, `class`), the
public constants, and property resolution through owner and delegate are
unchanged. Attribute access (`c.@delegate`, `c.@owner`) is left as it is, since
existing code relies on it. This complements #3196 (GROOVY-12675), which
removed the same members from `ClosureMetaClass.getProperties`; the two merge
cleanly.
## Tests
`ClosureTest.testMetaClassDoesNotExposePrivateMembersOfClosure`,
`testMetaClassCannotOverwritePrivateMembersOfClosure` and
`testToSelfDoesNotResolvePrivateMembersOfClosure` fail without the change.
`testMetaClassStillExposesPublicMembersOfClosure` and
`testOwnerAndDelegatePropertiesNamedLikePrivateMembersOfClosure` cover the
public members and owner/delegate resolution. `groovy.lang.*`,
`org.codehaus.groovy.runtime.*`, `groovy.*Closure*`, `bugs.*`,
`gls.invocation.*`, `groovy.transform.stc.*`,
`org.codehaus.groovy.classgen.asm.sc.*` and `groovy.*MetaClass*` pass. The
Checkstyle and CodeNarc gates pass.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]