Search in sources :

Example 6 with Metadata

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

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

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

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

the class CoreEipAnnotationProcessor method processRefWhenClauses.

/**
     * Special for processing an @XmlElementRef when field
     */
private void processRefWhenClauses(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("whenClauses".equals(fieldName)) {
        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();
        // 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);
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        // indicate that this element is one of when
        Set<String> oneOfTypes = new HashSet<String>();
        oneOfTypes.add("when");
        // when is predicate
        boolean asPredicate = true;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, false, "", docComment, deprecated, false, null, true, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) Metadata(org.apache.camel.spi.Metadata) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with Metadata

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

the class EndpointAnnotationProcessor method findClassProperties.

protected void findClassProperties(PrintWriter writer, RoundEnvironment roundEnv, ComponentModel componentModel, Set<EndpointPath> endpointPaths, Set<EndpointOption> endpointOptions, TypeElement classElement, String prefix, String excludeProperties) {
    Elements elementUtils = processingEnv.getElementUtils();
    while (true) {
        List<VariableElement> fieldElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
        for (VariableElement fieldElement : fieldElements) {
            Metadata metadata = fieldElement.getAnnotation(Metadata.class);
            boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
            Boolean secret = metadata != null ? metadata.secret() : null;
            UriPath path = fieldElement.getAnnotation(UriPath.class);
            String fieldName = fieldElement.getSimpleName().toString();
            if (path != null) {
                String name = path.name();
                if (isNullOrEmpty(name)) {
                    name = fieldName;
                }
                name = prefix + name;
                // should we exclude the name?
                if (excludeProperty(excludeProperties, name)) {
                    continue;
                }
                String defaultValue = path.defaultValue();
                if (Strings.isNullOrEmpty(defaultValue) && metadata != null) {
                    defaultValue = metadata.defaultValue();
                }
                String defaultValueNote = path.defaultValueNote();
                String required = metadata != null ? metadata.required() : null;
                String label = path.label();
                if (Strings.isNullOrEmpty(label) && metadata != null) {
                    label = metadata.label();
                }
                String displayName = path.displayName();
                if (Strings.isNullOrEmpty(displayName)) {
                    displayName = metadata != null ? metadata.displayName() : null;
                }
                TypeMirror fieldType = fieldElement.asType();
                String fieldTypeName = fieldType.toString();
                TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
                String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, false);
                if (isNullOrEmpty(docComment)) {
                    docComment = path.description();
                }
                // gather enums
                Set<String> enums = new LinkedHashSet<String>();
                boolean isEnum;
                if (!Strings.isNullOrEmpty(path.enums())) {
                    isEnum = true;
                    String[] values = path.enums().split(",");
                    for (String val : values) {
                        enums.add(val);
                    }
                } else {
                    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
                        if (enumClass != null) {
                            List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
                            for (VariableElement var : fields) {
                                if (var.getKind() == ElementKind.ENUM_CONSTANT) {
                                    String val = var.toString();
                                    enums.add(val);
                                }
                            }
                        }
                    }
                }
                // the field type may be overloaded by another type
                if (!Strings.isNullOrEmpty(path.javaType())) {
                    fieldTypeName = path.javaType();
                }
                String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
                boolean isSecret = secret != null ? secret : false;
                EndpointPath ep = new EndpointPath(name, displayName, fieldTypeName, required, defaultValue, docComment, deprecated, isSecret, group, label, isEnum, enums);
                endpointPaths.add(ep);
            }
            UriParam param = fieldElement.getAnnotation(UriParam.class);
            fieldName = fieldElement.getSimpleName().toString();
            if (param != null) {
                String name = param.name();
                if (isNullOrEmpty(name)) {
                    name = fieldName;
                }
                name = prefix + name;
                // should we exclude the name?
                if (excludeProperty(excludeProperties, name)) {
                    continue;
                }
                String paramOptionalPrefix = param.optionalPrefix();
                String paramPrefix = param.prefix();
                boolean multiValue = param.multiValue();
                String defaultValue = param.defaultValue();
                if (defaultValue == null && metadata != null) {
                    defaultValue = metadata.defaultValue();
                }
                String defaultValueNote = param.defaultValueNote();
                String required = metadata != null ? metadata.required() : null;
                String label = param.label();
                if (Strings.isNullOrEmpty(label) && metadata != null) {
                    label = metadata.label();
                }
                String displayName = param.displayName();
                if (Strings.isNullOrEmpty(displayName)) {
                    displayName = metadata != null ? metadata.displayName() : null;
                }
                // if the field type is a nested parameter then iterate through its fields
                TypeMirror fieldType = fieldElement.asType();
                String fieldTypeName = fieldType.toString();
                TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
                UriParams fieldParams = null;
                if (fieldTypeElement != null) {
                    fieldParams = fieldTypeElement.getAnnotation(UriParams.class);
                }
                if (fieldParams != null) {
                    String nestedPrefix = prefix;
                    String extraPrefix = fieldParams.prefix();
                    if (!isNullOrEmpty(extraPrefix)) {
                        nestedPrefix += extraPrefix;
                    }
                    findClassProperties(writer, roundEnv, componentModel, endpointPaths, endpointOptions, fieldTypeElement, nestedPrefix, excludeProperties);
                } else {
                    String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, false);
                    if (isNullOrEmpty(docComment)) {
                        docComment = param.description();
                    }
                    if (isNullOrEmpty(docComment)) {
                        docComment = "";
                    }
                    // gather enums
                    Set<String> enums = new LinkedHashSet<String>();
                    boolean isEnum;
                    if (!Strings.isNullOrEmpty(param.enums())) {
                        isEnum = true;
                        String[] values = param.enums().split(",");
                        for (String val : values) {
                            enums.add(val);
                        }
                    } else {
                        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);
                                    }
                                }
                            }
                        }
                    }
                    // the field type may be overloaded by another type
                    if (!Strings.isNullOrEmpty(param.javaType())) {
                        fieldTypeName = param.javaType();
                    }
                    boolean isSecret = secret != null ? secret : param.secret();
                    String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
                    EndpointOption option = new EndpointOption(name, displayName, fieldTypeName, required, defaultValue, defaultValueNote, docComment.trim(), paramOptionalPrefix, paramPrefix, multiValue, deprecated, isSecret, group, label, isEnum, enums);
                    endpointOptions.add(option);
                }
            }
        }
        // check super classes which may also have @UriParam fields
        TypeElement baseTypeElement = null;
        TypeMirror superclass = classElement.getSuperclass();
        if (superclass != null) {
            String superClassName = canonicalClassName(superclass.toString());
            baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
        }
        if (baseTypeElement != null) {
            classElement = baseTypeElement;
        } else {
            break;
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) 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) UriParam(org.apache.camel.spi.UriParam) EndpointPath(org.apache.camel.tools.apt.model.EndpointPath) UriParams(org.apache.camel.spi.UriParams) TypeMirror(javax.lang.model.type.TypeMirror) UriPath(org.apache.camel.spi.UriPath) EndpointOption(org.apache.camel.tools.apt.model.EndpointOption)

Aggregations

Metadata (org.apache.camel.spi.Metadata)17 Elements (javax.lang.model.util.Elements)14 TypeMirror (javax.lang.model.type.TypeMirror)13 XmlElements (javax.xml.bind.annotation.XmlElements)12 TypeElement (javax.lang.model.element.TypeElement)11 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)11 TreeSet (java.util.TreeSet)9 LinkedHashSet (java.util.LinkedHashSet)8 VariableElement (javax.lang.model.element.VariableElement)6 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)5 XmlElement (javax.xml.bind.annotation.XmlElement)2 AsPredicate (org.apache.camel.spi.AsPredicate)2 HashSet (java.util.HashSet)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 UriParam (org.apache.camel.spi.UriParam)1 UriParams (org.apache.camel.spi.UriParams)1 UriPath (org.apache.camel.spi.UriPath)1 ComponentOption (org.apache.camel.tools.apt.model.ComponentOption)1 EndpointOption (org.apache.camel.tools.apt.model.EndpointOption)1 EndpointPath (org.apache.camel.tools.apt.model.EndpointPath)1