Search in sources :

Example 1 with Metadata

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

the class EndpointAnnotationProcessor method findComponentClassProperties.

protected void findComponentClassProperties(PrintWriter writer, RoundEnvironment roundEnv, ComponentModel componentModel, Set<ComponentOption> componentOptions, TypeElement classElement, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    while (true) {
        Metadata componentAnnotation = classElement.getAnnotation(Metadata.class);
        if (componentAnnotation != null && Objects.equals("verifiers", componentAnnotation.label())) {
            componentModel.setVerifiers(componentAnnotation.enums());
        }
        List<ExecutableElement> methods = ElementFilter.methodsIn(classElement.getEnclosedElements());
        for (ExecutableElement method : methods) {
            String methodName = method.getSimpleName().toString();
            boolean deprecated = method.getAnnotation(Deprecated.class) != null;
            Metadata metadata = method.getAnnotation(Metadata.class);
            // must be the setter
            boolean isSetter = methodName.startsWith("set") && method.getParameters().size() == 1 & method.getReturnType().getKind().equals(TypeKind.VOID);
            if (!isSetter) {
                continue;
            }
            // skip unwanted methods as they are inherited from default component and are not intended for end users to configure
            if ("setEndpointClass".equals(methodName) || "setCamelContext".equals(methodName) || "setEndpointHeaderFilterStrategy".equals(methodName) || "setApplicationContext".equals(methodName)) {
                continue;
            }
            // must be a getter/setter pair
            String fieldName = methodName.substring(3);
            fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
            // we usually favor putting the @Metadata annotation on the field instead of the setter, so try to use it if its there
            VariableElement field = findFieldElement(classElement, fieldName);
            if (field != null && metadata == null) {
                metadata = field.getAnnotation(Metadata.class);
            }
            String required = metadata != null ? metadata.required() : null;
            String label = metadata != null ? metadata.label() : null;
            boolean secret = metadata != null && metadata.secret();
            String displayName = metadata != null ? metadata.displayName() : null;
            // we do not yet have default values / notes / as no annotation support yet
            // String defaultValueNote = param.defaultValueNote();
            String defaultValue = metadata != null ? metadata.defaultValue() : null;
            String defaultValueNote = null;
            ExecutableElement setter = method;
            String name = fieldName;
            name = prefix + name;
            TypeMirror fieldType = setter.getParameters().get(0).asType();
            String fieldTypeName = fieldType.toString();
            TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
            String docComment = findJavaDoc(elementUtils, method, fieldName, name, classElement, false);
            if (isNullOrEmpty(docComment)) {
                docComment = metadata != null ? metadata.description() : null;
            }
            if (isNullOrEmpty(docComment)) {
                // apt cannot grab javadoc from camel-core, only from annotations
                if ("setHeaderFilterStrategy".equals(methodName)) {
                    docComment = HEADER_FILTER_STRATEGY_JAVADOC;
                } else {
                    docComment = "";
                }
            }
            // gather enums
            Set<String> enums = new LinkedHashSet<String>();
            boolean isEnum;
            if (metadata != null && !Strings.isNullOrEmpty(metadata.enums())) {
                isEnum = true;
                String[] values = metadata.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);
                            }
                        }
                    }
                }
            }
            String group = EndpointHelper.labelAsGroupName(label, componentModel.isConsumerOnly(), componentModel.isProducerOnly());
            ComponentOption option = new ComponentOption(name, displayName, fieldTypeName, required, defaultValue, defaultValueNote, docComment.trim(), deprecated, secret, group, label, isEnum, enums);
            componentOptions.add(option);
        }
        // check super classes which may also have 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) ComponentOption(org.apache.camel.tools.apt.model.ComponentOption) ExecutableElement(javax.lang.model.element.ExecutableElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) TypeMirror(javax.lang.model.type.TypeMirror)

Example 2 with Metadata

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

the class SpringAnnotationProcessor method findDefaultValue.

private String findDefaultValue(VariableElement fieldElement, String fieldTypeName) {
    String defaultValue = null;
    Metadata metadata = fieldElement.getAnnotation(Metadata.class);
    if (metadata != null) {
        if (!Strings.isNullOrEmpty(metadata.defaultValue())) {
            defaultValue = metadata.defaultValue();
        }
    }
    if (defaultValue == null) {
        // if its a boolean type, then we use false as the default
        if ("boolean".equals(fieldTypeName) || "java.lang.Boolean".equals(fieldTypeName)) {
            defaultValue = "false";
        }
    }
    return defaultValue;
}
Also used : Metadata(org.apache.camel.spi.Metadata)

Example 3 with Metadata

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

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

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

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