Search in sources :

Example 1 with JavaSource

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

the class JavaFileIndexer method fillIndexBuilder.

@Override
public IndexBuilder fillIndexBuilder(final Path path) throws Exception {
    if (!ioService.exists(path)) {
        logger.error("Unable to index {} : file does not exist.", path.toUri());
        return null;
    }
    // create indexbuilder
    final KieModule module = getModule(path);
    if (module == null) {
        logger.error("Unable to index " + path.toUri().toString() + ": module could not be resolved.");
        return null;
    }
    final Package pkg = getPackage(path);
    if (pkg == null) {
        logger.error("Unable to index " + path.toUri().toString() + ": package could not be resolved.");
        return null;
    }
    // responsible for basic index info: module name, branch, etc
    final DefaultIndexBuilder builder = new DefaultIndexBuilder(Paths.convert(path).getFileName(), module, pkg);
    // visit/index java source
    final String javaSource = ioService.readAllString(path);
    org.jboss.forge.roaster.model.JavaType<?> javaType = Roaster.parse(javaSource);
    if (javaType.getSyntaxErrors() == null || javaType.getSyntaxErrors().isEmpty()) {
        if (javaFileIndexerExtensions != null) {
            for (JavaFileIndexerExtension javaFileIndexerExtension : javaFileIndexerExtensions) {
                javaFileIndexerExtension.process(builder, javaType);
            }
        }
        String pkgName = pkg.getPackageName();
        pkgName = javaType.getPackage();
        if (pkgName == null) {
            pkgName = "";
        }
        // use Java class package name, not Package name
        builder.setPackageName(pkgName);
        String javaTypeName = javaType.getQualifiedName();
        Resource resParts = new Resource(javaTypeName, ResourceType.JAVA);
        if (javaType instanceof JavaSource) {
            ClassLoader moduleClassLoader = getModuleClassLoader(module);
            JavaSourceVisitor visitor = new JavaSourceVisitor((JavaSource) javaType, moduleClassLoader, resParts);
            visitor.visit((JavaSource) javaType);
            addReferencedResourcesToIndexBuilder(builder, visitor);
        }
        builder.addGenerator(resParts);
    }
    return builder;
}
Also used : Resource(org.kie.workbench.common.services.refactoring.Resource) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) JavaSource(org.jboss.forge.roaster.model.source.JavaSource) Package(org.guvnor.common.services.project.model.Package) KieModule(org.kie.workbench.common.services.shared.project.KieModule)

Example 2 with JavaSource

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

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

the class JavaRoasterModelDriver method parseDataObject.

private Pair<DataObject, List<ObjectProperty>> parseDataObject(JavaClassSource javaClassSource) throws ModelDriverException {
    String className;
    String packageName;
    String superClass;
    String qualifiedName;
    ClassTypeResolver classTypeResolver;
    className = javaClassSource.getName();
    packageName = javaClassSource.getPackage();
    qualifiedName = NamingUtils.createQualifiedName(packageName, className);
    if (logger.isDebugEnabled()) {
        logger.debug("Building DataObject for, packageName: " + packageName + ", className: " + className);
    }
    classTypeResolver = DriverUtils.createClassTypeResolver(javaClassSource, classLoader);
    Visibility visibility = DriverUtils.buildVisibility(javaClassSource.getVisibility());
    DataObject dataObject = new DataObjectImpl(packageName, className, visibility, javaClassSource.isAbstract(), javaClassSource.isFinal());
    List<ObjectProperty> unmanagedProperties = new ArrayList<ObjectProperty>();
    try {
        if (javaClassSource.getSuperType() != null) {
            superClass = resolveTypeName(classTypeResolver, javaClassSource.getSuperType());
            dataObject.setSuperClassName(superClass);
        }
        List<AnnotationSource<JavaClassSource>> annotations = javaClassSource.getAnnotations();
        if (annotations != null) {
            for (AnnotationSource annotation : annotations) {
                addJavaClassAnnotation(dataObject, annotation, classTypeResolver);
            }
        }
        List<MethodSource<JavaClassSource>> classMethods = javaClassSource.getMethods();
        if (classMethods != null) {
            for (MethodSource<JavaClassSource> classMethod : classMethods) {
                if (isAccepted(classMethod)) {
                    addMethod(dataObject, classMethod, classTypeResolver);
                }
            }
        }
        List<JavaSource<?>> nestedTypes = javaClassSource.getNestedTypes();
        if (nestedTypes != null) {
            for (JavaSource nestedType : nestedTypes) {
                if (isAccepted(nestedType)) {
                    if (nestedType instanceof JavaClassSource) {
                        JavaClassImpl nestedJavaClass = new JavaClassImpl("", nestedType.getName(), DriverUtils.buildVisibility(nestedType.getVisibility()));
                        dataObject.addNestedClass(nestedJavaClass);
                        if (javaClassSource.getInterfaces() != null) {
                            for (String interfaceDefinition : ((JavaClassSource) nestedType).getInterfaces()) {
                                nestedJavaClass.addInterface(interfaceDefinition);
                            }
                        }
                        List<AnnotationSource<JavaClassSource>> nestedClassAnnotations = nestedType.getAnnotations();
                        if (nestedClassAnnotations != null) {
                            for (AnnotationSource annotation : nestedClassAnnotations) {
                                addJavaClassAnnotation(nestedJavaClass, annotation, classTypeResolver);
                            }
                        }
                        List<MethodSource<JavaClassSource>> nestedClassMethods = ((JavaClassSource) nestedType).getMethods();
                        if (nestedClassMethods != null) {
                            for (Method nestedClassMethod : nestedClassMethods) {
                                if (isAccepted(nestedClassMethod)) {
                                    addMethod(nestedJavaClass, nestedClassMethod, classTypeResolver);
                                }
                            }
                        }
                    }
                }
            }
        }
        List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields();
        if (fields != null) {
            for (FieldSource<JavaClassSource> field : fields) {
                if (DriverUtils.isManagedType(field.getType(), classTypeResolver)) {
                    addProperty(dataObject, field, classTypeResolver);
                } else {
                    logger.debug("field: " + field + "with fieldName: " + field.getName() + " won't be loaded by the diver because type: " + field.getType().getName() + " isn't a managed type.");
                    unmanagedProperties.add(new ObjectPropertyImpl(field.getName(), field.getType().toString(), false, DriverUtils.buildVisibility(field.getVisibility()), field.isStatic(), field.isFinal()));
                }
            }
        }
        List<Import> imports = javaClassSource.getImports();
        if (imports != null) {
            for (Import _import : imports) {
                dataObject.addImport(new ImportImpl(_import.getQualifiedName()));
            }
        }
        return new Pair<DataObject, List<ObjectProperty>>(dataObject, unmanagedProperties);
    } catch (ClassNotFoundException e) {
        logger.error(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
        throw new ModelDriverException(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
    } catch (ModelDriverException e) {
        logger.error(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
        throw new ModelDriverException(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
    }
}
Also used : Import(org.jboss.forge.roaster.model.source.Import) ArrayList(java.util.ArrayList) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) JavaClassImpl(org.kie.workbench.common.services.datamodeller.core.impl.JavaClassImpl) JavaSource(org.jboss.forge.roaster.model.source.JavaSource) FieldSource(org.jboss.forge.roaster.model.source.FieldSource) Pair(org.uberfire.commons.data.Pair) ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Method(org.jboss.forge.roaster.model.Method) DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) Visibility(org.kie.workbench.common.services.datamodeller.core.Visibility) ImportImpl(org.kie.workbench.common.services.datamodeller.core.impl.ImportImpl) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)

Example 4 with JavaSource

use of org.jboss.forge.roaster.model.source.JavaSource 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)

Example 5 with JavaSource

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

the class TestJavaIndexer method fillIndexBuilder.

@Override
protected IndexBuilder fillIndexBuilder(Path path) throws Exception {
    final DefaultIndexBuilder builder = getIndexBuilder(path);
    if (builder == null) {
        return null;
    }
    String javaSource = ioService.readAllString(path);
    org.jboss.forge.roaster.model.JavaType<?> javaType = Roaster.parse(javaSource);
    if (javaType.getSyntaxErrors() == null || javaType.getSyntaxErrors().isEmpty()) {
        String pkgName = javaType.getPackage();
        if (pkgName == null) {
            pkgName = "";
        }
        // use Java class package name, not Package name
        builder.setPackageName(pkgName);
        String javaTypeName = javaType.getQualifiedName();
        Resource resParts = new Resource(javaTypeName, ResourceType.JAVA);
        if (javaType instanceof JavaSource) {
            TestJavaSourceVisitor visitor = new TestJavaSourceVisitor((JavaSource) javaType, resParts);
            visitor.visit((JavaSource) javaType);
            addReferencedResourcesToIndexBuilder(builder, visitor);
        }
        builder.addGenerator(resParts);
    }
    return builder;
}
Also used : Resource(org.kie.workbench.common.services.refactoring.Resource) JavaSource(org.jboss.forge.roaster.model.source.JavaSource) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder)

Aggregations

JavaSource (org.jboss.forge.roaster.model.source.JavaSource)5 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)3 ArrayList (java.util.ArrayList)2 FieldSource (org.jboss.forge.roaster.model.source.FieldSource)2 Import (org.jboss.forge.roaster.model.source.Import)2 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)2 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)2 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)2 Resource (org.kie.workbench.common.services.refactoring.Resource)2 DefaultIndexBuilder (org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Package (org.guvnor.common.services.project.model.Package)1 Method (org.jboss.forge.roaster.model.Method)1 AnnotationSource (org.jboss.forge.roaster.model.source.AnnotationSource)1 GenerationContext (org.kie.workbench.common.services.datamodeller.codegen.GenerationContext)1 GenerationEngine (org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine)1 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)1 Visibility (org.kie.workbench.common.services.datamodeller.core.Visibility)1 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)1