Search in sources :

Example 1 with AsPredicate

use of org.apache.camel.spi.AsPredicate 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 2 with AsPredicate

use of org.apache.camel.spi.AsPredicate in project camel by apache.

the class CoreEipAnnotationProcessor method processElement.

private void processElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElement element, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (element != null) {
        String kind = "element";
        String name = element.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);
        boolean required = element.required();
        // metadata may overrule element required
        required = findRequired(fieldElement, required);
        // is it used as predicate (check field first and then fallback to its class)
        boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
        if (!asPredicate) {
            asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
        }
        // 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());
            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);
                    }
                }
            }
        }
        // gather oneOf expression/predicates which uses language
        Set<String> oneOfTypes = new TreeSet<String>();
        boolean isOneOf = ONE_OF_TYPE_NAME.equals(fieldTypeName);
        if (isOneOf) {
            // okay its actually an language expression, so favor using that in the eip option
            kind = "expression";
            for (String language : ONE_OF_LANGUAGES) {
                fieldTypeName = language;
                TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
                if (languages != null) {
                    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);
                        }
                    }
                }
            }
        }
        // special for otherwise as we want to indicate that the element is
        if ("otherwise".equals(name)) {
            isOneOf = true;
            oneOfTypes.add("otherwise");
        }
        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, required, defaultValue, docComment, deprecated, isEnum, enums, isOneOf, 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) 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)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)2 TreeSet (java.util.TreeSet)2 TypeElement (javax.lang.model.element.TypeElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Elements (javax.lang.model.util.Elements)2 XmlElements (javax.xml.bind.annotation.XmlElements)2 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)2 AsPredicate (org.apache.camel.spi.AsPredicate)2 Metadata (org.apache.camel.spi.Metadata)2 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)2 VariableElement (javax.lang.model.element.VariableElement)1