views:

315

answers:

2

I have an Expando class which I need to inspect its properties from Java. In Groovy:

def worker = new Expando()
worker.name = "John"
worker.surname = "Doe"

In Java:

Introspector.getBeanInfo(groovyObject.getClass())

Is it possible to compile at runtime the class from the object in Groovy?

A: 

The Expando is completely dynamic. It does not generate any bytecode getters or setters and therefore cannot be used as a JavaBean. What do you need to use the bean introspector for? You may be able to implement that logic using the expando directly if you write it in Groovy.

Chris Dail
I need it to make bindings over its properties. We're using betterbindings so the BeanInfo returned by the introspection should contain the expando properties. Groovy is used embedded for little scripts, in this case is to load a row (a GroovyRowResult object), but the container of that object it's in Java, the same as the other objects to be bound.
escanda
A: 

You might try the JSR 223 / Script engine with Groovy (example here) if you are using Java 6. It allows you to evaluate Groovy code from Java.

Depending on the location/definition of the Expando, you might be able to get its properties by evaluating getProperties() (as of Groovy 1.7).

Michael Easter
We're using the GroovyClassLoader and then using object as usually in Java, the problem is inspecting bean properties in the Java side to make bindings with betterbindings.
escanda