[ 
https://issues.apache.org/jira/browse/GROOVY-5358?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17476670#comment-17476670
 ] 

Eric Milles edited comment on GROOVY-5358 at 1/15/22, 6:48 PM:
---------------------------------------------------------------

The category mixin case isn't working because the check for GroovyObject 
methods in MetaClassImpl misses the kind of meta method:
{code:java}
    protected final void checkIfGroovyObjectMethod(MetaMethod metaMethod) {
        if (metaMethod instanceof ClosureMetaMethod || metaMethod instanceof 
MixinInstanceMetaMethod) { // NewInstanceMetaMethod
            if (isGetPropertyMethod(metaMethod)) {
                getPropertyMethod = metaMethod;
            } else if (isInvokeMethod(metaMethod)) {
                invokeMethodMethod = metaMethod;
            } else if (isSetPropertyMethod(metaMethod)) {
                setPropertyMethod = metaMethod;
            }
        }
    }
{code}

https://github.com/apache/groovy/pull/1676


was (Author: emilles):
The category mixin case isn't working because the check for GroovyObject 
methods in MetaClassImpl misses the kind of meta method:
{code:java}
    protected final void checkIfGroovyObjectMethod(MetaMethod metaMethod) {
        if (metaMethod instanceof ClosureMetaMethod || metaMethod instanceof 
MixinInstanceMetaMethod) { // NewInstanceMetaMethod
            if (isGetPropertyMethod(metaMethod)) {
                getPropertyMethod = metaMethod;
            } else if (isInvokeMethod(metaMethod)) {
                invokeMethodMethod = metaMethod;
            } else if (isSetPropertyMethod(metaMethod)) {
                setPropertyMethod = metaMethod;
            }
        }
    }
{code}

> Different ways of extending a class functionality esp. adding getProperty 
> inconsistent
> --------------------------------------------------------------------------------------
>
>                 Key: GROOVY-5358
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5358
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>            Reporter: OC
>            Assignee: Eric Milles
>            Priority: Minor
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> When getProperty is used e.g., to mimic a Map behaviour, the result differs 
> depending on whether getProperty was defined directly in the class, added 
> through a mixin, or through a metaclass.
> {code}
> class FooWorksAsMap { // class names the same length as LinkedHashMap to make 
> output pretty
>  def getProperty(String foo) { "OK:FooWorksAsMap.$foo" }
> }
> class BarWorksAsMap {}
> class BaxWorksAsMap {}
> @Category(BarWorksAsMap) class C {
>  def getProperty(String foo) { "OK:BarWorksAsMap.$foo" }
> }
> BarWorksAsMap.mixin C
> BaxWorksAsMap.metaClass.getProperty = { foo -> "OK:BaxWorksAsMap.$foo" }
> def maps = [new FooWorksAsMap(), new BarWorksAsMap(), new BaxWorksAsMap(),
>            [foo:'OK:LinkedHashMap.foo', class:'OK:LinkedHashMap.class']]
> for (def prop in ['foo','class']) {
>  for (def m in maps) {
>    def op = "${m.getClass().getSimpleName()}.$prop"
>    try { println "$op -> " + m."$prop" }
>    catch (t) { println "$op -> FAIL:$t" }
>  }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to