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)
精彩评论