Assuming you have a constructor
Customer(String name, String email, String phone)
You can do:
new Customer(*infoLine.split(','))
If you don't want to write a constructor, you can get Groovy to create one for you:
import groovy.transform.*
@TupleConstructor
class Customer {
String name
String email
String phone
}
String infoLine = "Stanislav,[email protected],004612345678"
new Customer(*infoLine.split(','))
Or even better, @Immutable as this makes the properties final
@Immutable
class Customer {
String name
String email
String phone
}
Another option (assuming your fields are defined in the order they appear in the string, and there are no other fields), would be to generate a map of [name: 'aaa', emai... etc, and tell groovy to convert the map to a Customer like:
class Customer {
String name
String email
String phone
}
String infoLine = "Stanislav,[email protected],004612345678"
def customer = [
Customer.declaredFields.findAll { !it.synthetic }*.name,
infoLine.split(',')
].transpose().collectEntries() as Customer
But this feels kinda brittle, and it's probably quicker to add the annotation or constructor.