Search in sources :

Example 86 with Elements

use of javax.lang.model.util.Elements in project neo4j by neo4j.

the class ProcedureProcessor method init.

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    Types typeUtils = processingEnv.getTypeUtils();
    Elements elementUtils = processingEnv.getElementUtils();
    visitedProcedures.clear();
    messagePrinter = new MessagePrinter(processingEnv.getMessager());
    visitor = new StoredProcedureVisitor(typeUtils, elementUtils, processingEnv.getOptions().containsKey(IGNORE_CONTEXT_WARNINGS));
    duplicationPredicate = new DuplicatedProcedureValidator<>(elementUtils, sprocType, ProcedureProcessor::getCustomName);
}
Also used : Types(javax.lang.model.util.Types) MessagePrinter(org.neo4j.tooling.procedure.messages.MessagePrinter) StoredProcedureVisitor(org.neo4j.tooling.procedure.visitors.StoredProcedureVisitor) Elements(javax.lang.model.util.Elements)

Example 87 with Elements

use of javax.lang.model.util.Elements in project neo4j by neo4j.

the class UserFunctionProcessor method init.

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    Types typeUtils = processingEnv.getTypeUtils();
    Elements elementUtils = processingEnv.getElementUtils();
    visitedFunctions.clear();
    messagePrinter = new MessagePrinter(processingEnv.getMessager());
    visitor = new UserFunctionVisitor(typeUtils, elementUtils, new TypeMirrorUtils(typeUtils, elementUtils));
    duplicationPredicate = new DuplicatedProcedureValidator<>(elementUtils, userFunctionType, UserFunctionProcessor::getCustomName);
}
Also used : Types(javax.lang.model.util.Types) UserFunctionVisitor(org.neo4j.tooling.procedure.visitors.UserFunctionVisitor) MessagePrinter(org.neo4j.tooling.procedure.messages.MessagePrinter) Elements(javax.lang.model.util.Elements) TypeMirrorUtils(org.neo4j.tooling.procedure.compilerutils.TypeMirrorUtils)

Example 88 with Elements

use of javax.lang.model.util.Elements in project logging-log4j2 by apache.

the class PluginProcessor method collectPlugins.

private void collectPlugins(final Iterable<? extends Element> elements) {
    final Elements elementUtils = processingEnv.getElementUtils();
    final ElementVisitor<PluginEntry, Plugin> pluginVisitor = new PluginElementVisitor(elementUtils);
    final ElementVisitor<Collection<PluginEntry>, Plugin> pluginAliasesVisitor = new PluginAliasesElementVisitor(elementUtils);
    for (final Element element : elements) {
        final Plugin plugin = element.getAnnotation(Plugin.class);
        if (plugin == null) {
            continue;
        }
        final PluginEntry entry = element.accept(pluginVisitor, plugin);
        final Map<String, PluginEntry> category = pluginCache.getCategory(entry.getCategory());
        category.put(entry.getKey(), entry);
        final Collection<PluginEntry> entries = element.accept(pluginAliasesVisitor, plugin);
        for (final PluginEntry pluginEntry : entries) {
            category.put(pluginEntry.getKey(), pluginEntry);
        }
    }
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Collection(java.util.Collection) Elements(javax.lang.model.util.Elements) Plugin(org.apache.logging.log4j.core.config.plugins.Plugin)

Example 89 with Elements

use of javax.lang.model.util.Elements in project react4j by react4j.

the class ReactProcessor method determineLifecycleMethods.

private void determineLifecycleMethods(@Nonnull final TypeElement typeElement, @Nonnull final ComponentDescriptor descriptor) {
    /*
     * Get the list of lifecycle methods that have been overridden by typeElement
     * a parent class, or by a default method method implemented by typeElement or
     * a parent class.
     */
    final Collection<ExecutableElement> lifecycleMethods = getComponentLifecycleMethods().values();
    final Elements elementUtils = processingEnv.getElementUtils();
    final Types typeUtils = processingEnv.getTypeUtils();
    final TypeElement componentType = elementUtils.getTypeElement(Constants.COMPONENT_CLASSNAME);
    final List<MethodDescriptor> overriddenLifecycleMethods = // Get all methods on type parent classes, and default methods from interfaces
    ProcessorUtil.getMethods(typeElement, processingEnv.getTypeUtils()).stream().filter(m -> lifecycleMethods.stream().anyMatch(l -> elementUtils.overrides(m, l, typeElement))).filter(m -> m.getEnclosingElement() != componentType).map(m -> new MethodDescriptor(m, (ExecutableType) typeUtils.asMemberOf(descriptor.getDeclaredType(), m))).collect(Collectors.toList());
    descriptor.setLifecycleMethods(overriddenLifecycleMethods);
}
Also used : Arrays(java.util.Arrays) PackageElement(javax.lang.model.element.PackageElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) SupportedSourceVersion(javax.annotation.processing.SupportedSourceVersion) Kind(javax.tools.Diagnostic.Kind) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter) ElementKind(javax.lang.model.element.ElementKind) ExecutableType(javax.lang.model.type.ExecutableType) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ExecutableElement(javax.lang.model.element.ExecutableElement) Set(java.util.Set) IOException(java.io.IOException) Element(javax.lang.model.element.Element) TypeSpec(com.squareup.javapoet.TypeSpec) Processor(javax.annotation.processing.Processor) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) JavaFile(com.squareup.javapoet.JavaFile) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) AutoService(com.google.auto.service.AutoService) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements)

Example 90 with Elements

use of javax.lang.model.util.Elements in project kie-wb-common by kiegroup.

the class FormDefinitionsProcessor method extracFormFields.

private List<Map<String, String>> extracFormFields(TypeElement type, FieldPolicy policy, I18nSettings i18nSettings) throws Exception {
    final Elements elementUtils = processingEnv.getElementUtils();
    Collection<FieldInfo> fieldInfos = extractFieldInfos(type, fieldElement -> {
        if (policy.equals(FieldPolicy.ALL)) {
            AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldElement, SkipFormField.class.getName());
            if (annotation != null) {
                return false;
            }
        } else {
            AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldElement, FormField.class.getName());
            if (annotation == null) {
                return false;
            }
        }
        return true;
    });
    List<Map<String, String>> elementsSettings = new ArrayList<>();
    for (FieldInfo fieldInfo : fieldInfos) {
        if (fieldInfo.getter != null && fieldInfo.setter != null) {
            String fieldName = fieldInfo.fieldElement.getSimpleName().toString();
            String fieldLabel = fieldName;
            String helpMessage = "";
            String binding = fieldName;
            String methodName = "getFormElement_" + fieldName;
            Map<String, Object> elementContext = new HashMap<>();
            boolean isList = false;
            org.kie.workbench.common.forms.model.TypeKind typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE;
            boolean overrideI18n = false;
            TypeMirror finalType = fieldInfo.fieldElement.asType();
            String fieldModifier = "";
            if (finalType instanceof DeclaredType) {
                Element finalTypeElement = processingEnv.getTypeUtils().asElement(finalType);
                if (finalTypeElement.getKind().equals(ElementKind.CLASS)) {
                    FieldDefinition fieldDefinitionAnnotation = finalTypeElement.getAnnotation(FieldDefinition.class);
                    if (fieldDefinitionAnnotation != null) {
                        // Override the using the i18n mechanism
                        if (fieldDefinitionAnnotation.i18nMode().equals(I18nMode.OVERRIDE_I18N_KEY)) {
                            fieldLabel = finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.labelKeySuffix();
                            Collection<FieldInfo> labelInfos = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null);
                            if (labelInfos != null && labelInfos.size() == 1) {
                                FieldInfo labelInfo = labelInfos.iterator().next();
                                fieldLabel = finalType.toString() + i18nSettings.separator() + labelInfo.fieldElement.getSimpleName();
                            }
                            helpMessage = finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.helpMessageKeySuffix();
                            Collection<FieldInfo> helpMessages = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null);
                            if (helpMessages != null && helpMessages.size() == 1) {
                                FieldInfo helpInfo = helpMessages.iterator().next();
                                helpMessage = finalType.toString() + i18nSettings.separator() + helpInfo.fieldElement.getSimpleName();
                            }
                        }
                        Collection<FieldInfo> fieldValue = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldValue.class) != null);
                        if (fieldValue == null || fieldValue.size() != 1) {
                            throw new Exception("Problem processing FieldDefinition [" + finalType + "]: it should have one field marked as @FieldValue");
                        }
                        FieldInfo valueInfo = fieldValue.iterator().next();
                        binding += "." + valueInfo.getFieldElement().getSimpleName();
                        fieldModifier = fixClassName(finalType.toString()) + "_FieldStatusModifier";
                        finalType = valueInfo.getFieldElement().asType();
                        overrideI18n = !fieldDefinitionAnnotation.i18nMode().equals(I18nMode.DONT_OVERRIDE);
                    } else {
                        FormDefinition formDefinitionAnnotation = finalTypeElement.getAnnotation(FormDefinition.class);
                        if (formDefinitionAnnotation != null) {
                            fieldLabel = finalType.toString() + i18nSettings.separator() + FieldDefinition.LABEL;
                            Collection<FieldInfo> labelInfos = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null);
                            if (labelInfos != null && labelInfos.size() == 1) {
                                FieldInfo labelInfo = labelInfos.iterator().next();
                                fieldLabel = finalType.toString() + i18nSettings.separator() + labelInfo.fieldElement.getSimpleName();
                                overrideI18n = true;
                            }
                            helpMessage = finalType.toString() + i18nSettings.separator() + FieldDefinition.HELP_MESSAGE;
                            Collection<FieldInfo> helpMessages = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null);
                            if (helpMessages != null && helpMessages.size() == 1) {
                                FieldInfo helpInfo = helpMessages.iterator().next();
                                helpMessage = finalType.toString() + i18nSettings.separator() + helpInfo.fieldElement.getSimpleName();
                                overrideI18n = true;
                            }
                            typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT;
                        }
                    }
                }
                DeclaredType fieldType = (DeclaredType) finalType;
                if (processingEnv.getTypeUtils().isAssignable(fieldType.asElement().asType(), listType)) {
                    if (fieldType.getTypeArguments().size() != 1) {
                        throw new IllegalArgumentException("Impossible to generate a field for type " + fieldType.toString() + ". Type should have one and only one Type arguments.");
                    }
                    isList = true;
                    finalType = fieldType.getTypeArguments().get(0);
                    if (FormModelPropertiesUtil.isBaseType(finalType.toString())) {
                        typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE;
                    } else if (elementUtils.getTypeElement(finalType.toString()).getSuperclass().toString().startsWith("java.lang.Enum")) {
                        typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM;
                    } else {
                        typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT;
                    }
                } else if (elementUtils.getTypeElement(finalType.toString()).getSuperclass().toString().startsWith("java.lang.Enum")) {
                    typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM;
                }
            }
            elementContext.put("formModel", type.getQualifiedName().toString());
            elementContext.put("methodName", methodName);
            elementContext.put("fieldName", fieldName);
            elementContext.put("binding", binding);
            elementContext.put("type", typeKind.toString());
            elementContext.put("className", finalType.toString());
            elementContext.put("isList", String.valueOf(isList));
            elementContext.put("fieldModifier", fieldModifier);
            Map<String, String> params = new HashMap<>();
            elementContext.put("params", params);
            String afterElement = "";
            FormField settings = fieldInfo.fieldElement.getAnnotation(FormField.class);
            if (settings != null) {
                String typeName;
                try {
                    typeName = settings.type().getName();
                } catch (MirroredTypeException exception) {
                    typeName = exception.getTypeMirror().toString();
                }
                if (StringUtils.isEmpty(typeName)) {
                    typeName = FieldType.class.getName();
                }
                afterElement = settings.afterElement();
                elementContext.put("preferredType", typeName);
                if (!overrideI18n && !isEmpty(settings.labelKey())) {
                    fieldLabel = settings.labelKey();
                }
                if (!overrideI18n && !isEmpty(settings.helpMessageKey())) {
                    helpMessage = settings.helpMessageKey();
                }
                elementContext.put("required", Boolean.valueOf(settings.required()).toString());
                elementContext.put("readOnly", Boolean.valueOf(settings.readonly()).toString());
                for (FieldParam fieldParam : settings.settings()) {
                    params.put(fieldParam.name(), fieldParam.value());
                }
                elementContext.put("wrap", Boolean.valueOf(settings.layoutSettings().wrap()).toString());
                elementContext.put("horizontalSpan", String.valueOf(settings.layoutSettings().horizontalSpan()));
                elementContext.put("verticalSpan", String.valueOf(settings.layoutSettings().verticalSpan()));
            } else {
                elementContext.put("preferredType", FieldType.class.getName());
                elementContext.put("required", Boolean.FALSE.toString());
                elementContext.put("readOnly", Boolean.FALSE.toString());
                elementContext.put("wrap", Boolean.FALSE.toString());
                elementContext.put("horizontalSpan", "1");
                elementContext.put("verticalSpan", "1");
            }
            if (!overrideI18n) {
                if (!isEmpty(i18nSettings.keyPreffix())) {
                    fieldLabel = i18nSettings.keyPreffix() + i18nSettings.separator() + fieldLabel;
                    helpMessage = i18nSettings.keyPreffix() + i18nSettings.separator() + helpMessage;
                }
            }
            elementContext.put("labelKey", fieldLabel);
            elementContext.put("helpMessageKey", helpMessage);
            elementContext.put("afterElement", afterElement);
            extractFieldExtraSettings(elementContext, fieldInfo.fieldElement);
            StringBuffer methodCode = writeTemplate("templates/FieldElement.ftl", elementContext);
            Map<String, String> fieldSettings = new HashMap<>();
            fieldSettings.put("elementName", fieldName);
            fieldSettings.put("afterElement", afterElement);
            fieldSettings.put("methodName", methodName);
            fieldSettings.put("method", methodCode.toString());
            elementsSettings.add(fieldSettings);
        }
    }
    return elementsSettings;
}
Also used : FieldParam(org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) FieldDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition) ArrayList(java.util.ArrayList) Elements(javax.lang.model.util.Elements) MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeMirror(javax.lang.model.type.TypeMirror) FormDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.FormDefinition) SkipFormField(org.kie.workbench.common.forms.adf.definitions.annotations.SkipFormField) FormField(org.kie.workbench.common.forms.adf.definitions.annotations.FormField) SkipFormField(org.kie.workbench.common.forms.adf.definitions.annotations.SkipFormField) MirroredTypeException(javax.lang.model.type.MirroredTypeException) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) FieldType(org.kie.workbench.common.forms.model.FieldType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Map(java.util.Map) HashMap(java.util.HashMap) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

Elements (javax.lang.model.util.Elements)146 TypeElement (javax.lang.model.element.TypeElement)90 TypeMirror (javax.lang.model.type.TypeMirror)52 Element (javax.lang.model.element.Element)46 Types (javax.lang.model.util.Types)40 VariableElement (javax.lang.model.element.VariableElement)35 ExecutableElement (javax.lang.model.element.ExecutableElement)32 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)17 PackageElement (javax.lang.model.element.PackageElement)17 LinkedHashSet (java.util.LinkedHashSet)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)15 Test (org.junit.Test)15 List (java.util.List)14 XmlElements (javax.xml.bind.annotation.XmlElements)14 Metadata (org.apache.camel.spi.Metadata)14 Set (java.util.Set)12 DeclaredType (javax.lang.model.type.DeclaredType)12 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)12