You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Ruben Laguna <ru...@gmail.com> on 2017/12/07 09:31:56 UTC

Is it possible to add GroovyObject methods via a category?

In the Groovy in Action book, chapter 8.4.5 says "Category method
names can well take the form of property accessors (pretending
property access), operator methods, and GroovyObject methods. MOP hook
methods cannot be added through a category class. This is a
restriction as of Groovy 2.4. The feature may become available in
later versions."

It interpreted this as meaning that I can add getProperty,
getMetaClass, invokeMethod to a class using categories but not
methodMissing or propertyMissing.

But when I tried to add invokeMethod using a category the change has no effect

class MyClass{}

a = new MyClass()


@Category(MyClass)
class MyCategory {
    def missingMethod(String name, def args) { "missingMethod" } //
GINA says no MOP hook method

    def invokeMethod(String name, def args) { "invokeMethod" } // but
GroovyObject method should be fine
    def getProperty(String name) { "missingProperty" }

    def getMyProperty() { "prop1" }
}

use(MyCategory) {
    assert "missingMethod" == a.missingMethod('a', 'b') // methods are the
    assert "invokeMethod" == a.invokeMethod('a', 'b')
    assert "prop1" == a.myProperty


    // but they are not in effect
    // assert "missingMethod" == a.method1() // MissingMethodException
    // assert "invokeMethod" == a.method2()  // MssingMethodException
    // assert "missingProperty" == a.property // MissingPropertyException
}


Is is possible or not?

https://stackoverflow.com/q/47691492/90580


-- 
/Rubén

Re: Is it possible to add GroovyObject methods via a category?

Posted by Ruben Laguna <ru...@gmail.com>.
I finally got time to address the propertyMisssing / methodMissing in
categories. I've submitted a pull request at
https://github.com/apache/groovy/pull/693


-- 
/Rubén

Re: Is it possible to add GroovyObject methods via a category?

Posted by Jochen Theodorou <bl...@gmx.org>.
Most likely we will have to replace the eager meta class init with a 
lazy one to finally get JDK9 stop complaining. In that case we are still 
a long way from 3.0

Am 11.12.2017 um 09:28 schrieb Paul King:
> 3.0 is still some time away from being feature complete.
> 
> On Mon, Dec 11, 2017 at 6:26 PM, Ruben Laguna <ruben.laguna@gmail.com 
> <ma...@gmail.com>> wrote:
> 
>     I see, thank you very much.
> 
>     I'll take a look at how to implement the methodMissing support since
>     I'm really interested on having this at some point in Groovy. Is there
>     any chance to see this in Groovy 3.0 if you receive a satisfactory
>     patch? or is too late for 3.0?
> 
>     Best regards/Rubén
> 
>     On Fri, Dec 8, 2017 at 7:02 PM, Jochen Theodorou <blackdrag@gmx.org
>     <ma...@gmx.org>> wrote:
>      > On 08.12.2017 10:31, Ruben Laguna wrote:
>      >>
>      >> I've found the following *old* bug report from 2009:
>      >>
>      >> [GROOVY-3867 Allow methodMissing/propertyMissing to be defined
>     through
>      >> category |https://issues.apache.org/jira/browse/GROOVY-3867
>     <https://issues.apache.org/jira/browse/GROOVY-3867>]
>      >>
>      >> which says that methodMissing does not work since Groovy 1.6.5.
>      >
>      >
>      > ok, then it is something we never got to fix
>      >
>      >> So I've opened a new JIRA ticket [iGROOVY-8408 invokeMethod
>     cannot be
>      >> set through
>     category|https://issues.apache.org/jira/browse/GROOVY-8408
>     <https://issues.apache.org/jira/browse/GROOVY-8408>]
>      >
>      >
>      > as long as we have the GroovyObject based logic we currently have
>      > invokeMethod cannot receive this kind of treatment.
>      >
>      > bye Jochen
> 
> 
> 
>     --
>     /Rubén
> 
> 

Re: Is it possible to add GroovyObject methods via a category?

Posted by Paul King <pa...@asert.com.au>.
3.0 is still some time away from being feature complete.

On Mon, Dec 11, 2017 at 6:26 PM, Ruben Laguna <ru...@gmail.com>
wrote:

> I see, thank you very much.
>
> I'll take a look at how to implement the methodMissing support since
> I'm really interested on having this at some point in Groovy. Is there
> any chance to see this in Groovy 3.0 if you receive a satisfactory
> patch? or is too late for 3.0?
>
> Best regards/Rubén
>
> On Fri, Dec 8, 2017 at 7:02 PM, Jochen Theodorou <bl...@gmx.org>
> wrote:
> > On 08.12.2017 10:31, Ruben Laguna wrote:
> >>
> >> I've found the following *old* bug report from 2009:
> >>
> >> [GROOVY-3867 Allow methodMissing/propertyMissing to be defined through
> >> category |https://issues.apache.org/jira/browse/GROOVY-3867]
> >>
> >> which says that methodMissing does not work since Groovy 1.6.5.
> >
> >
> > ok, then it is something we never got to fix
> >
> >> So I've opened a new JIRA ticket [iGROOVY-8408 invokeMethod cannot be
> >> set through category|https://issues.apache.org/jira/browse/GROOVY-8408]
> >
> >
> > as long as we have the GroovyObject based logic we currently have
> > invokeMethod cannot receive this kind of treatment.
> >
> > bye Jochen
>
>
>
> --
> /Rubén
>

Re: Is it possible to add GroovyObject methods via a category?

Posted by Ruben Laguna <ru...@gmail.com>.
I see, thank you very much.

I'll take a look at how to implement the methodMissing support since
I'm really interested on having this at some point in Groovy. Is there
any chance to see this in Groovy 3.0 if you receive a satisfactory
patch? or is too late for 3.0?

Best regards/Rubén

On Fri, Dec 8, 2017 at 7:02 PM, Jochen Theodorou <bl...@gmx.org> wrote:
> On 08.12.2017 10:31, Ruben Laguna wrote:
>>
>> I've found the following *old* bug report from 2009:
>>
>> [GROOVY-3867 Allow methodMissing/propertyMissing to be defined through
>> category |https://issues.apache.org/jira/browse/GROOVY-3867]
>>
>> which says that methodMissing does not work since Groovy 1.6.5.
>
>
> ok, then it is something we never got to fix
>
>> So I've opened a new JIRA ticket [iGROOVY-8408 invokeMethod cannot be
>> set through category|https://issues.apache.org/jira/browse/GROOVY-8408]
>
>
> as long as we have the GroovyObject based logic we currently have
> invokeMethod cannot receive this kind of treatment.
>
> bye Jochen



-- 
/Rubén

Re: Is it possible to add GroovyObject methods via a category?

Posted by Jochen Theodorou <bl...@gmx.org>.
On 08.12.2017 10:31, Ruben Laguna wrote:
> I've found the following *old* bug report from 2009:
> 
> [GROOVY-3867 Allow methodMissing/propertyMissing to be defined through
> category |https://issues.apache.org/jira/browse/GROOVY-3867]
> 
> which says that methodMissing does not work since Groovy 1.6.5.

ok, then it is something we never got to fix

> So I've opened a new JIRA ticket [iGROOVY-8408 invokeMethod cannot be
> set through category|https://issues.apache.org/jira/browse/GROOVY-8408]

as long as we have the GroovyObject based logic we currently have 
invokeMethod cannot receive this kind of treatment.

bye Jochen

Re: Is it possible to add GroovyObject methods via a category?

Posted by Ruben Laguna <ru...@gmail.com>.
I've found the following *old* bug report from 2009:

[GROOVY-3867 Allow methodMissing/propertyMissing to be defined through
category |https://issues.apache.org/jira/browse/GROOVY-3867]

which says that methodMissing does not work since Groovy 1.6.5.

So I've opened a new JIRA ticket [iGROOVY-8408 invokeMethod cannot be
set through category|https://issues.apache.org/jira/browse/GROOVY-8408]

Thanks for the help




On Fri, Dec 8, 2017 at 9:02 AM, Jochen Theodorou <bl...@gmx.org> wrote:
> On 07.12.2017 13:44, Ruben Laguna wrote:
>>
>> There was a typo in my previous mail missingMethod -> methodMissing.
>> It still doesn not work, a.method1() throws MissingMethodException
>> after adding the methodMissing via category.
>
>
> yes, just tested myself. if you add the method through the meta class it
> works. It is strange it does not work via category. This is supposed to
> work... and I was really thinking that used to work too. So I am puzzled,
> looks like a bug/regression to me
>
> bye Jochen



-- 
/Rubén

Re: Is it possible to add GroovyObject methods via a category?

Posted by Jochen Theodorou <bl...@gmx.org>.
On 07.12.2017 13:44, Ruben Laguna wrote:
> There was a typo in my previous mail missingMethod -> methodMissing.
> It still doesn not work, a.method1() throws MissingMethodException
> after adding the methodMissing via category.

yes, just tested myself. if you add the method through the meta class it 
works. It is strange it does not work via category. This is supposed to 
work... and I was really thinking that used to work too. So I am 
puzzled, looks like a bug/regression to me

bye Jochen

Re: Is it possible to add GroovyObject methods via a category?

Posted by Ruben Laguna <ru...@gmail.com>.
There was a typo in my previous mail missingMethod -> methodMissing.
It still doesn not work, a.method1() throws MissingMethodException
after adding the methodMissing via category.

class MyClass{

}

a = new MyClass()


@Category(MyClass)
class MyCategory {
    def methodMissing(String name, def args) { "missingMethod" }
    def propertyMissing(String name) { "propertyMissing" }
    def test() { "works" }
}


use(MyCategory) {
    assert "works" == a.test()
    //assert "propertyMissing" == a.property1 // MissingPropertyException
    assert "missingMethod" == a.method1() // MissingMethodException
}

On Thu, Dec 7, 2017 at 1:37 PM, Ruben Laguna <ru...@gmail.com> wrote:
> Well I've also tried with propertyMissing and methodMissing and they
> don't work either:
>
> class MyClass{}
>
> a = new MyClass()
>
>
> @Category(MyClass)
> class MyCategory {
>     def missingMethod(String name, def args) { "missingMethod" }
>     def propertyMissing(String name) { "propertyMissing" }
>     def test() { "works" }
> }
>
>
> use(MyCategory) {
>     assert "works" == a.test()
>     // assert "propertyMissing" == a.property1 // MissingPropertyException
>     assert "missingMethod" == a.method1() // MissingMethodException
> }
>
> The missingMethod is not callled on a.method1() . Am I using the right syntax?
>
> Best regards/Rubén
>
>
>
> On Thu, Dec 7, 2017 at 1:11 PM, Jochen Theodorou <bl...@gmx.org> wrote:
>>
>>
>> Am 07.12.2017 um 10:31 schrieb Ruben Laguna:
>>>
>>> In the Groovy in Action book, chapter 8.4.5 says "Category method
>>> names can well take the form of property accessors (pretending
>>> property access), operator methods, and GroovyObject methods. MOP hook
>>> methods cannot be added through a category class. This is a
>>> restriction as of Groovy 2.4. The feature may become available in
>>> later versions."
>>>
>>> It interpreted this as meaning that I can add getProperty,
>>> getMetaClass, invokeMethod to a class using categories but not
>>> methodMissing or propertyMissing.
>>
>>
>> "MOP hook methods" means here getProperty, getMetaClass, invokeMethod and
>> setProperty. MEaning you cannot override those using a category.
>> methodMissing and propertyMissing should work
>>
>> bye Jochen
>
>
>
> --
> /Rubén



-- 
/Rubén

Re: Is it possible to add GroovyObject methods via a category?

Posted by Ruben Laguna <ru...@gmail.com>.
Well I've also tried with propertyMissing and methodMissing and they
don't work either:

class MyClass{}

a = new MyClass()


@Category(MyClass)
class MyCategory {
    def missingMethod(String name, def args) { "missingMethod" }
    def propertyMissing(String name) { "propertyMissing" }
    def test() { "works" }
}


use(MyCategory) {
    assert "works" == a.test()
    // assert "propertyMissing" == a.property1 // MissingPropertyException
    assert "missingMethod" == a.method1() // MissingMethodException
}

The missingMethod is not callled on a.method1() . Am I using the right syntax?

Best regards/Rubén



On Thu, Dec 7, 2017 at 1:11 PM, Jochen Theodorou <bl...@gmx.org> wrote:
>
>
> Am 07.12.2017 um 10:31 schrieb Ruben Laguna:
>>
>> In the Groovy in Action book, chapter 8.4.5 says "Category method
>> names can well take the form of property accessors (pretending
>> property access), operator methods, and GroovyObject methods. MOP hook
>> methods cannot be added through a category class. This is a
>> restriction as of Groovy 2.4. The feature may become available in
>> later versions."
>>
>> It interpreted this as meaning that I can add getProperty,
>> getMetaClass, invokeMethod to a class using categories but not
>> methodMissing or propertyMissing.
>
>
> "MOP hook methods" means here getProperty, getMetaClass, invokeMethod and
> setProperty. MEaning you cannot override those using a category.
> methodMissing and propertyMissing should work
>
> bye Jochen



-- 
/Rubén

Re: Is it possible to add GroovyObject methods via a category?

Posted by Jochen Theodorou <bl...@gmx.org>.

Am 07.12.2017 um 10:31 schrieb Ruben Laguna:
> In the Groovy in Action book, chapter 8.4.5 says "Category method
> names can well take the form of property accessors (pretending
> property access), operator methods, and GroovyObject methods. MOP hook
> methods cannot be added through a category class. This is a
> restriction as of Groovy 2.4. The feature may become available in
> later versions."
> 
> It interpreted this as meaning that I can add getProperty,
> getMetaClass, invokeMethod to a class using categories but not
> methodMissing or propertyMissing.

"MOP hook methods" means here getProperty, getMetaClass, invokeMethod 
and setProperty. MEaning you cannot override those using a category. 
methodMissing and propertyMissing should work

bye Jochen