groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ruben Laguna <ruben.lag...@gmail.com>
Subject Re: Is it possible to add GroovyObject methods via a category?
Date Thu, 07 Dec 2017 12:44:29 GMT
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 <ruben.laguna@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 <blackdrag@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

Mime
View raw message