Search in sources :

Example 6 with GenerationTools

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);
        }
    }
}
Also used : GenerationTools(org.kie.workbench.common.services.datamodeller.codegen.GenerationTools) GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) Type(org.jboss.forge.roaster.model.Type) JavaType(org.jboss.forge.roaster.model.JavaType) ElementType(org.kie.workbench.common.services.datamodeller.core.ElementType) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource)

Aggregations

GenerationTools (org.kie.workbench.common.services.datamodeller.codegen.GenerationTools)6 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)4 GenerationContext (org.kie.workbench.common.services.datamodeller.codegen.GenerationContext)4 GenerationEngine (org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine)4 JavaType (org.jboss.forge.roaster.model.JavaType)2 Type (org.jboss.forge.roaster.model.Type)2 ElementType (org.kie.workbench.common.services.datamodeller.core.ElementType)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ParserException (org.jboss.forge.roaster.ParserException)1 AnnotationSource (org.jboss.forge.roaster.model.source.AnnotationSource)1 FieldSource (org.jboss.forge.roaster.model.source.FieldSource)1 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)1 AnnotationDefinition (org.kie.workbench.common.services.datamodeller.core.AnnotationDefinition)1 AnnotationValuePairDefinition (org.kie.workbench.common.services.datamodeller.core.AnnotationValuePairDefinition)1 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)1 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)1 ObjectPropertyImpl (org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl)1 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)1