use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method createField.
public void createField(JavaClassSource javaClassSource, ObjectProperty property, ClassTypeResolver classTypeResolver) throws Exception {
String fieldSource;
String methodSource;
String methodName;
GenerationContext generationContext = new GenerationContext(null);
GenerationEngine engine;
GenerationTools genTools = new GenerationTools();
try {
engine = GenerationEngine.getInstance();
fieldSource = genTools.indent(engine.generateCompleteFieldString(generationContext, property));
javaClassSource.addField(fieldSource);
// create getter
methodSource = genTools.indent(engine.generateFieldGetterString(generationContext, property));
methodName = genTools.toJavaGetter(property.getName(), property.getClassName());
// remove old getter if exists
removeMethodByParamsClassName(javaClassSource, methodName);
// add the new getter
javaClassSource.addMethod(methodSource);
// create setter
methodSource = genTools.indent(engine.generateFieldSetterString(generationContext, property));
methodName = genTools.toJavaSetter(property.getName());
// remove old setter if exists
// TODO check collections
// TODO aca tengo un problema cuando creo un Pojo en memoria y a su vez un field de ese tipo.
// Porque intento resolver la clase con el classTypeResolver y el Pojo aun no ha sido creado con lo cual
// tengo Class Not found exception.
// Tengo que implementar el remove de otra forma para este caso, posiblemente iterando todos los metodos.
// Cuando le cambio el tipo a un field de un pojo existente hacia un tipo de una clase creada en memoria
// Crei que podria darse tambien esta exception pero parece que no.
// Tengo que ver a ver porque no se da el error en este caso.
// Class<?> fieldClass = classTypeResolver.resolveType( property.getClassName() );
removeMethodByParamsClassName(javaClassSource, methodName, property.getClassName());
// add the new setter
javaClassSource.addMethod(methodSource);
} catch (Exception e) {
logger.error("Field: " + property.getName() + " couldn't be created.", e);
throw e;
}
}
use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext 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