设置元类后 Groovy newInstance() 方法丢失
我定义一个元类
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)
原因是什么?我的理解是我将其他方法委托给超类, newInstance() 方法应该仍然可以调用。
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 String 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为:
应该是:
I think:
Should be: