2016-07-28 98 views
2

如何獲取接口的方法作爲屬性列表。我有下面的界面,像這樣groovy接口方法屬性

interface ValuationEvent { 
    String getType() 
    String getAggregateId() 
    String getXmlPayload() 
} 

我想獲得的方法爲屬性列表方法有些像最後的輸出應該是

[type, aggregateId, xmlPayload] 

我使用性能的方法嘗試,但它給了我54種性質一長串不包含上述特性

def documentProperties = ValuationEvent.properties 

回答

1

也許只是讓所有的方法,從正面和首字母小寫刪除「搞定」?

ValuationEvent.getDeclaredMethods().collect{ 
    it.name.replace("get","").with{ it[0].toLowerCase() + it[1..-1] } 
}​ 
0

可以collect屬性名稱:

interface ValuationEvent { 
    String getType() 
    String getAggregateId() 
    String getXmlPayload() 
} 

def properties = ValuationEvent.metaClass.properties.collect { it.name } 

assert properties == ['type', 'aggregateId', 'xmlPayload']