开发者

Groovy newInstance() method missing after set metaClass

开发者 https://www.devze.com 2023-04-08 22:40 出处:网络
I define an metaclass class MyMetaClass extends DelegatingMetaClass { MyMetaClass(Class theClass){ super(theClass)

I define an metaclass

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)
    }
  }
}

and class A:

class A {
  private St开发者_如何学Cring a
  String getA(){
    return a
  }
}

and register metaclass:

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

Now, I try create instance using:

A a2 = A.class.newInstance()

I get error:

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)

What's the reason? My understanding is I have delegate other methods to super class, the newInstance() method should still callable.


I think:

  return super.invokeMethod(object, methodName, arguments)

Should be:

  return super.invokeStaticMethod(object, methodName, arguments)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号