I create a new Java class through a Json, i.e. :
{
"package" : "crud.vaadin",
"className" : "StudentForm",
"extension" : "AbstractForm",
"extensiongeneric" : "Person",
"properties" : {
"Name" : {
"type" : "MTextField"
},
"Email" : {
"type" : "MTextField"
}
},
"model" : "Person"
}
will generate :
package crud.vaadin;
public class StudentForm extends AbstractForm<Person>
{
private MTextField Email;
private MTextField Name;
}
I use JCodeModel for the StudentForm generation and the Field generating method looks like this:
public void createFields(Map<String, Object> properties, JDefinedClass jc, JCodeModel model) {
for(String key : properties.keySet()){
//getType returns MTextField and key is either Email or Name
String type = getType(prop.get(key));
String name = key;
try {
JType jt = model.parseType(type);
JFieldVar field = jc.field(JMod.PRIVATE, jt, name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
But I would like to initialize the fields upon creation of the source.
private MTextField email = new MTextField("Email");
Is there a way to extend the JFieldVar by the initialization?
You just need to specify an initializing
JExpression
with the.field
call like so: