Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The title ask it all : How to get all property names 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.

like image 841
Klaim Avatar asked Apr 06 '10 15:04

Klaim


2 Answers

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]
like image 145
eklektek Avatar answered Oct 05 '22 22:10

eklektek


Take a look at the MetaClass API.

like image 21
armandino Avatar answered Oct 05 '22 23:10

armandino