use of org.kie.workbench.common.services.datamodeller.codegen.GenerationTools in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method updateField.
public void updateField(JavaClassSource javaClassSource, String fieldName, ObjectProperty property, ClassTypeResolver classTypeResolver) throws Exception {
GenerationTools genTools = new GenerationTools();
GenerationEngine engine = GenerationEngine.getInstance();
GenerationContext context = new GenerationContext(null);
boolean updateAccessors = false;
FieldSource<JavaClassSource> field;
field = javaClassSource.getField(fieldName);
Type oldType = field.getType();
if (hasChangedToCollectionType(field, property, classTypeResolver)) {
// fields that changed to a collection like java.util.List<SomeEntity>
// needs to be removed and created again due to Roaster. Ideally it shouldn't be so.
updateCollectionField(javaClassSource, fieldName, property, classTypeResolver);
} else {
if (!fieldName.equals(property.getName())) {
field.setName(property.getName());
// the field was renamed, accessors must be updated.
updateAccessors = true;
}
if (DriverUtils.isManagedType(field.getType(), classTypeResolver) && !DriverUtils.equalsType(field.getType(), property.getClassName(), property.isMultiple(), property.getBag(), classTypeResolver)) {
// the has type changed, and not to a collection type.
String newClassName = property.getClassName();
field.setType(newClassName);
if (field.getLiteralInitializer() != null) {
// valid for the new type.
if (NamingUtils.isPrimitiveTypeId(newClassName)) {
setPrimitiveTypeDefaultInitializer(field, newClassName);
} else {
field.setLiteralInitializer(null);
}
}
updateAccessors = true;
}
updateAnnotations(field, property.getAnnotations(), classTypeResolver);
if (updateAccessors) {
String accessorName;
String methodSource;
String oldClassName;
// remove old accessors
// TODO check primitive types
Class<?> oldClass = classTypeResolver.resolveType(oldType.getName());
oldClassName = oldClass.getName();
accessorName = genTools.toJavaGetter(fieldName, oldClassName);
removeMethodByParamsClass(javaClassSource, accessorName);
accessorName = genTools.toJavaSetter(fieldName);
removeMethodByParamsClass(javaClassSource, accessorName, oldClass);
// and generate the new ones
methodSource = genTools.indent(engine.generateFieldGetterString(context, property));
javaClassSource.addMethod(methodSource);
methodSource = genTools.indent(engine.generateFieldSetterString(context, property));
javaClassSource.addMethod(methodSource);
}
}
}
Aggregations