Search in sources :

Example 1 with Property

use of org.kie.workbench.common.stunner.core.definition.annotation.Property in project kie-wb-common by kiegroup.

the class MainProcessor method processDefinitions.

private boolean processDefinitions(final Element e) {
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        String defintionClassName = packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName();
        Map<String, String> baseTypes = processingContext.getDefinitionAnnotations().getBaseTypes();
        TypeElement parentElement = getDefinitionInheritedType(classElement);
        if (null != parentElement && !baseTypes.containsKey(defintionClassName)) {
            PackageElement basePackageElement = (PackageElement) parentElement.getEnclosingElement();
            String baseClassName = basePackageElement.getQualifiedName().toString() + "." + parentElement.getSimpleName();
            baseTypes.put(defintionClassName, baseClassName);
        }
        // Id field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_ID, processingContext.getDefinitionAnnotations().getIdFieldNames(), false);
        // Category field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_CATEGORY, processingContext.getDefinitionAnnotations().getCategoryFieldNames(), true);
        // Title field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_TITLE, processingContext.getDefinitionAnnotations().getTitleFieldNames(), false);
        // Description field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DESCRIPTION, processingContext.getDefinitionAnnotations().getDescriptionFieldNames(), false);
        // Labels field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_LABELS, processingContext.getDefinitionAnnotations().getLabelsFieldNames(), true);
        // Builder class.
        processDefinitionModelBuilder(e, defintionClassName, processingContext.getDefinitionAnnotations().getBuilderFieldNames());
        // Graph element.
        Definition definitionAnn = e.getAnnotation(Definition.class);
        TypeMirror mirror = null;
        try {
            definitionAnn.graphFactory();
        } catch (MirroredTypeException mte) {
            mirror = mte.getTypeMirror();
        }
        if (null == mirror) {
            throw new RuntimeException("No graph factory class specified for the @Definition.");
        }
        String fqcn = mirror.toString();
        processingContext.getDefinitionAnnotations().getGraphFactory().put(defintionClassName, fqcn);
        // Properties fields.
        Map<String, VariableElement> propertyFields = visitVariables("", classElement, variableElement -> hasAnnotation(variableElement, ANNOTATION_PROPERTY));
        List<String> propertyFieldNames = new ArrayList<>();
        List<Boolean> typedPropertyFields = new ArrayList<>();
        DefinitionAdapterBindings.PropertyMetaTypes defMetaTypes = new DefinitionAdapterBindings.PropertyMetaTypes();
        propertyFields.forEach((field, variable) -> {
            TypeMirror type = variable.asType();
            TypeElement element = (TypeElement) ((DeclaredType) type).asElement();
            String elementClassName = GeneratorUtils.getTypeMirrorDeclaredName(element.asType());
            boolean isTypedProperty = processingContext.getPropertyAnnotations().getValueFieldNames().containsKey(elementClassName);
            int index = propertyFieldNames.size();
            Property propertyAnnotation = variable.getAnnotation(Property.class);
            // Populate the context collections.
            propertyFieldNames.add(field);
            typedPropertyFields.add(isTypedProperty);
            org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes propertyMetaType = propertyAnnotation.meta();
            if (org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes.NONE.equals(propertyMetaType)) {
                propertyMetaType = getDeclaredMetaType(elementClassName);
            }
            if (null != propertyMetaType) {
                defMetaTypes.setIndex(propertyMetaType, index);
            }
        });
        processingContext.getDefinitionAnnotations().getPropertyFieldNames().put(defintionClassName, propertyFieldNames);
        processingContext.getDefinitionAnnotations().getTypedPropertyFields().put(defintionClassName, typedPropertyFields);
        processingContext.getMetaPropertyTypesFields().put(defintionClassName, defMetaTypes);
        // -- Morphing annotations --
        MorphBase morphBaseAnn = e.getAnnotation(MorphBase.class);
        Morph morphAnn = e.getAnnotation(Morph.class);
        if (null != morphBaseAnn && null != morphAnn) {
            TypeElement superElement = getAnnotationInTypeInheritance(classElement, MorphBase.class.getName());
            final String packageName = packageElement.getQualifiedName().toString();
            String morphBaseClassName = packageName + "." + superElement.getSimpleName().toString();
            Map<String, String> defaultTypesMap = processingContext.getMorphingAnnotations().getBaseDefaultTypes();
            if (null == defaultTypesMap.get(morphBaseClassName)) {
                TypeMirror morphDefaultTypeMirror = null;
                try {
                    morphBaseAnn.defaultType();
                } catch (MirroredTypeException mte) {
                    morphDefaultTypeMirror = mte.getTypeMirror();
                }
                if (null == morphDefaultTypeMirror) {
                    throw new RuntimeException("No default type class declared for the @MorphBase.");
                }
                String morphDefaultTypeClassName = morphDefaultTypeMirror.toString();
                processingContext.getMorphingAnnotations().getBaseDefaultTypes().put(morphBaseClassName, morphDefaultTypeClassName);
                // MorphBase - targets
                List<? extends TypeMirror> morphTargetMirrors = null;
                try {
                    morphBaseAnn.targets();
                } catch (MirroredTypesException mte) {
                    morphTargetMirrors = mte.getTypeMirrors();
                }
                if (null != morphTargetMirrors) {
                    Set<String> morphTargetMirrorClasses = new LinkedHashSet<>();
                    for (TypeMirror morphTargetMirror : morphTargetMirrors) {
                        String morphTargetMirrorClassName = morphTargetMirror.toString();
                        morphTargetMirrorClasses.add(morphTargetMirrorClassName);
                    }
                    processingContext.getMorphingAnnotations().getBaseTargets().put(morphBaseClassName, morphTargetMirrorClasses);
                }
                // Morph Properties.
                processMorphProperties(superElement, morphBaseClassName);
            }
            TypeMirror morphBaseTypeMirror = null;
            try {
                morphAnn.base();
            } catch (MirroredTypeException mte) {
                morphBaseTypeMirror = mte.getTypeMirror();
            }
            if (null == morphBaseTypeMirror) {
                throw new RuntimeException("No base type class declared for the @MorphBase.");
            }
            String morphBaseTypeClassName = morphBaseTypeMirror.toString();
            Set<String> currentTargets = processingContext.getMorphingAnnotations().getBaseTargets().computeIfAbsent(morphBaseTypeClassName, k -> new LinkedHashSet<>());
            currentTargets.add(defintionClassName);
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MirroredTypesException(javax.lang.model.type.MirroredTypesException) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeMirror(javax.lang.model.type.TypeMirror) Morph(org.kie.workbench.common.stunner.core.definition.annotation.morph.Morph) MorphProperty(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphProperty) Property(org.kie.workbench.common.stunner.core.definition.annotation.Property) TypeElement(javax.lang.model.element.TypeElement) Definition(org.kie.workbench.common.stunner.core.definition.annotation.Definition) MorphBase(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphBase) PackageElement(javax.lang.model.element.PackageElement) DefinitionAdapterBindings(org.kie.workbench.common.stunner.core.definition.adapter.binding.DefinitionAdapterBindings)

Example 2 with Property

use of org.kie.workbench.common.stunner.core.definition.annotation.Property in project kie-wb-common by kiegroup.

the class MainProcessor method processProperties.

private boolean processProperties(final Element e) {
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        String propertyClassName = packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName();
        // Meta-properties
        Property metaProperty = e.getAnnotation(Property.class);
        if (null != metaProperty) {
            org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes type = metaProperty.meta();
            if (!org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes.NONE.equals(type)) {
                processingContext.getMetaPropertyTypes().put(type, propertyClassName + ".class");
            }
        }
        // Value fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_VALUE, processingContext.getPropertyAnnotations().getValueFieldNames(), true);
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) MorphProperty(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphProperty) Property(org.kie.workbench.common.stunner.core.definition.annotation.Property)

Example 3 with Property

use of org.kie.workbench.common.stunner.core.definition.annotation.Property in project kie-wb-common by kiegroup.

the class MainProcessor method processProperties.

private boolean processProperties(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception {
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        String propertyClassName = packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName();
        // Meta-properties
        Property metaProperty = e.getAnnotation(Property.class);
        if (null != metaProperty) {
            PropertyMetaTypes type = metaProperty.meta();
            if (!PropertyMetaTypes.NONE.equals(type)) {
                processingContext.getMetaPropertyTypes().put(type, propertyClassName + ".class");
            }
        }
        // Value fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_VALUE, processingContext.getPropertyAnnotations().getValueFieldNames(), true);
        // Allowed Values fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_ALLOWED_VALUES, processingContext.getPropertyAnnotations().getAllowedValuesFieldNames(), false);
        // Caption fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_CAPTION, processingContext.getPropertyAnnotations().getCaptionFieldNames(), false);
        // Description fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_DESCRIPTION, processingContext.getPropertyAnnotations().getDescriptionFieldNames(), false);
        // Property Type field.
        final boolean propTypeAnnFound = processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_TYPE, processingContext.getPropertyAnnotations().getTypeFieldNames(), false);
        // the value object.
        if (!propTypeAnnFound) {
            Map<String, Element> fieldNames = getFieldNames(classElement, ANNOTATION_PROPERTY_VALUE);
            if (!fieldNames.isEmpty()) {
                final TypeElement te = (TypeElement) fieldNames.values().iterator().next();
                final String type = te.getQualifiedName().toString();
                final Class<?> typeClass = Class.forName(type);
                final Class<? extends PropertyType> defaultPropertyType = DefinitionUtils.getDefaultPropertyType(typeClass);
                if (null != defaultPropertyType) {
                    processingContext.getPropertyAnnotations().getTypes().put(propertyClassName, defaultPropertyType.getName());
                } else {
                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "No property type specified for class " + propertyClassName, classElement);
                }
            }
        }
        // Read only fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_READONLY, processingContext.getPropertyAnnotations().getReadOnlyFieldNames(), false);
        // Optional fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_OPTIONAL, processingContext.getPropertyAnnotations().getOptionalFieldNames(), false);
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) PropertyMetaTypes(org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes) PackageElement(javax.lang.model.element.PackageElement) MorphProperty(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphProperty) Property(org.kie.workbench.common.stunner.core.definition.annotation.Property)

Example 4 with Property

use of org.kie.workbench.common.stunner.core.definition.annotation.Property in project kie-wb-common by kiegroup.

the class BackendPropertySetAdapter method getProperties.

@Override
public Set<?> getProperties(final T propertySet) {
    Set<Object> result = null;
    if (null != propertySet) {
        Field[] fields = propertySet.getClass().getDeclaredFields();
        if (null != fields) {
            result = new HashSet<>();
            for (Field field : fields) {
                Property annotation = field.getAnnotation(Property.class);
                if (null != annotation) {
                    try {
                        field.setAccessible(true);
                        Object property = field.get(propertySet);
                        result.add(property);
                    } catch (Exception e) {
                        LOG.error("Error obtaining annotated properties for T with id " + getId(propertySet));
                    }
                }
            }
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) Property(org.kie.workbench.common.stunner.core.definition.annotation.Property)

Aggregations

Property (org.kie.workbench.common.stunner.core.definition.annotation.Property)4 PackageElement (javax.lang.model.element.PackageElement)3 TypeElement (javax.lang.model.element.TypeElement)3 MorphProperty (org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphProperty)3 VariableElement (javax.lang.model.element.VariableElement)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Element (javax.lang.model.element.Element)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1 MirroredTypesException (javax.lang.model.type.MirroredTypesException)1 TypeMirror (javax.lang.model.type.TypeMirror)1 DefinitionAdapterBindings (org.kie.workbench.common.stunner.core.definition.adapter.binding.DefinitionAdapterBindings)1 Definition (org.kie.workbench.common.stunner.core.definition.annotation.Definition)1 Morph (org.kie.workbench.common.stunner.core.definition.annotation.morph.Morph)1 MorphBase (org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphBase)1 PropertyMetaTypes (org.kie.workbench.common.stunner.core.definition.property.PropertyMetaTypes)1