You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by frenchy48 <ba...@in2p3.fr> on 2016/04/13 10:04:23 UTC

aliasing methods

Hello all
I have a resource file that describes alias names for method (something with
entries like: "org.smurf.MyClass#myMethod=otherName")  

now an initialisation code will read this resource to alias "myMethod" to
"otherName"  

there are numerous problems:  
- when doing this aliasing I do not have instances (just the class)
- there may be methods that are overloaded
- and methods with arguments or no argument

code such as:

is not completely ok (if it is null I won't make the difference between a
no-arg method and a method with just one arg which happens to be null)
is there a more elegant expression to do that?
thanks



-----
member of Grumpy Old Programmers
--
View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: aliasing methods

Posted by frenchy48 <ba...@in2p3.fr>.
the solution I implemented: redefining "methodmissing" in the classes
described by the resource
and pointing to a static general method that handles the redefined methods:
-----------------------------------------------------------
clazz.metaClass."methodMissing" = { name, args -> missMethod(delegate, name,
args) }
----------------------------------------------------------
the static missMethod picks up the translation of the method and invokes the
original method.

now still a question: is using systematically "methodMissing" a significant
performance penalty over adding a new method to the metaClass.



-----
member of Grumpy Old Programmers
--
View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316p5732339.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: aliasing methods

Posted by frenchy48 <ba...@in2p3.fr>.
by the way doing this by changing the .properties file description with such
a protocol
-----------------------------
#properties file
java.awt.geom.Area#isEmpty() = estVide
 java.awt.geom.Area#contains = contient
-----------------------------
now this code does not work! 
the key name is parsed into className and methodName
clazz is the Class corresponding to methodName
------------------------------------------------------------------------
 if(methodName.endsWith("()")) {
                    methodName = methodName.replace("()","")
                    clazz.metaClass."$newMethodName" = { 
delegate."$methodName"() }
   } else {
                    clazz.metaClass."$newMethodName" = { it== null?
delegate."$methodName"(null)
                            :delegate."$methodName"(*it)}
    }
------------------------------------------------------------------
it does not work because if you invoke the translation of "contains" with
two double values
these are transformed in Doubles and the invocation fails!

so there should be a better idea ...



-----
member of Grumpy Old Programmers
--
View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316p5732338.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: aliasing methods

Posted by frenchy48 <ba...@in2p3.fr>.
missing the code parts ? sorry ....
ok I try again

----------------------------------------------------
// this part read from a resource so simplifying
String className = 'java.awt.Canvas'
String originalMethodName = 'repaint'
String newMethName = 'repeindre'
Class clazz = Class.forName(className)
// now I want Canvas#repaint aliased as 'repeindre'
clazz.metaClass."$newMethName" = { it== null? delegate."$originalMethName"()
                                                                   
:delegate."$originalMethName"(*it)}
-------------------------------------------------------------------------

now the aliasing expression does not work properly
is there something simple or do I have to explore all the different cases
for the method argument?

for example this does not work:
-----------------------------------------------------
clazz.metaClass."$newMethName"  = clazz.metaClass.&"$originalMethName" 
------------------------------------------------------

thanks





-----
member of Grumpy Old Programmers
--
View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316p5732335.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: aliasing methods

Posted by Jochen Theodorou <bl...@gmx.org>.
You posts are missing the code parts

On 13.04.2016 10:39, frenchy48 wrote:
> a code example?
> well almost everything is in the initial post
> I read the resource I get the name of a Class, the name of a method and the
> name for the alias for this method
> the code will add the alias to the metaclass of the named class (suppose
> this class to be "java.awt.Frame")
> later user of the library will just code using
>
> so now my problem is just to precisely get the best code to implement this
> aliasing.
>
> the code I have  shown in the initial post works but is prone to bugs (when
> there is a real null arg to the method invocation)
>
>
>
> -----
> member of Grumpy Old Programmers
> --
> View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316p5732318.html
> Sent from the Groovy Users mailing list archive at Nabble.com.
>

Re: aliasing methods

Posted by frenchy48 <ba...@in2p3.fr>.
a code example?
well almost everything is in the initial post
I read the resource I get the name of a Class, the name of a method and the
name for the alias for this method
the code will add the alias to the metaclass of the named class (suppose
this class to be "java.awt.Frame")
later user of the library will just code using

so now my problem is just to precisely get the best code to implement this
aliasing.

the code I have  shown in the initial post works but is prone to bugs (when
there is a real null arg to the method invocation)



-----
member of Grumpy Old Programmers
--
View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316p5732318.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: aliasing methods

Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
Hi,

Can you come with some specific code examples? 

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: frenchy48 <ba...@in2p3.fr>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: April 13, 2016 at 10:04:28
To: users@groovy.incubator.apache.org <us...@groovy.incubator.apache.org>
Subject:  aliasing methods  

Hello all  
I have a resource file that describes alias names for method (something with  
entries like: "org.smurf.MyClass#myMethod=otherName")  

now an initialisation code will read this resource to alias "myMethod" to  
"otherName"  

there are numerous problems:  
- when doing this aliasing I do not have instances (just the class)  
- there may be methods that are overloaded  
- and methods with arguments or no argument  

code such as:  

is not completely ok (if it is null I won't make the difference between a  
no-arg method and a method with just one arg which happens to be null)  
is there a more elegant expression to do that?  
thanks  



-----  
member of Grumpy Old Programmers  
--  
View this message in context: http://groovy.329449.n5.nabble.com/aliasing-methods-tp5732316.html  
Sent from the Groovy Users mailing list archive at Nabble.com.