Groovy: how to get properties declared in a base class

In the following code, I need all the properties available in the Child class (i.e. foo, bar ). I'm not interested in all the other properties added by groovy.

 class Parent {def foo} class Child extends Parent {def bar} 

So far, none of them have received the result:

 println Child.fields println Child.properties.each{k,v->println "$k -> $v"} println Child.declaredFields.findAll { !it.synthetic }.collect {it.name} println Child.methods.findAll {it.name.startsWith("get")}.collect {it.name} 

I am trying to find some direct method that would give me this.

+6
source share
2 answers

This will give you what you need:

 assert ['foo', 'class', 'bar'] == B.metaClass.properties*.name 
+4
source

What about instance validation? Also, I missed extends in Child

 class A { def foo } class B extends A { def bar } b = new B(foo: 'foo', bar: 'bar') assert b.properties == [foo: 'foo', class: B, bar: 'bar'] 
0
source

All Articles