18 Jul 17:34
[groovy-user] Category methods: where do they live at runtime?
From: JRM <oodev@...>
Subject: [groovy-user] Category methods: where do they live at runtime?
Newsgroups: gmane.comp.lang.groovy.user
Date: 2008-07-18 15:37:19 GMT
Subject: [groovy-user] Category methods: where do they live at runtime?
Newsgroups: gmane.comp.lang.groovy.user
Date: 2008-07-18 15:37:19 GMT
I've searched the list and groovy docs for an answer to this, but I've been unable to find an answer so far. If it's been covered already, my apologies. Is it possible (under Groovy 1.5.4) from within the scope of a category usage to get a reference at runtime to the methods that are added from the category? I can't seem to figure out where references to those methods are stored. They aren't available in the MetaClass or the binding. Where do they come from? It's all so magical.Here is a script that demonstrates trying to find them in the script's class methods and metaClass methods: public class MyMagicCategory { static def whereDoILive( Script self ) { println "Hello. I'm right here." } } use (MyMagicCategory) { whereDoILive() def foundInMethods = this.class.methods.find{ it.name == "doSomething" } != null def foundInMetaMethods = this.metaClass.metaMethods.find{ it.name == "doSomething" } != null assert foundInMethods || foundInMetaMethods } Output: Hello. I'm right here. Exception thrown: java.lang.AssertionError: Expression: (foundInMethods || foundInMetaMethods). Values: foundInMethods = false, foundInMetaMethods = false java.lang.AssertionError: Expression: (foundInMethods || foundInMetaMethods). Values: foundInMethods = false, foundInMetaMethods = false at Script15$_run_closure1.doCall(Script15:14) at Script15$_run_closure1.doCall(Script15) at Script15.run(Script15:9) Thanks for the help! -- Jason --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
Here is a script that demonstrates trying to find them in the script's
class methods and metaClass methods:
public class MyMagicCategory {
static def whereDoILive( Script self ) {
println "Hello. I'm right here."
}
}
use (MyMagicCategory) {
whereDoILive()
def foundInMethods = this.class.methods.find{ it.name ==
"doSomething" } != null
def foundInMetaMethods = this.metaClass.metaMethods.find{ it.name
== "doSomething" } != null
assert foundInMethods || foundInMetaMethods
}
Output:
Hello. I'm right here.
Exception thrown: java.lang.AssertionError: Expression:
(foundInMethods || foundInMetaMethods). Values: foundInMethods =
false, foundInMetaMethods = false
java.lang.AssertionError: Expression: (foundInMethods ||
foundInMetaMethods). Values: foundInMethods = false,
foundInMetaMethods = false
at Script15$_run_closure1.doCall(Script15:14)
at Script15$_run_closure1.doCall(Script15)
at Script15.run(Script15:9)
Thanks for the help!
--
Jason
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
RSS Feed