如何在groovy中列出JVM中的所有(groovy)类
我正在编写一个 DelegatingMetaClass,我想将其应用于项目中的所有 groovy 类,但我不知道如何获取项目中的所有类?
这是代码:
/*
This will work ok, since I know Foo beforehand, but what about classes
that do not exist yet?
*/
def myMetaClass = new DelegatingMetaClass(Foo.class)
InvokerHelper.metaRegistry.setMetaClass(Foo.class, myMetaClass)
/*
how to do this?
allGroovyClasses.each{
def myMetaClass = new DelegatingMetaClass(it)
InvokerHelper.metaRegistry.setMetaClass(it, myMetaClass)
}
*/
class SimpleInterceptor extends DelegatingMetaClass{
public SimpleInterceptor(final Class aclass) {
super(aclass);
initialize();
}
public Object getProperty(Object object, String prop) {
println ("I am in a property interceptor!!!")
return super.getProperty(object, prop)
}
public Object invokeMethod(Object a_object, String a_methodName, Object[] a_arguments)
{
println ("I am in a method interceptor!!!")
return super.invokeMethod(a_object, a_methodName, a_arguments)
}
I am writing a DelegatingMetaClass that I would like to apply to all groovy classes in my project, but I do not how to get hold of all classes in the project?
Here is the code:
/*
This will work ok, since I know Foo beforehand, but what about classes
that do not exist yet?
*/
def myMetaClass = new DelegatingMetaClass(Foo.class)
InvokerHelper.metaRegistry.setMetaClass(Foo.class, myMetaClass)
/*
how to do this?
allGroovyClasses.each{
def myMetaClass = new DelegatingMetaClass(it)
InvokerHelper.metaRegistry.setMetaClass(it, myMetaClass)
}
*/
class SimpleInterceptor extends DelegatingMetaClass{
public SimpleInterceptor(final Class aclass) {
super(aclass);
initialize();
}
public Object getProperty(Object object, String prop) {
println ("I am in a property interceptor!!!")
return super.getProperty(object, prop)
}
public Object invokeMethod(Object a_object, String a_methodName, Object[] a_arguments)
{
println ("I am in a method interceptor!!!")
return super.invokeMethod(a_object, a_methodName, a_arguments)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个 如何在 java 中执行此操作的示例,它也应该与 groovy 一起工作。我认为这是一种粗略的方法。
您需要在域类中拦截 getter/setter 吗? Hibernate对此有支持(我假设GORM 也是)。
您需要拦截控制器操作吗?您可以使用控制器拦截器。
你的目标是什么?
There's an example of how to do this in java, which should also work with groovy. I think it's a sketchy way to do it though.
Do you need to intercept getters/setters in domain classes? Hibernate has support for that (I assume GORM too).
Do you need to intercept controller actions? You can use controller interceptors.
What's you goal?