Search in sources :

Example 51 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.

the class RoasterApiTest method createClass.

public JavaClassSource createClass(String pkg, String name) {
    JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    javaClass.setPackage(pkg);
    javaClass.setName(name);
    return javaClass;
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource)

Example 52 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.

the class DriverUtils method createClassTypeResolver.

public static ClassTypeResolver createClassTypeResolver(JavaSource javaSource, ClassLoader classLoader) {
    String packageName;
    Set<String> classImports = new HashSet<String>();
    // Importer.getImports() returns both normal and static imports
    // You can see if an Import is static by calling hte
    // Import.isStatic() method
    List<Import> imports = javaSource.getImports();
    if (imports != null) {
        for (Import currentImport : imports) {
            String importName = currentImport.getQualifiedName();
            if (currentImport.isWildcard()) {
                importName = importName + ".*";
            }
            classImports.add(importName);
        }
    }
    packageName = javaSource.getPackage();
    // add current package too, if not added, the class type resolver don't resolve current package classes.
    if (packageName != null && !"".equals(packageName)) {
        classImports.add(packageName + ".*");
    }
    if (javaSource instanceof JavaClassSource) {
        JavaClassSource javaClassSource = (JavaClassSource) javaSource;
        // add current file inner types as import clauses to help the ClassTypeResolver to find variables of inner types
        // It was detected that current ClassTypeResolver don't resolve inner classes well.
        // workaround for BZ https://bugzilla.redhat.com/show_bug.cgi?id=1172711
        List<JavaSource<?>> innerTypes = javaClassSource.getNestedTypes();
        if (innerTypes != null) {
            for (JavaSource<?> type : innerTypes) {
                classImports.add(packageName + "." + javaClassSource.getName() + "." + type.getName());
            }
        }
    }
    return new ClassTypeResolver(classImports, classLoader);
}
Also used : Import(org.jboss.forge.roaster.model.source.Import) JavaSource(org.jboss.forge.roaster.model.source.JavaSource) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) HashSet(java.util.HashSet)

Example 53 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.

the class JavaRoasterModelDriver method addMethod.

private void addMethod(JavaClassSource javaClassSource, org.kie.workbench.common.services.datamodeller.core.Method method, ClassTypeResolver classTypeResolver) throws ClassNotFoundException {
    MethodSource<JavaClassSource> methodSource = javaClassSource.addMethod();
    methodSource.setName(method.getName());
    methodSource.setReturnType(buildMethodReturnTypeString(method.getReturnType(), classTypeResolver));
    methodSource.setParameters(buildMethodParameterString(method.getParameters(), classTypeResolver));
    methodSource.setBody(method.getBody());
    methodSource.setVisibility(buildVisibility(method.getVisibilty()));
    for (Annotation annotation : method.getAnnotations()) {
        addAnnotation(methodSource, annotation);
    }
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Annotation(org.kie.workbench.common.services.datamodeller.core.Annotation)

Example 54 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource 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 55 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource 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());
    }
}
Also used : GenerationTools(org.kie.workbench.common.services.datamodeller.codegen.GenerationTools) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource)

Aggregations

JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)95 File (java.io.File)37 ArrayList (java.util.ArrayList)28 Test (org.junit.Test)26 ParserResult (org.apache.camel.parser.ParserResult)19 List (java.util.List)15 IOException (java.io.IOException)14 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)12 InputStream (java.io.InputStream)10 Collections (java.util.Collections)10 Roaster (org.jboss.forge.roaster.Roaster)10 BufferedReader (java.io.BufferedReader)8 HashMap (java.util.HashMap)8 Matcher (java.util.regex.Matcher)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)8 Arrays (java.util.Arrays)7 Map (java.util.Map)7 Import (org.jboss.forge.roaster.model.source.Import)7 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)7