« Return to Thread: Best way to do AOP-like stunts?
static void main(String[] arguments){
def pointcutMethods = Example.metaClass.methods.findAll{it.name.startsWith 'show'}
def advise = { MetaMethod oldMethod, delegate1, Object... args ->
}
Spoke too soon -- you end up hitting the same catch I was hitting before, just in a different way
than I was.
Here's the catch: your code has lots of explicit definitions.
Your code:
def oldPlus = Integer.metaClass.getMetaMethod("plus", [Integer] as Class[])
Integer.metaClass.plus = { Integer n ->
return oldPlus.invoke( Math.abs(delegate), Math.abs(n) )
}
My code:
def oldMethods = Foo.metaClass.methods.find { ... }
oldMethods.each { MetaMethod oldMeth ->
Foo.metaClass.???? = { ?????
That's where it falls down -- how do I specify the new method declaration?
~~ Robert.
Robert Fischer wrote:Perfection itself.~~ Robert.Ted Naleid wrote:It's hard to know without more details, but this approach might work foryou:http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/It doesn't whack invokeMethod and simply uses the reference to the oldmethod (whatever that may be).-TedOn Mon, Jun 22, 2009 at 12:15 PM, Robert Fischer<robert.fischer@...<robert.fischer@...>> wrote:I'd like to wrap all calls into a particular library that contain aparticular type of argument, andthen do some adjustment to that argument before it continuesprocessing. I specifically do *not*want to override/define invokeMethod, because I suspect someone elsemight be hijacking that fortheir own nefarious purposes, and I'm concerned I'll trounce theirwork (or vice versa). What's thebest way to go about this? If I iterate over the MetaMethods, I'mnot entirely sure how to redefinethe calls so that my new implementation can call the oldimplementation with the appropriatelymangled argument. Is there an idiom I should be using for this kindof behavior?What I really want is something like Java 7's method handles...~~ Robert Fischer, Smokejumper IT Consulting.Enfranchised Mind Blog http://EnfranchisedMind.com/blogCheck out my book, "Grails Persistence with GORM and GSQL"!http://www.smokejumperit.com/redirect.html
--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog
Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html
« Return to Thread: Best way to do AOP-like stunts?
| Free embeddable forum powered by Nabble | Forum Help |