English 中文(简体)
Groovy MetaClass - Add category methods to appropriate metaClasses
原标题:

I have several categories that I use in my Grails plugin. e.g.,

class Foo {
    static foo(ClassA a,Object someArg) { ... }
    static bar(ClassB b,Object... someArgs) { ... }
}

I m looking for the best way to add these methods to the meta-classes so that I don t have to use the category classes and can just invoke them as instance methods. e.g.,

aInstance.foo(someArg)

bInstance.bar(someArgs)

Is there a Groovy/Grails class or method that will help me do this or am I stuck iterating through the methods and adding them all myself?

最佳回答

In Groovy 1.6 a much simpler mechanism for using categories/mixins was introduced. Previously the methods of a category class had to be declared static, and the first parameter indicates which class of objects they could be applied to (as in your Foo class above).

I find this somewhat awkward because once the methods of the category are "mixed in" to the target class they are non-static, but in the category class they are static.

Anyway, since Groovy 1.6 you can do this instead

// Define the category
class MyCategory {
  void doIt() {
    println "done"
  }

  void doIt2() {
    println "done2"
  }
}

// Mix the category into the target class
@Mixin (MyCategory)
class MyClass {
   void callMixin() {
     doIt()
   }
}

// Test that it works
def obj = new MyClass()
obj.callMixin()

A few other features are available. If you want to restrict the classes that the category can be applied to, use the @Category annotation. For example, if you only want to apply MyCategory to MyClass (or it s subclasses), define it as:

@Category(MyClass)
class MyCategory {
  // Implementation omitted
}

Instead of mixing the categories in at compile-time using @Mixin (as above), you can mix them in at runtime instead using:

MyClass.mixin MyCategory

In you re using Grails, Bootstrap.groovy is a place where you might do this.

问题回答

暂无回答




相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

热门标签