开发者

Inspect Groovy object properties with Java reflection

开发者 https://www.devze.com 2022-12-17 12:30 出处:网络
I have an Expando class which I need to inspect its properties from Java. In Groovy: def worker = new Expando()

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).

0

精彩评论

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

关注公众号