Inspect Groovy object properties with Java reflection
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.g开发者_如何学编程etClass())
Is it possible to compile at runtime the class from the object in Groovy?
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.
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).
精彩评论