Search in sources :

Example 16 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class EndpointAnnotationProcessor method findComponentProperties.

protected ComponentModel findComponentProperties(RoundEnvironment roundEnv, UriEndpoint uriEndpoint, TypeElement endpointClassElement, String title, String scheme, String extendsScheme, String label) {
    ComponentModel model = new ComponentModel(scheme);
    // if the scheme is an alias then replace the scheme name from the syntax with the alias
    String syntax = scheme + ":" + Strings.after(uriEndpoint.syntax(), ":");
    // alternative syntax is optional
    if (!Strings.isNullOrEmpty(uriEndpoint.alternativeSyntax())) {
        String alternativeSyntax = scheme + ":" + Strings.after(uriEndpoint.alternativeSyntax(), ":");
        model.setAlternativeSyntax(alternativeSyntax);
    }
    model.setExtendsScheme(extendsScheme);
    model.setSyntax(syntax);
    model.setTitle(title);
    model.setLabel(label);
    model.setConsumerOnly(uriEndpoint.consumerOnly());
    model.setProducerOnly(uriEndpoint.producerOnly());
    model.setLenientProperties(uriEndpoint.lenientProperties());
    model.setAsync(implementsInterface(processingEnv, roundEnv, endpointClassElement, "org.apache.camel.AsyncEndpoint"));
    // what is the first version this component was added to Apache Camel
    String firstVersion = uriEndpoint.firstVersion();
    if (Strings.isNullOrEmpty(firstVersion) && endpointClassElement.getAnnotation(Metadata.class) != null) {
        // fallback to @Metadata if not from @UriEndpoint
        firstVersion = endpointClassElement.getAnnotation(Metadata.class).firstVersion();
    }
    if (!Strings.isNullOrEmpty(firstVersion)) {
        model.setFirstVersion(firstVersion);
    }
    String data = loadResource(processingEnv, "META-INF/services/org/apache/camel/component", scheme);
    if (data != null) {
        Map<String, String> map = parseAsMap(data);
        model.setJavaType(map.get("class"));
    }
    data = loadResource(processingEnv, "META-INF/services/org/apache/camel", "component.properties");
    if (data != null) {
        Map<String, String> map = parseAsMap(data);
        // now we have a lot more data, so we need to load it as key/value
        // need to sanitize the description first
        String doc = map.get("projectDescription");
        if (doc != null) {
            model.setDescription(sanitizeDescription(doc, true));
        } else {
            model.setDescription("");
        }
        // we can mark a component as deprecated by using the annotation or in the pom.xml
        boolean deprecated = endpointClassElement.getAnnotation(Deprecated.class) != null;
        if (!deprecated) {
            String name = map.get("projectName");
            // we may have marked a component as deprecated in the project name
            deprecated = name != null && name.contains("(deprecated)");
        }
        model.setDeprecated(deprecated);
        if (map.containsKey("groupId")) {
            model.setGroupId(map.get("groupId"));
        } else {
            model.setGroupId("");
        }
        if (map.containsKey("artifactId")) {
            model.setArtifactId(map.get("artifactId"));
        } else {
            model.setArtifactId("");
        }
        if (map.containsKey("version")) {
            model.setVersionId(map.get("version"));
        } else {
            model.setVersionId("");
        }
    }
    // favor to use endpoint class javadoc as description
    Elements elementUtils = processingEnv.getElementUtils();
    TypeElement typeElement = findTypeElement(processingEnv, roundEnv, endpointClassElement.getQualifiedName().toString());
    if (typeElement != null) {
        String doc = elementUtils.getDocComment(typeElement);
        if (doc != null) {
            // need to sanitize the description first (we only want a summary)
            doc = sanitizeDescription(doc, true);
            // the javadoc may actually be empty, so only change the doc if we got something
            if (!Strings.isNullOrEmpty(doc)) {
                model.setDescription(doc);
            }
        }
    }
    return model;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) ComponentModel(org.apache.camel.tools.apt.model.ComponentModel) Elements(javax.lang.model.util.Elements)

Example 17 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class SpringAnnotationProcessor method findEipModelProperties.

protected EipModel findEipModelProperties(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, String javaTypeName, String name) {
    EipModel model = new EipModel();
    model.setJavaType(javaTypeName);
    model.setName(name);
    Metadata metadata = classElement.getAnnotation(Metadata.class);
    if (metadata != null) {
        if (!Strings.isNullOrEmpty(metadata.label())) {
            model.setLabel(metadata.label());
        }
        if (!Strings.isNullOrEmpty(metadata.title())) {
            model.setTitle(metadata.title());
        }
    }
    // favor to use class javadoc of component as description
    if (model.getJavaType() != null) {
        Elements elementUtils = processingEnv.getElementUtils();
        TypeElement typeElement = findTypeElement(processingEnv, roundEnv, model.getJavaType());
        if (typeElement != null) {
            String doc = elementUtils.getDocComment(typeElement);
            if (doc != null) {
                // need to sanitize the description first (we only want a summary)
                doc = sanitizeDescription(doc, true);
                // the javadoc may actually be empty, so only change the doc if we got something
                if (!Strings.isNullOrEmpty(doc)) {
                    model.setDescription(doc);
                }
            }
        }
    }
    return model;
}
Also used : 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)

Example 18 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class SpringAnnotationProcessor method processElement.

private void processElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElement element, XmlElementRef elementRef, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (element != null || elementRef != null) {
        String kind = "element";
        String name = element != null ? element.name() : elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        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);
        if (isNullOrEmpty(docComment)) {
            Metadata metadata = fieldElement.getAnnotation(Metadata.class);
            docComment = metadata != null ? metadata.description() : null;
        }
        boolean required = element != null ? element.required() : elementRef.required();
        // metadata may overrule element required
        required = findRequired(fieldElement, required);
        // gather enums
        Set<String> enums = new LinkedHashSet<String>();
        boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
        if (isEnum) {
            TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
            // 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);
                }
            }
        }
        // is it a definition/factory-bean type then its a oneOf
        TreeSet oneOfTypes = new TreeSet<String>();
        if (fieldTypeName.endsWith("Definition") || fieldTypeName.endsWith("FactoryBean")) {
            TypeElement definitionClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
            if (definitionClass != null) {
                XmlRootElement rootElement = definitionClass.getAnnotation(XmlRootElement.class);
                if (rootElement != null) {
                    String childName = rootElement.name();
                    if (childName != null) {
                        oneOfTypes.add(childName);
                    }
                }
            }
        } else if (fieldTypeName.endsWith("Definition>") || fieldTypeName.endsWith("FactoryBean>")) {
            // its a list so we need to load the generic type
            String typeName = Strings.between(fieldTypeName, "<", ">");
            TypeElement definitionClass = findTypeElement(processingEnv, roundEnv, typeName);
            if (definitionClass != null) {
                XmlRootElement rootElement = definitionClass.getAnnotation(XmlRootElement.class);
                if (rootElement != null) {
                    String childName = rootElement.name();
                    if (childName != null) {
                        oneOfTypes.add(childName);
                    }
                }
            }
        }
        boolean oneOf = !oneOfTypes.isEmpty();
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        boolean asPredicate = false;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, oneOf, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Example 19 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class SpringAnnotationProcessor method processElements.

private void processElements(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElements elements, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (elements != null) {
        String kind = "element";
        String name = fieldName;
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
        if (isNullOrEmpty(docComment)) {
            Metadata metadata = fieldElement.getAnnotation(Metadata.class);
            docComment = metadata != null ? metadata.description() : null;
        }
        boolean required = false;
        required = findRequired(fieldElement, required);
        // gather oneOf of the elements
        Set<String> oneOfTypes = new TreeSet<String>();
        for (XmlElement element : elements.value()) {
            String child = element.name();
            oneOfTypes.add(child);
        }
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, kind, displayName, fieldTypeName, required, defaultValue, docComment, false, false, null, true, oneOfTypes, false);
        eipOptions.add(ep);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet) Metadata(org.apache.camel.spi.Metadata) XmlElement(javax.xml.bind.annotation.XmlElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 20 with Elements

use of javax.lang.model.util.Elements in project camel by apache.

the class CoreEipAnnotationProcessor method processIdentified.

/**
     * Special for process the OptionalIdentifiedDefinition
     */
private void processIdentified(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    // id
    String docComment = findJavaDoc(elementUtils, null, "id", null, classElement, true);
    EipOption ep = new EipOption("id", "Id", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // description
    docComment = findJavaDoc(elementUtils, null, "description", null, classElement, true);
    ep = new EipOption("description", "Description", "element", "org.apache.camel.model.DescriptionDefinition", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // lets skip custom id as it has no value for end users to configure
    if (!skipUnwanted) {
        // custom id
        docComment = findJavaDoc(elementUtils, null, "customId", null, classElement, true);
        ep = new EipOption("customId", "Custom Id", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
        eipOptions.add(ep);
    }
}
Also used : Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Aggregations

Elements (javax.lang.model.util.Elements)66 TypeElement (javax.lang.model.element.TypeElement)44 TypeMirror (javax.lang.model.type.TypeMirror)28 VariableElement (javax.lang.model.element.VariableElement)19 ExecutableElement (javax.lang.model.element.ExecutableElement)17 Types (javax.lang.model.util.Types)17 Element (javax.lang.model.element.Element)15 LinkedHashSet (java.util.LinkedHashSet)14 XmlElements (javax.xml.bind.annotation.XmlElements)14 Metadata (org.apache.camel.spi.Metadata)14 Test (org.junit.Test)14 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)12 Set (java.util.Set)9 TreeSet (java.util.TreeSet)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Optional (java.util.Optional)6 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)6