smeeb smeeb - 3 months ago 13
Groovy Question

List Groovy constructors generated by Canonical and TupleConstructor

I have a deep hierarchy of POGOs (domain models) that make heavy use of

@Canonical
and
@TupleConstructor
. I now want to list out all the dynamically-generated constructors available for any one of my domain model classes.

I found this little Groovy gem:

println MyObject.metaClass.methods*.name.sort().unique()


Which, when ran, beautifully prints a list of all the (defined and generated) methods for
MyObject
.

But, when I try to use this for constructors:

println MyObject.metaClass.constructors*.name.sort().unique()


...it just prints out:

[groovy.lang.ExpandoMetaClass$ExpandoMetaProperty@67c27493]


How can I list out all the
@Canonical
- and
@TupleConstructor
-generated constructors (and their respective arguments) for any given class?


More importantly (well not quite, but almost)...where is this documented?!?

Answer

Just remove metaClass:

MyObject.constructors​*.name.sort().unique()​​​​​

It should show just one value: 'MyObject' (because all constructors have name MyObject)

Comments