Example usage for javax.lang.model.element TypeElement getEnclosingElement

List of usage examples for javax.lang.model.element TypeElement getEnclosingElement

Introduction

In this page you can find the example usage for javax.lang.model.element TypeElement getEnclosingElement.

Prototype

@Override
Element getEnclosingElement();

Source Link

Document

Returns the package of a top-level type and returns the immediately lexically enclosing element for a NestingKind#isNested nested type.

Usage

From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java

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");
            }/*from  w  w w.jav a2s.c o m*/
        }
        // Value fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_VALUE,
                processingContext.getPropertyAnnotations().getValueFieldNames(), true);
        // Default Value fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_DEFAULT_VALUE,
                processingContext.getPropertyAnnotations().getDefaultValueFieldNames(), 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(), true);
        // Description fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_DESCRIPTION,
                processingContext.getPropertyAnnotations().getDescriptionFieldNames(), true);
        // Type fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_TYPE,
                processingContext.getPropertyAnnotations().getTypeFieldNames(), true);
        // Read only fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_READONLY,
                processingContext.getPropertyAnnotations().getReadOnlyFieldNames(), true);
        // Optional fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_PROPERTY_OPTIONAL,
                processingContext.getPropertyAnnotations().getOptionalFieldNames(), true);
    }
    return false;
}

From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java

private boolean processDefinitionSets(final Set<? extends TypeElement> set, final Element e,
        final RoundEnvironment roundEnv) throws Exception {
    final Messager messager = processingEnv.getMessager();
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        messager.printMessage(Diagnostic.Kind.NOTE,
                "Discovered definition set class [" + classElement.getSimpleName() + "]");
        final String packageName = packageElement.getQualifiedName().toString();
        final String className = classElement.getSimpleName().toString();
        processingContext.setDefinitionSet(packageName, className);
        String defSetClassName = packageName + "." + className;
        // Description fields.
        processFieldName(classElement, defSetClassName, ANNOTATION_DESCRIPTION,
                processingContext.getDefSetAnnotations().getDescriptionFieldNames(), true);
        // Definitions identifiers.
        DefinitionSet definitionSetAnn = e.getAnnotation(DefinitionSet.class);
        List<? extends TypeMirror> mirrors = null;
        try {/* www.j  a v a  2s  . c o m*/
            Class<?>[] defsClasses = definitionSetAnn.definitions();
        } catch (MirroredTypesException mte) {
            mirrors = mte.getTypeMirrors();
        }
        if (null == mirrors) {
            throw new RuntimeException("No graph class class specifyed for the @DefinitionSet.");
        }
        Set<String> defIds = new LinkedHashSet<>();
        for (TypeMirror mirror : mirrors) {
            if (mirror.getKind().equals(TypeKind.DECLARED)) {
                final TypeElement t = (TypeElement) ((DeclaredType) mirror).asElement();
                processingContext.getDefinitionElements().add(t);
            }
            String fqcn = mirror.toString();
            defIds.add(fqcn);
        }
        processingContext.getDefSetAnnotations().getDefinitionIds().addAll(defIds);
        // Builder class.
        processDefinitionSetModelBuilder(e, defSetClassName,
                processingContext.getDefSetAnnotations().getBuilderFieldNames());
        // Graph factory type.
        TypeMirror mirror = null;
        try {
            Class<?> graphClass = definitionSetAnn.graphFactory();
        } catch (MirroredTypeException mte) {
            mirror = mte.getTypeMirror();
        }
        if (null == mirror) {
            throw new RuntimeException("No graph factory class specifyed for the @DefinitionSet.");
        }
        String fqcn = mirror.toString();
        processingContext.getDefSetAnnotations().getGraphFactoryTypes().put(defSetClassName, fqcn);
        // Definition Set's qualifier.
        try {
            Class<?> qualifierClass = definitionSetAnn.qualifier();
        } catch (MirroredTypeException mte) {
            mirror = mte.getTypeMirror();
        }
        if (null == mirror) {
            throw new RuntimeException("No qualifier class specifyed for the @DefinitionSet.");
        }
        processingContext.getDefSetAnnotations().getQualifiers().put(defSetClassName, mirror.toString());
    }
    return true;
}

From source file:org.kie.workbench.common.stunner.core.processors.MainProcessor.java

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);
        }//from   w  w w .j  a  v  a 2s  .c  om
        // Category fields.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_CATEGORY,
                processingContext.getDefinitionAnnotations().getCategoryFieldNames(), true);
        // Title fields.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_TITLE,
                processingContext.getDefinitionAnnotations().getTitleFieldNames(), true);
        // Description fields.
        processFieldName(classElement, defintionClassName, ANNOTATION_DESCRIPTION,
                processingContext.getDefinitionAnnotations().getDescriptionFieldNames(), true);
        // Labels fields.
        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;
}

From source file:org.kie.workbench.common.forms.adf.processors.FormDefinitionsProcessor.java

protected void processFormDefinition(TypeElement formElement) throws Exception {
    final Messager messager = processingEnv.getMessager();

    messager.printMessage(Diagnostic.Kind.NOTE,
            "Discovered FormDefintion class [" + formElement.getSimpleName() + "]");

    boolean checkInheritance = false;

    FormDefinition defintion = formElement.getAnnotation(FormDefinition.class);

    checkInheritance = defintion.allowInheritance();

    List<Map<String, String>> formElements = new ArrayList<>();

    if (checkInheritance) {
        TypeElement parent = getParent(formElement);
        formElements.addAll(extractParentFormFields(parent, defintion.policy(), defintion.i18n()));
    }/*from ww w .ja  va  2  s .com*/

    formElements.addAll(extracFormFields(formElement, defintion.policy(), defintion.i18n()));

    FormGenerationUtils.sort(defintion.startElement(), formElements);

    messager.printMessage(Diagnostic.Kind.NOTE, "Discovered " + formElements.size() + " elements for form ["
            + formElement.getQualifiedName().toString() + "]");

    String modelClassName = formElement.getQualifiedName().toString();
    String builderClassName = fixClassName(formElement.getQualifiedName().toString()) + "FormBuilder";

    Map<String, Object> templateContext = new HashMap<>();
    templateContext.put("modelClass", modelClassName);
    templateContext.put("builderClassName", builderClassName);
    templateContext.put("startElement", defintion.startElement());

    templateContext.put("i18n_bundle",
            StringUtils.isEmpty(defintion.i18n().bundle()) ? formElement.asType().toString()
                    : defintion.i18n().bundle());

    Column[] columns = defintion.layout().value();

    List<String> layoutColumns = new ArrayList<>();
    if (columns.length == 0) {
        layoutColumns.add(ColSpan.SPAN_12.getName());
    } else {
        for (Column column : columns) {
            layoutColumns.add(column.value().getName());
        }
    }

    templateContext.put("layout_columns", layoutColumns);

    templateContext.put("elements", formElements);

    StringBuffer builder = writeTemplate("templates/FormDefinitionSettingsBuilder.ftl", templateContext);

    Map<String, String> form = new HashMap<>();

    form.put("package", ((PackageElement) formElement.getEnclosingElement()).getQualifiedName().toString());
    form.put("modelClass", modelClassName);
    form.put("builderClass", builderClassName);
    form.put("builderCode", builder.toString());

    context.getForms().add(form);
}