Search in sources :

Example 1 with MorphBase

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

the class MainProcessor method processDefinitions.

private boolean processDefinitions(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 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 {
            Class<?> graphClass = 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().getGraphFactoryFieldNames().put(defintionClassName, fqcn);
        // PropertySets fields.
        Map<String, Element> propertySetElements = getFieldNames(classElement, ANNOTATION_PROPERTY_SET);
        if (null != propertySetElements && !propertySetElements.isEmpty()) {
            processingContext.getPropertySetElements().addAll(propertySetElements.values());
            processingContext.getDefinitionAnnotations().getPropertySetFieldNames().put(defintionClassName, new LinkedHashSet<>(propertySetElements.keySet()));
        } else {
            note("Definition for tye [" + defintionClassName + "] have no Property Set members.");
        }
        // Properties fields.
        Map<String, Element> propertyElements = getFieldNames(classElement, ANNOTATION_PROPERTY);
        if (null != propertyElements && !propertyElements.isEmpty()) {
            processingContext.getPropertyElements().addAll(propertyElements.values());
            processingContext.getDefinitionAnnotations().getPropertyFieldNames().put(defintionClassName, new LinkedHashSet<>(propertyElements.keySet()));
        } else {
            note("Definition for tye [" + defintionClassName + "] have no Property members.");
        }
        // -- 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 {
                    Class<?> defaultTypeClass = morphBaseAnn.defaultType();
                } catch (MirroredTypeException mte) {
                    morphDefaultTypeMirror = mte.getTypeMirror();
                }
                if (null == morphDefaultTypeMirror) {
                    throw new RuntimeException("No default type class specifyed for the @MorphBase.");
                }
                String morphDefaultTypeClassName = morphDefaultTypeMirror.toString();
                processingContext.getMorphingAnnotations().getBaseDefaultTypes().put(morphBaseClassName, morphDefaultTypeClassName);
                // MorphBase - targets
                List<? extends TypeMirror> morphTargetMirrors = null;
                try {
                    Class<?>[] defsClasses = 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 {
                Class<?> defaultTypeClass = morphAnn.base();
            } catch (MirroredTypeException mte) {
                morphBaseTypeMirror = mte.getTypeMirror();
            }
            if (null == morphBaseTypeMirror) {
                throw new RuntimeException("No base type class specifyed for the @MorphBase.");
            }
            String morphBaseTypeClassName = morphBaseTypeMirror.toString();
            Set<String> currentTargets = processingContext.getMorphingAnnotations().getBaseTargets().get(morphBaseTypeClassName);
            if (null == currentTargets) {
                currentTargets = new LinkedHashSet<>();
                processingContext.getMorphingAnnotations().getBaseTargets().put(morphBaseTypeClassName, currentTargets);
            }
            currentTargets.add(defintionClassName);
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MirroredTypesException(javax.lang.model.type.MirroredTypesException) 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) Definition(org.kie.workbench.common.stunner.core.definition.annotation.Definition) MorphBase(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphBase) MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeMirror(javax.lang.model.type.TypeMirror) Morph(org.kie.workbench.common.stunner.core.definition.annotation.morph.Morph) PackageElement(javax.lang.model.element.PackageElement)

Example 2 with MorphBase

use of org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphBase 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)

Aggregations

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