Search in sources :

Example 71 with TypeMirror

use of javax.lang.model.type.TypeMirror in project camel by apache.

the class CoreEipAnnotationProcessor method processOutputs.

/**
     * Special for processing an @XmlElementRef outputs field
     */
private void processOutputs(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    if ("outputs".equals(fieldName) && supportOutputs(originalClassType)) {
        String kind = "element";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        // gather oneOf which extends any of the output base classes
        Set<String> oneOfTypes = new TreeSet<String>();
        // find all classes that has that superClassName
        Set<TypeElement> children = new LinkedHashSet<TypeElement>();
        for (String superclass : ONE_OF_OUTPUTS) {
            findTypeElementChildren(processingEnv, roundEnv, children, superclass);
        }
        for (TypeElement child : children) {
            XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
            if (rootElement != null) {
                String childName = rootElement.name();
                oneOfTypes.add(childName);
            }
        }
        // remove some types which are not intended as an output in eips
        oneOfTypes.remove("route");
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, true, "", "", false, false, null, true, oneOfTypes, false);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata)

Example 72 with TypeMirror

use of javax.lang.model.type.TypeMirror in project camel by apache.

the class CoreEipAnnotationProcessor method processAttribute.

private boolean processAttribute(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, VariableElement fieldElement, String fieldName, XmlAttribute attribute, Set<EipOption> eipOptions, String prefix, String modelName) {
    Elements elementUtils = processingEnv.getElementUtils();
    String name = attribute.name();
    if (isNullOrEmpty(name) || "##default".equals(name)) {
        name = fieldName;
    }
    // lets skip some unwanted attributes
    if (skipUnwanted) {
        // we want to skip inheritErrorHandler which is only applicable for the load-balancer
        boolean loadBalancer = "LoadBalanceDefinition".equals(originalClassType.getSimpleName().toString());
        if (!loadBalancer && "inheritErrorHandler".equals(name)) {
            return true;
        }
    }
    name = prefix + name;
    TypeMirror fieldType = fieldElement.asType();
    String fieldTypeName = fieldType.toString();
    TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
    String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
    String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
    boolean required = attribute.required();
    // metadata may overrule element required
    required = findRequired(fieldElement, required);
    // gather enums
    Set<String> enums = new TreeSet<String>();
    boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
    if (isEnum) {
        TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
        if (enumClass != null) {
            // find all the enum constants which has the possible enum value that can be used
            List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
            for (VariableElement var : fields) {
                if (var.getKind() == ElementKind.ENUM_CONSTANT) {
                    String val = var.toString();
                    enums.add(val);
                }
            }
        }
    }
    boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
    String displayName = null;
    Metadata metadata = fieldElement.getAnnotation(Metadata.class);
    if (metadata != null) {
        displayName = metadata.displayName();
    }
    EipOption ep = new EipOption(name, displayName, "attribute", fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, false, null, false);
    eipOptions.add(ep);
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) TreeSet(java.util.TreeSet) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 73 with TypeMirror

use of javax.lang.model.type.TypeMirror in project camel by apache.

the class CoreEipAnnotationProcessor method processRefExpression.

/**
     * Special for processing an @XmlElementRef expression field
     */
private void processRefExpression(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("expression".equals(fieldName)) {
        String kind = "expression";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        // find javadoc from original class as it will override the setExpression method where we can provide the javadoc for the given EIP
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
        // is it used as predicate (check field first and then fallback to its class / original class)
        boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
        if (!asPredicate) {
            asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
        }
        if (!asPredicate) {
            asPredicate = originalClassType.getAnnotation(AsPredicate.class) != null;
        }
        // gather oneOf expression/predicates which uses language
        Set<String> oneOfTypes = new TreeSet<String>();
        for (String language : ONE_OF_LANGUAGES) {
            TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
            String superClassName = canonicalClassName(languages.toString());
            // find all classes that has that superClassName
            Set<TypeElement> children = new LinkedHashSet<TypeElement>();
            findTypeElementChildren(processingEnv, roundEnv, children, superClassName);
            for (TypeElement child : children) {
                XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
                if (rootElement != null) {
                    String childName = rootElement.name();
                    oneOfTypes.add(childName);
                }
            }
        }
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, true, "", docComment, deprecated, false, null, true, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) AsPredicate(org.apache.camel.spi.AsPredicate) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Example 74 with TypeMirror

use of javax.lang.model.type.TypeMirror in project spring-boot by spring-projects.

the class ConfigurationMetadataAnnotationProcessor method processSimpleLombokTypes.

private void processSimpleLombokTypes(String prefix, TypeElement element, ExecutableElement source, TypeElementMembers members, Map<String, Object> fieldValues) {
    for (Map.Entry<String, VariableElement> entry : members.getFields().entrySet()) {
        String name = entry.getKey();
        VariableElement field = entry.getValue();
        if (!isLombokField(field, element)) {
            continue;
        }
        TypeMirror returnType = field.asType();
        Element returnTypeElement = this.processingEnv.getTypeUtils().asElement(returnType);
        boolean isExcluded = this.typeExcludeFilter.isExcluded(returnType);
        boolean isNested = isNested(returnTypeElement, field, element);
        boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
        boolean hasSetter = hasLombokSetter(field, element);
        if (!isExcluded && !isNested && (hasSetter || isCollection)) {
            String dataType = this.typeUtils.getType(returnType);
            String sourceType = this.typeUtils.getQualifiedName(element);
            String description = this.typeUtils.getJavaDoc(field);
            Object defaultValue = fieldValues.get(name);
            boolean deprecated = isDeprecated(field) || isDeprecated(source);
            this.metadataCollector.add(ItemMetadata.newProperty(prefix, name, dataType, sourceType, null, description, defaultValue, (deprecated ? new ItemDeprecation() : null)));
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ItemDeprecation(org.springframework.boot.configurationprocessor.metadata.ItemDeprecation) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 75 with TypeMirror

use of javax.lang.model.type.TypeMirror in project spring-boot by spring-projects.

the class ConfigurationMetadataAnnotationProcessor method processSimpleTypes.

private void processSimpleTypes(String prefix, TypeElement element, ExecutableElement source, TypeElementMembers members, Map<String, Object> fieldValues) {
    for (Map.Entry<String, ExecutableElement> entry : members.getPublicGetters().entrySet()) {
        String name = entry.getKey();
        ExecutableElement getter = entry.getValue();
        TypeMirror returnType = getter.getReturnType();
        ExecutableElement setter = members.getPublicSetter(name, returnType);
        VariableElement field = members.getFields().get(name);
        Element returnTypeElement = this.processingEnv.getTypeUtils().asElement(returnType);
        boolean isExcluded = this.typeExcludeFilter.isExcluded(returnType);
        boolean isNested = isNested(returnTypeElement, field, element);
        boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
        if (!isExcluded && !isNested && (setter != null || isCollection)) {
            String dataType = this.typeUtils.getType(returnType);
            String sourceType = this.typeUtils.getQualifiedName(element);
            String description = this.typeUtils.getJavaDoc(field);
            Object defaultValue = fieldValues.get(name);
            boolean deprecated = isDeprecated(getter) || isDeprecated(setter) || isDeprecated(source);
            this.metadataCollector.add(ItemMetadata.newProperty(prefix, name, dataType, sourceType, null, description, defaultValue, (deprecated ? getItemDeprecation(getter) : null)));
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)1086 TypeElement (javax.lang.model.element.TypeElement)419 ExecutableElement (javax.lang.model.element.ExecutableElement)259 VariableElement (javax.lang.model.element.VariableElement)237 Element (javax.lang.model.element.Element)196 DeclaredType (javax.lang.model.type.DeclaredType)177 ArrayList (java.util.ArrayList)154 Test (org.junit.Test)152 AnnotationMirror (javax.lang.model.element.AnnotationMirror)75 Types (javax.lang.model.util.Types)70 List (java.util.List)66 Elements (javax.lang.model.util.Elements)60 HashSet (java.util.HashSet)53 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)50 Map (java.util.Map)49 ExecutableType (javax.lang.model.type.ExecutableType)45 MethodSpec (com.squareup.javapoet.MethodSpec)44 PackageElement (javax.lang.model.element.PackageElement)41 ArrayType (javax.lang.model.type.ArrayType)41 Expression (com.google.devtools.j2objc.ast.Expression)39