【发布时间】:2015-11-28 04:52:48
【问题描述】:
我有以下类来扩展 Object 类,但是这里添加的方法不能被任何 Object 的子类访问。是否有解决方法或 Groovy 元编程限制/限制?
package groovy.runtime.metaclass.java.lang;
class ObjectMetaClass extends DelegatingMetaClass {
static {
Object.metaClass.enableGlobally();
}
ObjectMetaClass(MetaClass meta) {
super(meta);
meta.enableGlobally();
}
Object invokeMethod(Object object,
String method,
Object[] arguments) {
if (method == 'bar') {
bar(*arguments);
} else {
super.invokeMethod object, method, arguments
}
}
Object bar(config, app) {
println("... ObjectMetaClass.bar(${config}, ${app})");
}
}
在测试脚本中:
o = new Object();
o.bar("profile.properties", "meta-app"); // works
1.bar("profile.properties", "integer-app"); // does not works
"aString".bar("profile.properties", "string-app"); // does not works
【问题讨论】:
标签: inheritance groovy metaprogramming