Search in sources :

Example 11 with AnnotationSource

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

the class DefaultJavaRoasterModelAnnotationDriver method parseAnnotationValue.

private Object parseAnnotationValue(AnnotationSource javaAnnotationToken, AnnotationValuePairDefinition valuePairDefinition) throws ModelDriverException {
    String value = javaAnnotationToken.getLiteralValue(valuePairDefinition.getName());
    AnnotationDefinition annotationDefinition = valuePairDefinition.getAnnotationDefinition();
    Object result = null;
    if (value == null)
        return null;
    if (annotationDefinition == null) {
        return value;
    }
    if (valuePairDefinition.isArray()) {
        AnnotationSource[] javaAnnotationTokenValueArray = javaAnnotationToken.getAnnotationArrayValue(valuePairDefinition.getName());
        List<Annotation> annotationList = new ArrayList<Annotation>();
        Annotation annotation;
        if (javaAnnotationTokenValueArray != null) {
            for (int i = 0; i < javaAnnotationTokenValueArray.length; i++) {
                annotation = buildAnnotation(annotationDefinition, javaAnnotationTokenValueArray[i]);
                if (annotation != null) {
                    annotationList.add(annotation);
                }
            }
        }
        result = annotationList.size() > 0 ? annotationList : null;
    } else {
        AnnotationSource javaAnnotationTokenValue = javaAnnotationToken.getAnnotationValue(valuePairDefinition.getName());
        if (javaAnnotationTokenValue != null) {
            result = buildAnnotation(annotationDefinition, javaAnnotationTokenValue);
        }
    }
    return result;
}
Also used : AnnotationDefinition(org.kie.workbench.common.services.datamodeller.core.AnnotationDefinition) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) ArrayList(java.util.ArrayList) Annotation(org.kie.workbench.common.services.datamodeller.core.Annotation)

Example 12 with AnnotationSource

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

the class DefaultJavaRoasterModelAnnotationDriver method buildAnnotation.

@Override
public Annotation buildAnnotation(AnnotationDefinition annotationDefinition, Object annotationToken) throws ModelDriverException {
    AnnotationSource javaAnnotationToken = (AnnotationSource) annotationToken;
    AnnotationImpl annotation = new AnnotationImpl(annotationDefinition);
    if (annotationDefinition.isMarker()) {
        return annotation;
    } else {
        if (javaAnnotationToken.getValues() != null) {
            List<ValuePair> values = javaAnnotationToken.getValues();
            if (values != null && values.size() > 0) {
                for (AnnotationValuePairDefinition valuePairDefinition : annotationDefinition.getValuePairs()) {
                    Object annotationValue = buildAnnotationValue(javaAnnotationToken, valuePairDefinition);
                    if (annotationValue != null) {
                        annotation.setValue(valuePairDefinition.getName(), annotationValue);
                    }
                }
            }
        }
    }
    return annotation;
}
Also used : ValuePair(org.jboss.forge.roaster.model.ValuePair) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) AnnotationImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl) AnnotationValuePairDefinition(org.kie.workbench.common.services.datamodeller.core.AnnotationValuePairDefinition)

Example 13 with AnnotationSource

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

the class JavaRoasterModelDriver method addAnnotationMemberValue.

private void addAnnotationMemberValue(AnnotationSource annotationSource, AnnotationValuePairDefinition valuePairDefinition, Annotation annotation) {
    AnnotationSource targetAnnotation = annotationSource.addAnnotationValue(valuePairDefinition.getName());
    targetAnnotation.setName(annotation.getClassName());
    if (!annotation.getAnnotationDefinition().isMarker()) {
        for (AnnotationValuePairDefinition memberDefinition : annotation.getAnnotationDefinition().getValuePairs()) {
            Object value = annotation.getValue(memberDefinition.getName());
            if (value != null) {
                addMemberValue(targetAnnotation, memberDefinition, value);
            }
        }
    }
}
Also used : AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) AnnotationValuePairDefinition(org.kie.workbench.common.services.datamodeller.core.AnnotationValuePairDefinition)

Example 14 with AnnotationSource

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

the class JavaRoasterModelDriver method parseAnnotationWithValuePair.

public Pair<Annotation, List<DriverError>> parseAnnotationWithValuePair(String annotationClassName, ElementType target, String valuePairName, String literalValue, ClassLoader classLoader) {
    List<DriverError> driverErrors = new ArrayList<DriverError>();
    Annotation annotation = null;
    Pair<AnnotationSource<JavaClassSource>, List<DriverError>> parseResult = parseAnnotationWithValuePair(annotationClassName, target, valuePairName, literalValue);
    driverErrors.addAll(parseResult.getK2());
    if (driverErrors.size() == 0) {
        // TODO review this, we should use DriverUtils.createClassTypeResolver( javaClassSource, classLoader ); instead
        ClassTypeResolver classTypeResolver = new ClassTypeResolver(Collections.emptySet(), classLoader);
        try {
            annotation = createAnnotation(parseResult.getK1(), classTypeResolver);
        } catch (Exception e) {
            driverErrors.add(new DriverError(e.getMessage()));
        }
    }
    return new Pair<>(annotation, driverErrors);
}
Also used : ArrayList(java.util.ArrayList) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) List(java.util.List) ArrayList(java.util.ArrayList) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) Annotation(org.kie.workbench.common.services.datamodeller.core.Annotation) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) IOException(java.io.IOException) ParserException(org.jboss.forge.roaster.ParserException) DriverError(org.kie.workbench.common.services.datamodeller.driver.model.DriverError) Pair(org.uberfire.commons.data.Pair)

Example 15 with AnnotationSource

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

Aggregations

AnnotationSource (org.jboss.forge.roaster.model.source.AnnotationSource)17 ArrayList (java.util.ArrayList)11 List (java.util.List)8 Type (org.jboss.forge.roaster.model.Type)6 IOException (java.io.IOException)4 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)4 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3 Modifier (java.lang.reflect.Modifier)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 URLClassLoader (java.net.URLClassLoader)3