Search in sources :

Example 1 with GenerationContext

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext in project kie-wb-common by kiegroup.

the class DataModelOracleModelDriver method generateModel.

@Override
public void generateModel(DataModel dataModel, ModelDriverListener generationListener) throws Exception {
    GenerationContext generationContext = new GenerationContext(dataModel);
    generationContext.setGenerationListener(generationListener);
    GenerationEngine generationEngine = GenerationEngine.getInstance();
    generationEngine.generate(generationContext);
}
Also used : GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine)

Example 2 with GenerationContext

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext 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);
    }
}
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)

Example 3 with GenerationContext

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext 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);
    }
}
Also used : GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) GenerationTools(org.kie.workbench.common.services.datamodeller.codegen.GenerationTools) ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) FieldSource(org.jboss.forge.roaster.model.source.FieldSource)

Example 4 with GenerationContext

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext in project kie-wb-common by kiegroup.

the class DataModelerServiceImpl method createJavaSource.

private String createJavaSource(DataObject dataObject) throws Exception {
    GenerationContext generationContext = new GenerationContext(null);
    String source;
    GenerationEngine engine;
    try {
        engine = GenerationEngine.getInstance();
        source = engine.generateJavaClassString(generationContext, dataObject);
    } catch (Exception e) {
        logger.error("Java source for dataObject: " + dataObject.getClassName() + " couldn't be created.", e);
        throw e;
    }
    return source;
}
Also used : GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ServiceException(org.kie.workbench.common.screens.datamodeller.service.ServiceException) FileAlreadyExistsException(org.uberfire.java.nio.file.FileAlreadyExistsException)

Example 5 with GenerationContext

use of org.kie.workbench.common.services.datamodeller.codegen.GenerationContext in project kie-wb-common by kiegroup.

the class JavaRoasterModelDriver method updateSource.

public void updateSource(JavaClassSource javaClassSource, DataObject dataObject, UpdateInfo updateInfo, ClassTypeResolver classTypeResolver) throws Exception {
    if (javaClassSource == null || !javaClassSource.isClass()) {
        logger.warn("A null javaClassSource or javaClassSouce is not a Class, no processing will be done. javaClassSource: " + javaClassSource + " className: " + (javaClassSource != null ? javaClassSource.getName() : null));
        return;
    }
    Map<String, FieldSource<JavaClassSource>> currentClassFields = new HashMap<String, FieldSource<JavaClassSource>>();
    List<FieldSource<JavaClassSource>> classFields = javaClassSource.getFields();
    Map<String, String> preservedFields = new HashMap<String, String>();
    // update package, class name, and super class name if needed.
    updatePackage(javaClassSource, dataObject.getPackageName());
    updateImports(javaClassSource, dataObject.getImports(), updateInfo);
    updateAnnotations(javaClassSource, dataObject.getAnnotations(), classTypeResolver);
    updateMethods(javaClassSource, dataObject.getMethods(), classTypeResolver);
    updateClassName(javaClassSource, dataObject.getName());
    updateSuperClassName(javaClassSource, dataObject.getSuperClassName(), classTypeResolver);
    if (classFields != null) {
        for (FieldSource<JavaClassSource> field : classFields) {
            currentClassFields.put(field.getName(), field);
        }
    }
    List<ObjectProperty> currentManagedProperties = parseManagedTypesProperties(javaClassSource, classTypeResolver);
    currentManagedProperties = DataModelUtils.filterAssignableFields(currentManagedProperties);
    // prior to touch the class fields get the constructors candidates to be the current all fields, position annotated fields, and key annotated fields constructors.
    List<MethodSource<JavaClassSource>> allFieldsConstructorCandidates = findAllFieldsConstructorCandidates(javaClassSource, currentManagedProperties, classTypeResolver);
    List<MethodSource<JavaClassSource>> keyFieldsConstructorCandidates = findKeyFieldsConstructorCandidates(javaClassSource, currentManagedProperties, classTypeResolver);
    List<MethodSource<JavaClassSource>> positionFieldsConstructorCandidates = findPositionFieldsConstructorCandidates(javaClassSource, currentManagedProperties, classTypeResolver);
    // create new fields and update existing.
    for (ObjectProperty property : dataObject.getProperties()) {
        if (property.isFinal() || property.isStatic()) {
            preservedFields.put(property.getName(), property.getName());
            continue;
        }
        if (currentClassFields.containsKey(property.getName())) {
            updateField(javaClassSource, property.getName(), property, classTypeResolver);
        } else {
            createField(javaClassSource, property, classTypeResolver);
        }
        preservedFields.put(property.getName(), property.getName());
    }
    // update constructors, equals and hashCode methods.
    updateConstructors(javaClassSource, dataObject, allFieldsConstructorCandidates, keyFieldsConstructorCandidates, positionFieldsConstructorCandidates, classTypeResolver);
    // delete fields from .java file that not exists in the DataObject.
    List<String> removableFields = new ArrayList<String>();
    for (FieldSource<JavaClassSource> field : currentClassFields.values()) {
        if (!preservedFields.containsKey(field.getName()) && isManagedField(field, classTypeResolver)) {
            removableFields.add(field.getName());
        }
    }
    for (String fieldName : removableFields) {
        removeField(javaClassSource, fieldName, classTypeResolver);
    }
    // update nested classes
    List<JavaSource<?>> nestedTypes = javaClassSource.getNestedTypes();
    if (nestedTypes != null) {
        for (JavaSource nestedJavaSource : nestedTypes) {
            if (isAccepted(nestedJavaSource)) {
                javaClassSource.removeNestedType(nestedJavaSource);
            }
        }
    }
    GenerationEngine engine = GenerationEngine.getInstance();
    GenerationContext context = new GenerationContext(null);
    for (org.kie.workbench.common.services.datamodeller.core.JavaClass nestedJavaClass : dataObject.getNestedClasses()) {
        javaClassSource.addNestedType(engine.generateNestedClassString(context, nestedJavaClass, ""));
    }
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) JavaSource(org.jboss.forge.roaster.model.source.JavaSource) FieldSource(org.jboss.forge.roaster.model.source.FieldSource)

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