English 中文(简体)
Groovy newInstance() 方法在设定的计量后缺失
原标题:Groovy newInstance() method missing after set metaClass

I下定义

class MyMetaClass extends DelegatingMetaClass {
  MyMetaClass(Class theClass){
    super(theClass)
    println theClass
  }
  Object invokeStaticMethod(Object object, String methodName, Object[] arguments) {
    if(methodName ==  save ) {
      println  save method 
      return 
    } else {
      return super.invokeMethod(object, methodName, arguments)
    }
  }
}

A类:

class A {
  private String a
  String getA(){
    return a
  }
}

登记元:

def amc = new MyMetaClass(A)
amc.initialize()
InvokerHelper.metaRegistry.setMetaClass(A, amc)

现在,我尝试利用:

A a2 = A.class.newInstance()

我发现错误:

Caught: groovy.lang.MissingMethodException: No signature of method: A.newInstance() is applicable for argument types: () values: []
at MyMetaClass.invokeStaticMethod(MyMetaClass.groovy:37)
at test.run(test.groovy:139)

原因是什么? 我的理解是,我把其他方法下放到超级类别,即<代码>新内容()方法仍然可以采用。

最佳回答

我认为:

  return super.invokeMethod(object, methodName, arguments)

应:

  return super.invokeStaticMethod(object, methodName, arguments)
问题回答

暂无回答




相关问题
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 ...

热门标签