开发者

How to get all property names of a Groovy class? [duplicate]

This question already has answers here: Groovy property iteration (2 answers) Closed 6 years ago.

The title ask it all : How to get all property na开发者_开发技巧mes of a Groovy class?

Is it even possible? I thought I could use collection syntaxes with classes too be it don't seem to work.


I am using groovy compiler 2.4 I get a java.util.LinkedHashMap containing all the properties and their values returned by calling getProperties() on a groovy object.

class PropertyDemoClass {
    int firstProperty = 1;
    String secondProperty = "rhubarb"
    String thirdProperty = "custard"
}

PropertyDemoClass demoClass = new PropertyDemoClass()
println demoClass.getProperties().toString()

which results in:

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


Take a look at the MetaClass API.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜