我通过Json创建一个新的Java类,即:
{
"package" : "crud.vaadin",
"className" : "StudentForm",
"extension" : "AbstractForm",
"extensiongeneric" : "Person",
"properties" : {
"Name" : {
"type" : "MTextField"
},
"Email" : {
"type" : "MTextField"
}
},
"model" : "Person"
}
将生成:
package crud.vaadin;
public class StudentForm extends AbstractForm<Person>
{
private MTextField Email;
private MTextField Name;
}
我使用JCodeModel生成StudentForm,Field生成方法如下所示:
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();
}
}
}
但是我想在创建源代码时初始化字段。
private MTextField email = new MTextField("Email");
有没有办法通过初始化来扩展JFieldVar?
答案 0 :(得分:0)
您只需要使用JExpression
调用指定初始化.field
,如下所示:
JFieldVar field = jc.field(JMod.PRIVATE, jt, name, JExpr._new(jt).arg(name));