0

Using Groovy 2.0

Just as background I am rolling Yaml with SnakeYaml into Groovy classes. SnakeYaml uses Java reflection to construct and invoke the setting of properties. I want to with certain classes (that are created from Yaml) to allow for the addition of simple properties that do not exist. Thought first about using Groovy get/setProperty on the class definition and dumping the contents into a map store. But since SnakeYaml uses reflection this don't work. Tried to override the utility responsible for setting properties:

def originalMethod = org.yaml.snakeyaml.introspector.PropertyUtils.metaClass.getMetaMethod("getProperty", Class, String)
org.yaml.snakeyaml.introspector.PropertyUtils.metaClass.getProperty = { Class type, String name ->
  // do stuff, like create the property on the metaClass
  def result = originalMethod.invoke(delegate, type, name)
  result
}

But the overridden "getProperty" method never gets called. Ideas? Is it better to use a proxy interceptor? The footprint of the "getProperty" method is:

public Property getProperty(Class<? extends Object> type, String name)
        throws IntrospectionException {
    return getProperty(type, name, beanAccess);
}
Matthew Campbell
  • 1,864
  • 3
  • 24
  • 51
  • 2
    If `PropertyUtils.getProperty` is being called from a Java class, it will have no knowledge of the metaClass declared on the Groovy side – tim_yates Jul 03 '12 at 08:50
  • Soon when pages like http://radomirml.com/2009/03/23/intercepting-method-call-in-groovy-and-invoking-the-original-method say "add or replace any Java method" they mean called from Groovy not Java? – Matthew Campbell Jul 03 '12 at 09:51
  • Found http://stackoverflow.com/questions/1226821/how-can-i-intercept-execution-of-all-the-methods-in-a-java-application-using-gro which talks about the Groovy dispatcher. So weaving is my only option? – Matthew Campbell Jul 03 '12 at 09:53
  • Yes. If you're calling that method from groovy, it will check the metaClass. But if that class is being called from java, it will have no knowledge of the metaclass, so will not find the overridden method – tim_yates Jul 03 '12 at 09:54
  • (my previous comment was to your 'add or replace any Java method' comment) – tim_yates Jul 03 '12 at 10:02
  • Do you have a small complete example of what you're trying to do? Maybe we can work out a workaround? – tim_yates Jul 03 '12 at 10:03
  • @Tim any tips on lightweight AOP from Groovy like https://github.com/chanwit/groovy-aop or something else (played with Groovy for about 3 days now, maybe something latent in the framework?)? – Matthew Campbell Jul 03 '12 at 10:06
  • It might be possible without AOP, if you have a brief complete example? – tim_yates Jul 03 '12 at 10:47
  • @Tim my only example is the YAML. The PropertyUtils is used deep down in the YAML parsing (from SnakeYaml http://code.google.com/p/snakeyaml/wiki/Documentation). Nothing I have a handle to thus the thought about AOP. Let me know if I an explain myself more. – Matthew Campbell Jul 03 '12 at 12:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13353/discussion-between-matthew-young-and-tim-yates) – Matthew Campbell Jul 03 '12 at 12:23

0 Answers0