Is it possible to add GroovyObject methods via a category?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Is it possible to add GroovyObject methods via a category?

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.

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
Reply | Threaded
Open this post in threaded view
|

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

Jochen Theodorou


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
Reply | Threaded
Open this post in threaded view
|

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

Ruben Laguna
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

Ruben Laguna
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 <[hidden email]> 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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

Jochen Theodorou
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
Reply | Threaded
Open this post in threaded view
|

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

Ruben Laguna
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

Jochen Theodorou
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
Reply | Threaded
Open this post in threaded view
|

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

Ruben Laguna
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

paulk_asert
3.0 is still some time away from being feature complete.

On Mon, Dec 11, 2017 at 6:26 PM, Ruben Laguna <[hidden email]> 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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

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

Jochen Theodorou
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 <[hidden email]
> <mailto:[hidden email]>> 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 <[hidden email]
>     <mailto:[hidden email]>> 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
>
>