如何获取Groovy类的所有属性名称? [英] How to get all property names of a Groovy class?

查看:128
本文介绍了如何获取Groovy类的所有属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题问题全部:如何获取Groovy类的所有属性名称?



甚至有可能吗?我以为我可以使用集合语法与类似于似乎不起作用。

解决方案

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

  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] 


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.

解决方案

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]

这篇关于如何获取Groovy类的所有属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆