Hi,
The changes you are introducing (when you do "obj.metaClass.getA = {..}") are not becoming part of the class Object but of the metaClass for that particular object.
Since BeanUtils works off the class data and not groovy metaclass data, accessing dynamically attached properties through BeanUtils will not work.
I don't think it will anyhow work out for object level metaClass modification, but if you are modifying the metaClass at class level (say, you do "Object.metaClass.getA = {...}") then it may work out if your bean also explicitly exposes bean descriptor data.
rgds,
Roshan
On Tue, Sep 7, 2010 at 12:39 PM, Kenny Cheang
<kch...@gmail.com> wrote:
Hi,
I am trying to inject a getter to one of my domain objects dynamically and later use BeanUtils to retrieve the value using BeanUtils.getProperty. Below is the code snippet.
def obj = new Object()
obj.metaClass.getA = { 'hello' }
println obj.a
println BeanUtils.getProperty(obj, 'a')
The first println statement works correctly but the second println returns an error:
java.lang.NoSuchMethodException: Unknown property 'a' on class 'class java.lang.Object'
Does anyone know how I can work around this issue?
Thanks!
Kenny
--
rgds,
Roshan
http://roshandawrani.wordpress.com/