This is an old question, but I just ran into this requirement and found another solution that is worth answering here to those who are faced with this thread. I put together an example based on this thread:
Bean example
class SampleBean { long id private String firstName String lastName def email Map asMap() { this.class.declaredFields.findAll { !it.synthetic }.collectEntries { [ (it.name):this."$it.name" ] } } }
Testing class
class Test { static main(args) {
SampleBean I put a lot of fields to show that it works, this is println output:
[id:1, firstName:john, lastName:doe, email: jd@gmail.com ]
Hatem jaber
source share