Search in sources :

Example 1 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverter method canRead.

/**
	 * {@inheritDoc}
	 * <p>Jaxb2CollectionHttpMessageConverter can read a generic
	 * {@link Collection} where the generic type is a JAXB type annotated with
	 * {@link XmlRootElement} or {@link XmlType}.
	 */
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
    if (!(type instanceof ParameterizedType)) {
        return false;
    }
    ParameterizedType parameterizedType = (ParameterizedType) type;
    if (!(parameterizedType.getRawType() instanceof Class)) {
        return false;
    }
    Class<?> rawType = (Class<?>) parameterizedType.getRawType();
    if (!(Collection.class.isAssignableFrom(rawType))) {
        return false;
    }
    if (parameterizedType.getActualTypeArguments().length != 1) {
        return false;
    }
    Type typeArgument = parameterizedType.getActualTypeArguments()[0];
    if (!(typeArgument instanceof Class)) {
        return false;
    }
    Class<?> typeArgumentClass = (Class<?>) typeArgument;
    return (typeArgumentClass.isAnnotationPresent(XmlRootElement.class) || typeArgumentClass.isAnnotationPresent(XmlType.class)) && canRead(mediaType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlType(javax.xml.bind.annotation.XmlType) MediaType(org.springframework.http.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) XmlType(javax.xml.bind.annotation.XmlType)

Example 2 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement 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 3 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.

the class CoreEipAnnotationProcessor method processModelClass.

protected void processModelClass(final ProcessingEnvironment processingEnv, final RoundEnvironment roundEnv, final TypeElement classElement) {
    final String javaTypeName = canonicalClassName(classElement.getQualifiedName().toString());
    String packageName = javaTypeName.substring(0, javaTypeName.lastIndexOf("."));
    // skip abstract classes
    if (classElement.getModifiers().contains(Modifier.ABSTRACT)) {
        return;
    }
    // skip unwanted classes which are "abstract" holders
    if (skipUnwanted) {
        if (classElement.getQualifiedName().toString().equals(ONE_OF_TYPE_NAME)) {
            return;
        }
    }
    final XmlRootElement rootElement = classElement.getAnnotation(XmlRootElement.class);
    if (rootElement == null) {
        return;
    }
    String aName = rootElement.name();
    if (isNullOrEmpty(aName) || "##default".equals(aName)) {
        XmlType typeElement = classElement.getAnnotation(XmlType.class);
        aName = typeElement.name();
    }
    final String name = aName;
    // lets use the xsd name as the file name
    String fileName;
    if (isNullOrEmpty(name) || "##default".equals(name)) {
        fileName = classElement.getSimpleName().toString() + ".json";
    } else {
        fileName = name + ".json";
    }
    // write json schema
    Func1<PrintWriter, Void> handler = new Func1<PrintWriter, Void>() {

        @Override
        public Void call(PrintWriter writer) {
            writeJSonSchemeDocumentation(processingEnv, writer, roundEnv, classElement, rootElement, javaTypeName, name);
            return null;
        }
    };
    processFile(processingEnv, packageName, fileName, handler);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlType(javax.xml.bind.annotation.XmlType) PrintWriter(java.io.PrintWriter)

Example 4 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement 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 5 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.

the class CoreEipAnnotationProcessor method processRoute.

private void processRoute(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    // group
    String docComment = findJavaDoc(elementUtils, null, "group", null, classElement, true);
    EipOption ep = new EipOption("group", "Group", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // group
    docComment = findJavaDoc(elementUtils, null, "streamCache", null, classElement, true);
    ep = new EipOption("streamCache", "Stream Cache", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // trace
    docComment = findJavaDoc(elementUtils, null, "trace", null, classElement, true);
    ep = new EipOption("trace", "Trace", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // trace
    docComment = findJavaDoc(elementUtils, null, "messageHistory", null, classElement, true);
    ep = new EipOption("messageHistory", "Message History", "attribute", "java.lang.String", false, "true", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // trace
    docComment = findJavaDoc(elementUtils, null, "handleFault", null, classElement, true);
    ep = new EipOption("handleFault", "Handle Fault", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // delayer
    docComment = findJavaDoc(elementUtils, null, "delayer", null, classElement, true);
    ep = new EipOption("delayer", "Delayer", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // autoStartup
    docComment = findJavaDoc(elementUtils, null, "autoStartup", null, classElement, true);
    ep = new EipOption("autoStartup", "Auto Startup", "attribute", "java.lang.String", false, "true", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // startupOrder
    docComment = findJavaDoc(elementUtils, null, "startupOrder", null, classElement, true);
    ep = new EipOption("startupOrder", "Startup Order", "attribute", "java.lang.Integer", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // errorHandlerRef
    docComment = findJavaDoc(elementUtils, null, "errorHandlerRef", null, classElement, true);
    ep = new EipOption("errorHandlerRef", "Error Handler", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // routePolicyRef
    docComment = findJavaDoc(elementUtils, null, "routePolicyRef", null, classElement, true);
    ep = new EipOption("routePolicyRef", "Route Policy", "attribute", "java.lang.String", false, "", docComment, false, false, null, false, null, false);
    eipOptions.add(ep);
    // shutdownRoute
    Set<String> enums = new LinkedHashSet<String>();
    enums.add("Default");
    enums.add("Defer");
    docComment = findJavaDoc(elementUtils, null, "shutdownRoute", "Default", classElement, true);
    ep = new EipOption("shutdownRoute", "Shutdown Route", "attribute", "org.apache.camel.ShutdownRoute", false, "", docComment, false, true, enums, false, null, false);
    eipOptions.add(ep);
    // shutdownRunningTask
    enums = new LinkedHashSet<String>();
    enums.add("CompleteCurrentTaskOnly");
    enums.add("CompleteAllTasks");
    docComment = findJavaDoc(elementUtils, null, "shutdownRunningTask", "CompleteCurrentTaskOnly", classElement, true);
    ep = new EipOption("shutdownRunningTask", "Shutdown Running Task", "attribute", "org.apache.camel.ShutdownRunningTask", false, "", docComment, false, true, enums, false, null, false);
    eipOptions.add(ep);
    // inputs
    Set<String> oneOfTypes = new TreeSet<String>();
    oneOfTypes.add("from");
    docComment = findJavaDoc(elementUtils, null, "inputs", null, classElement, true);
    ep = new EipOption("inputs", "Inputs", "element", "java.util.List<org.apache.camel.model.FromDefinition>", true, "", docComment, false, false, null, true, oneOfTypes, false);
    eipOptions.add(ep);
    // outputs
    // gather oneOf which extends any of the output base classes
    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");
    docComment = findJavaDoc(elementUtils, null, "outputs", null, classElement, true);
    ep = new EipOption("outputs", "Outputs", "element", "java.util.List<org.apache.camel.model.ProcessorDefinition<?>>", true, "", docComment, false, false, null, true, oneOfTypes, false);
    eipOptions.add(ep);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) TreeSet(java.util.TreeSet) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Aggregations

XmlRootElement (javax.xml.bind.annotation.XmlRootElement)39 XmlSchema (javax.xml.bind.annotation.XmlSchema)11 XmlType (javax.xml.bind.annotation.XmlType)9 QName (javax.xml.namespace.QName)9 LinkedHashSet (java.util.LinkedHashSet)6 TreeSet (java.util.TreeSet)6 TypeElement (javax.lang.model.element.TypeElement)6 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)6 Method (java.lang.reflect.Method)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Elements (javax.lang.model.util.Elements)5 XmlElements (javax.xml.bind.annotation.XmlElements)5 Metadata (org.apache.camel.spi.Metadata)5 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2