[
https://issues.apache.org/jira/browse/GROOVY-3056?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152668#comment-18152668
]
ASF GitHub Bot commented on GROOVY-3056:
----------------------------------------
paulk-asert commented on PR #3346:
URL: https://github.com/apache/groovy/pull/3346#issuecomment-7267635859
Thanks for this. The rule looks right to me: accepting a suffix exactly when
`BeanUtils.capitalize` leaves it unchanged matches how Groovy forms accessor
names. Over all BMP identifier characters, I found no suffix accepted by the
old rule that the new one rejects. It also brings EMC closures into line with
real and mixed-in methods, where e.g. `get_r()` is already a property. On that
basis I'd keep `$` and digit-leading names too, even though the JIRA only
mentions `_`.
My main concern is the "compatible widening … not a Groovy 7-only break"
claim. Once such an accessor is registered, there are visible changes. I ran
the same script with the current and the PR versions of `ExpandoMetaClass`:
| Scenario | Before | With this PR |
|---|---|---|
| `A.metaClass.get_secret = { -> 'S' }`, then `JsonOutput.toJson(new A())` |
`{"n":"x"}` | `{"n":"x","_secret":"S"}` (also in `.properties`) |
| `get_boom` throws, then `toJson` | works | throws |
| `new U(u.properties.findAll { it.key != 'class' })` with `get_k`
registered | works | `ReadOnlyPropertyException` |
| `set_c` only, over `private _c = 'priv'`, then `toJson` | `{}` |
`{"_c":"priv"}` |
| `get_c` over `private _c`, class reads `_c` inside a closure | field value
| `get_c()` result |
| `set_v` over `private _v`, class writes `_v` inside a closure | writes the
field | calls `set_v` |
| `propertyMissing` plus `get_x`, reading `._x` | `propertyMissing` |
`get_x()` |
I checked several of these against their `getC`/`setC` equivalents, and they
match existing behaviour for conventionally named properties, so I think the
new behaviour is right. But `_name` is a common backing-field convention, and
`COMPATIBILITY.md` lists MetaClass dispatch and serialization formats among the
behaviours whose change counts as breaking. I'd treat this as 7.0 only, with no
backport to 6.0.x.
Suggested changes:
1. **Description:** replace the "compatible widening" paragraph with the
actual impact.
2. **`COMPATIBILITY.md`:** add a Groovy 7 entry covering
`getProperties()`/JSON visibility, private backing fields accessed from
closures, and `propertyMissing`.
3. **Tests:** please pin
- the `getProperties()`/JSON result after registering e.g. `get_secret`;
- a private `_field` read and written inside a closure of its own class;
- a lowercase letter with no single-char uppercase form, e.g. `getßx`.
The `capitalize` comparison handles this correctly, whereas a later
"simplification" to `!Character.isLowerCase(..)` would break it.
4. **Javadoc:**
- `isPropertyName` gives two cases where capitalization is a no-op. The
third is a lowercase letter with no single-char uppercase form, such as `ß`.
- I'd trim the rest, as it is a lot of text for a one-line change. In
particular, I'd drop the `StringIndexOutOfBoundsException` notes on
`getPropertyForGetter`/`getPropertyForSetter`: they turn an implementation
accident into a documented contract, and the latter is public API.
5. **Nits in `Groovy3056`:**
- `import groovy.lang.ExpandoMetaClass` is redundant.
- The `getOther <<` assertion in
`wrongArityUnderscoreMethodsAreNotAccessors` isn't about arity or underscores.
> Cannot override getter of property starting with "_" using ExpandoMetaClass
> ---------------------------------------------------------------------------
>
> Key: GROOVY-3056
> URL: https://issues.apache.org/jira/browse/GROOVY-3056
> Project: Groovy
> Issue Type: Bug
> Components: groovy-runtime
> Affects Versions: 1.5.6
> Reporter: Peter Ledbrook
> Assignee: Daniel Sun
> Priority: Major
> Attachments: ExpandoMetaClassUnderscoreTests.groovy
>
>
> For most properties you can use code like this to override property read
> access:
> {code}
> MyClass.metaClass.getMyProp = {-> return "New value" }
> {code}
> Groovy's {{getProperty()}} method will then call the above code so objects of
> class {{MyClass}} will appear to have the new value for that property.
> However, if the property starts with an underscore, this behaviour breaks
> down:
> {code}
> MyClass.metaClass.get_ajax = {-> return "Some value" }
> {code}
> The above method will not be called if one uses {{getProperty("_ajax")}}.
> Test case attached.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)