Search in sources :

Example 61 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processConnectionRules.

private boolean processConnectionRules(final Element element) {
    final Messager messager = processingEnv.getMessager();
    final boolean isIface = element.getKind() == ElementKind.INTERFACE;
    final boolean isClass = element.getKind() == ElementKind.CLASS;
    if (isIface || isClass) {
        TypeElement classElement = (TypeElement) element;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered connection rule for class [" + classElement.getSimpleName() + "]");
        final String classNameActivity = classElement.getSimpleName() + RULE_CONNECTION_SUFFIX_CLASSNAME;
        generateRuleCode(connectionRuleGenerator, messager, classElement, packageElement, classNameActivity);
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Example 62 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processContainmentRules.

private boolean processContainmentRules(final Element e) {
    final Messager messager = processingEnv.getMessager();
    final boolean isIface = e.getKind() == ElementKind.INTERFACE;
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isIface || isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered containment rule for class [" + classElement.getSimpleName() + "]");
        final String classNameActivity = classElement.getSimpleName() + RULE_CONTAINMENT_SUFFIX_CLASSNAME;
        generateRuleCode(containmentRuleGenerator, messager, classElement, packageElement, classNameActivity);
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Example 63 with PackageElement

use of javax.lang.model.element.PackageElement 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 64 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processEdgeCardinalityRules.

private boolean processEdgeCardinalityRules(final Element e) {
    final Messager messager = processingEnv.getMessager();
    final boolean isIface = e.getKind() == ElementKind.INTERFACE;
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isIface || isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered edge cardinality rule for class [" + classElement.getSimpleName() + "]");
        final String classNameActivity = classElement.getSimpleName() + RULE_EDGE_CARDINALITY_SUFFIX_CLASSNAME;
        generateRuleCode(edgeCardinalityRuleGenerator, messager, classElement, packageElement, classNameActivity);
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Example 65 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processRuleExtension.

private boolean processRuleExtension(final Element e) {
    final Messager messager = processingEnv.getMessager();
    final boolean isIface = e.getKind() == ElementKind.INTERFACE;
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isIface || isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE, "Discovered rule extension for class [" + classElement.getSimpleName() + "]");
        final String classNameActivity = classElement.getSimpleName() + RULE_EXTENSION_SUFFIX_CLASSNAME;
        generateRuleCode(extensionRuleGenerator, messager, classElement, packageElement, classNameActivity);
    }
    return true;
}
Also used : Messager(javax.annotation.processing.Messager) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)134 TypeElement (javax.lang.model.element.TypeElement)93 Element (javax.lang.model.element.Element)52 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8