开发者

How to list all (groovy) classes in JVM in groovy

开发者 https://www.devze.com 2022-12-28 09:58 出处:网络
I am writing a DelegatingMetaClass that I would like开发者_如何学JAVA to apply to all groovy classes in my project, but I do not how to get hold of all classes in the project?

I am writing a DelegatingMetaClass that I would like开发者_如何学JAVA 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) 
 }


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号