use of org.kie.workbench.common.services.datamodeller.codegen.GenerationTools in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method updateCollectionField.
private void updateCollectionField(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 = true;
FieldSource<JavaClassSource> currentField;
currentField = javaClassSource.getField(fieldName);
Type currentType = currentField.getType();
StringBuilder fieldSource = new StringBuilder();
fieldSource.append(engine.generateCompleteFieldString(context, property));
javaClassSource.removeField(currentField);
javaClassSource.addField(fieldSource.toString());
if (updateAccessors) {
String accessorName;
String methodSource;
String oldClassName;
// remove old accessors
// TODO check primitive types
Class<?> oldClass = classTypeResolver.resolveType(currentType.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);
}
}
use of org.kie.workbench.common.services.datamodeller.codegen.GenerationTools in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method resolveAnnotationSource.
public org.kie.workbench.common.services.datamodeller.driver.model.AnnotationSource resolveAnnotationSource(Annotation annotation) {
org.kie.workbench.common.services.datamodeller.driver.model.AnnotationSource annotationSource = new org.kie.workbench.common.services.datamodeller.driver.model.AnnotationSource();
// TODO this method can be optimized and likely migrated to Roaster. Should be reviewed when we evaluate
// the removal of Velocity.
GenerationTools generationTools = new GenerationTools();
AnnotationDefinition annotationDefinition;
StringBuilder annotationCode = new StringBuilder();
annotationCode.append("@");
annotationCode.append(annotation.getClassName());
if ((annotationDefinition = annotation.getAnnotationDefinition()) != null) {
if (!annotationDefinition.isMarker()) {
annotationCode.append(generationTools.resolveAnnotationType(annotation));
}
if (annotationDefinition.getValuePairs() != null) {
Object value;
String valuePairCode;
for (AnnotationValuePairDefinition valuePairDefinition : annotationDefinition.getValuePairs()) {
if ((value = annotation.getValue(valuePairDefinition.getName())) != null) {
valuePairCode = generationTools.resolveMemberTypeExpression(valuePairDefinition, value);
} else {
valuePairCode = null;
}
annotationSource.withValuePairSource(valuePairDefinition.getName(), valuePairCode);
}
}
}
annotationSource.withSource(annotationCode.toString());
return annotationSource;
}
use of org.kie.workbench.common.services.datamodeller.codegen.GenerationTools in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method removeField.
/**
* Takes care of field and the corresponding setter/getter removal.
*/
public void removeField(JavaClassSource javaClassSource, String fieldName, ClassTypeResolver classTypeResolver) throws Exception {
logger.debug("Removing field: " + fieldName + ", from class: " + javaClassSource.getName());
FieldSource<JavaClassSource> field;
GenerationTools genTools = new GenerationTools();
String methodName;
field = javaClassSource.getField(fieldName);
if (field != null) {
// check if the class has a setter/getter for the given field.
Class<?> fieldClass = classTypeResolver.resolveType(field.getType().getName());
methodName = genTools.toJavaGetter(fieldName, fieldClass.getName());
removeMethodByParamsClass(javaClassSource, methodName);
methodName = genTools.toJavaSetter(fieldName);
removeMethodByParamsClass(javaClassSource, methodName, fieldClass);
// finally remove the field.
javaClassSource.removeField(field);
} else {
logger.debug("Field field: " + fieldName + " was not found in class: " + javaClassSource.getName());
}
}
use of org.kie.workbench.common.services.datamodeller.codegen.GenerationTools in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method updateConstructors.
public void updateConstructors(JavaClassSource javaClassSource, DataObject dataObject, List<MethodSource<JavaClassSource>> allFieldsConstructorCandidates, List<MethodSource<JavaClassSource>> keyFieldsConstructorCandidates, List<MethodSource<JavaClassSource>> positionFieldsConstructorCandidates, ClassTypeResolver classTypeResolver) throws Exception {
GenerationContext generationContext = new GenerationContext(null);
GenerationEngine engine = GenerationEngine.getInstance();
GenerationTools genTools = new GenerationTools();
JavaRoasterModelDriver modelDriver = new JavaRoasterModelDriver();
boolean needsAllFieldsConstructor;
boolean needsKeyFieldsConstructor;
boolean needsPositionFieldsConstructor;
boolean needsEmptyConstructor;
String defaultConstructorSource;
String allFieldsConstructorSource;
String keyFieldsConstructorSource;
String positionFieldsConstructorSource;
String equalsMethodSource;
String hashCodeMethodSource;
// check if the candidate methods has exactly the same body of the generated by the data modeller.
List<MethodSource<JavaClassSource>> currentAllFieldsConstructors = modelDriver.filterGeneratedConstructors(allFieldsConstructorCandidates);
List<MethodSource<JavaClassSource>> currentKeyFieldsConstructors = modelDriver.filterGeneratedConstructors(keyFieldsConstructorCandidates);
List<MethodSource<JavaClassSource>> currentPositionFieldsConstructors = modelDriver.filterGeneratedConstructors(positionFieldsConstructorCandidates);
if (logger.isDebugEnabled()) {
logger.debug("allFieldsConstructorCandidates candidates: " + allFieldsConstructorCandidates.size());
logger.debug(allFieldsConstructorCandidates.size() > 0 ? allFieldsConstructorCandidates.get(0).toString() : "");
logger.debug("\n\n");
logger.debug("currentAllFieldsConstructors: " + currentAllFieldsConstructors.size());
logger.debug(currentAllFieldsConstructors.size() > 0 ? currentAllFieldsConstructors.get(0).toString() : "");
logger.debug("\n\n");
logger.debug("KeyFieldsConstructorCandidates: " + keyFieldsConstructorCandidates.size());
logger.debug(keyFieldsConstructorCandidates.size() > 0 ? keyFieldsConstructorCandidates.get(0).toString() : "");
logger.debug("\n\n");
logger.debug("currentKeyFieldsConstructors: " + currentKeyFieldsConstructors.size());
logger.debug(currentKeyFieldsConstructors.size() > 0 ? currentKeyFieldsConstructors.get(0).toString() : "");
logger.debug("\n\n");
logger.debug("positionFieldsConstructorCandidates: " + positionFieldsConstructorCandidates.size());
logger.debug(positionFieldsConstructorCandidates.size() > 0 ? positionFieldsConstructorCandidates.get(0).toString() : "");
logger.debug("\n\n");
logger.debug("currentPositionFieldsConstructors: " + currentPositionFieldsConstructors.size());
logger.debug(currentPositionFieldsConstructors.size() > 0 ? currentPositionFieldsConstructors.get(0).toString() : "");
logger.debug("\n\n");
}
// delete current data modeller generated all fields, key fields, and position fields constructors if there are any.
for (MethodSource<JavaClassSource> constructor : currentAllFieldsConstructors) {
javaClassSource.removeMethod(constructor);
}
for (MethodSource<JavaClassSource> constructor : currentKeyFieldsConstructors) {
javaClassSource.removeMethod(constructor);
}
for (MethodSource<JavaClassSource> constructor : currentPositionFieldsConstructors) {
javaClassSource.removeMethod(constructor);
}
// calculate the file order for the fields.
List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields();
if (fields != null && fields.size() > 0) {
int fileOrder = 0;
for (FieldSource<JavaClassSource> field : fields) {
ObjectPropertyImpl objectProperty = (ObjectPropertyImpl) dataObject.getProperty(field.getName());
if (objectProperty != null) {
objectProperty.setFileOrder(fileOrder);
}
fileOrder++;
}
}
// get the sorted list of all fields, position annotated and key annotated fields. These lists will be used
// to identify collisions with client provided constructors.
List<ObjectProperty> allFields = DataModelUtils.sortByFileOrder(DataModelUtils.filterAssignableFields(dataObject));
List<ObjectProperty> positionFields = DataModelUtils.sortByPosition(DataModelUtils.filterPositionFields(dataObject));
List<ObjectProperty> keyFields = DataModelUtils.sortByFileOrder(DataModelUtils.filterKeyFields(dataObject));
// we always wants to generate the default constructor.
needsEmptyConstructor = true;
needsAllFieldsConstructor = allFields.size() > 0;
needsPositionFieldsConstructor = positionFields.size() > 0 && !DataModelUtils.equalsByFieldName(allFields, positionFields) && !DataModelUtils.equalsByFieldType(allFields, positionFields);
needsKeyFieldsConstructor = keyFields.size() > 0 && !DataModelUtils.equalsByFieldName(allFields, keyFields) && !DataModelUtils.equalsByFieldType(allFields, keyFields) && !DataModelUtils.equalsByFieldName(positionFields, keyFields) && !DataModelUtils.equalsByFieldType(positionFields, keyFields);
List<MethodSource<JavaClassSource>> currentConstructors = new ArrayList<MethodSource<JavaClassSource>>();
MethodSource<JavaClassSource> currentEquals = null;
MethodSource<JavaClassSource> currentHashCode = null;
MethodSource<JavaClassSource> newConstructor;
// Iterate remaining methods looking for client provided constructors, hashCode and equals methods.
List<MethodSource<JavaClassSource>> methods = javaClassSource.getMethods();
if (methods != null) {
for (MethodSource<JavaClassSource> method : methods) {
if (method.isConstructor()) {
currentConstructors.add(method);
if (method.getParameters() == null || method.getParameters().size() == 0) {
needsEmptyConstructor = false;
}
} else if (isEquals(method)) {
currentEquals = method;
} else if (isHashCode(method)) {
currentHashCode = method;
}
}
}
// check collisions with remaining constructors first.
needsAllFieldsConstructor = needsAllFieldsConstructor && (findMatchingConstructorsByTypes(javaClassSource, allFields, classTypeResolver).size() == 0);
needsPositionFieldsConstructor = needsPositionFieldsConstructor && (findMatchingConstructorsByTypes(javaClassSource, positionFields, classTypeResolver).size() == 0);
needsKeyFieldsConstructor = needsKeyFieldsConstructor && (findMatchingConstructorsByTypes(javaClassSource, keyFields, classTypeResolver).size() == 0);
// remove current equals and hashCode methods
if (currentEquals != null) {
javaClassSource.removeMethod(currentEquals);
}
if (currentHashCode != null) {
javaClassSource.removeMethod(currentHashCode);
}
if (needsEmptyConstructor) {
defaultConstructorSource = genTools.indent(engine.generateDefaultConstructorString(generationContext, dataObject));
newConstructor = javaClassSource.addMethod(defaultConstructorSource);
newConstructor.setConstructor(true);
}
if (needsAllFieldsConstructor) {
allFieldsConstructorSource = genTools.indent(engine.generateAllFieldsConstructorString(generationContext, dataObject));
if (allFieldsConstructorSource != null && !allFieldsConstructorSource.trim().isEmpty()) {
newConstructor = javaClassSource.addMethod(allFieldsConstructorSource);
newConstructor.setConstructor(true);
}
}
if (needsPositionFieldsConstructor) {
positionFieldsConstructorSource = genTools.indent(engine.generatePositionFieldsConstructorString(generationContext, dataObject));
if (positionFieldsConstructorSource != null && !positionFieldsConstructorSource.trim().isEmpty()) {
newConstructor = javaClassSource.addMethod(positionFieldsConstructorSource);
newConstructor.setConstructor(true);
}
}
if (needsKeyFieldsConstructor) {
keyFieldsConstructorSource = genTools.indent(engine.generateKeyFieldsConstructorString(generationContext, dataObject));
if (keyFieldsConstructorSource != null && !keyFieldsConstructorSource.trim().isEmpty()) {
newConstructor = javaClassSource.addMethod(keyFieldsConstructorSource);
newConstructor.setConstructor(true);
}
}
if (keyFields.size() > 0) {
equalsMethodSource = genTools.indent(engine.generateEqualsString(generationContext, dataObject));
javaClassSource.addMethod(equalsMethodSource);
hashCodeMethodSource = genTools.indent(engine.generateHashCodeString(generationContext, dataObject));
javaClassSource.addMethod(hashCodeMethodSource);
}
}
use of org.kie.workbench.common.services.datamodeller.codegen.GenerationTools 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;
}
}
Aggregations