Groovy Beans allows you to directly access fields. You do not need to define getter / setter methods. They are created for you. Whenever you access the bean property, the getter / setter method is called internally. You can get around this behavior using the @ operator. See the following example:
class Person { String name Address address List<Account> accounts = [] } class Address { String street Integer zip } class Account { String bankName Long balance } def person = new Person(name: 'Richardson Heights', address: new Address(street: 'Baker Street', zip: 22222)) person.accounts << new Account(bankName: 'BOA', balance: 450) person.accounts << new Account(bankName: 'CitiBank', balance: 300)
If you are not dealing with collections, you can simply call up the field you want to access.
assert 'Richardson Heights' == person.name assert 'Baker Street' == person.address.street assert 22222 == person.address.zip
If you want to access the field inside the collection, you need to select an element:
assert 'BOA' == person.accounts[0].bankName assert 300 == person.accounts[1].balanceβββββββββ
Benjamin muschko
source share