如何获取Groovy类的所有属性名称?

标题问了一下:如何获取Groovy类的所有属性名称?

它甚至可能吗? 我以为我可以使用类的集合语法,因为它似乎不起作用。

我正在使用groovy编译器2.4我得到一个java.util.LinkedHashMap其中包含通过在groovy对象上调用getProperties()返回的所有属性及其值。

 class PropertyDemoClass { int firstProperty = 1; String secondProperty = "rhubarb" String thirdProperty = "custard" } PropertyDemoClass demoClass = new PropertyDemoClass() println demoClass.getProperties().toString() 

这导致:

 [firstProperty:1, secondProperty:rhubarb, class:class PropertyDemoClass, thirdProperty:custard] 

看看MetaClass API 。

你可以使用Java Reflection API吗?

看一下java.lang.Class的getDeclaredFields 。

关于使用Reflection API的某种教程/指南 。