[英]How to list all (groovy) classes in JVM in groovy
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? 我正在编写一个DelegatingMetaClass,我想将其应用于项目中的所有常规类,但是我不怎么掌握项目中的所有类?
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)
}
There's an example of how to do this in java , which should also work with groovy. 有一个关于如何在java中执行此操作的示例,该示例也应与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).
Hibernate也对此提供了支持 (我也假设是GORM)。
Do you need to intercept controller actions? 您需要拦截控制器动作吗? You can use controller interceptors .
您可以使用控制器拦截器 。
What's you goal? 你的目标是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.