Search in sources :

Example 6 with GenerationEngine

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine 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;
    }
}
Also used : GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) GenerationTools(org.kie.workbench.common.services.datamodeller.codegen.GenerationTools) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) IOException(java.io.IOException) ParserException(org.jboss.forge.roaster.ParserException)

Example 7 with GenerationEngine

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine 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

GenerationContext (org.kie.workbench.common.services.datamodeller.codegen.GenerationContext)7 GenerationEngine (org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine)7 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)4 GenerationTools (org.kie.workbench.common.services.datamodeller.codegen.GenerationTools)4 ArrayList (java.util.ArrayList)2 JavaType (org.jboss.forge.roaster.model.JavaType)2 Type (org.jboss.forge.roaster.model.Type)2 FieldSource (org.jboss.forge.roaster.model.source.FieldSource)2 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)2 ElementType (org.kie.workbench.common.services.datamodeller.core.ElementType)2 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)2 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ParserException (org.jboss.forge.roaster.ParserException)1 JavaSource (org.jboss.forge.roaster.model.source.JavaSource)1 ServiceException (org.kie.workbench.common.screens.datamodeller.service.ServiceException)1 ObjectPropertyImpl (org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl)1 FileAlreadyExistsException (org.uberfire.java.nio.file.FileAlreadyExistsException)1