Search in sources :

Example 1 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class JAXBContextInitializer method walkReferences.

private void walkReferences(Class<?> cls) {
    if (cls == null) {
        return;
    }
    if (cls.getName().startsWith("java.") || cls.getName().startsWith("javax.")) {
        return;
    }
    // walk the public fields/methods to try and find all the classes. JAXB will only load the
    // EXACT classes in the fields/methods if they are in a different package. Thus,
    // subclasses won't be found and the xsi:type stuff won't work at all.
    // We'll grab the public field/method types and then add the ObjectFactory stuff
    // as well as look for jaxb.index files in those packages.
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    if (accessType != XmlAccessType.PROPERTY) {
        // only look for fields if we are instructed to
        // fields are accessible even if not public, must look at the declared fields
        // then walk to parents declared fields, etc...
        Field[] fields = ReflectionUtil.getDeclaredFields(cls);
        for (Field f : fields) {
            if (isFieldAccepted(f, accessType)) {
                XmlJavaTypeAdapter xjta = Utils.getFieldXJTA(f);
                if (xjta != null) {
                    Type t = Utils.getTypeFromXmlAdapter(xjta);
                    if (t != null) {
                        addType(t);
                        continue;
                    }
                }
                addType(f.getGenericType());
            }
        }
        walkReferences(cls.getSuperclass());
    }
    if (accessType != XmlAccessType.FIELD) {
        // only look for methods if we are instructed to
        Method[] methods = ReflectionUtil.getDeclaredMethods(cls);
        for (Method m : methods) {
            if (isMethodAccepted(m, accessType)) {
                XmlJavaTypeAdapter xjta = Utils.getMethodXJTA(m);
                if (xjta != null) {
                    Type t = Utils.getTypeFromXmlAdapter(xjta);
                    if (t != null) {
                        addType(t);
                        continue;
                    }
                }
                addType(m.getGenericReturnType());
                for (Type t : m.getGenericParameterTypes()) {
                    addType(t);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) XmlType(javax.xml.bind.annotation.XmlType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) Method(java.lang.reflect.Method) XmlAccessType(javax.xml.bind.annotation.XmlAccessType)

Example 2 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class Utils method setMethodValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
static void setMethodValue(Method getter, Method setter, Object target, Object value) throws Exception {
    XmlJavaTypeAdapter xjta = getMethodXJTA(getter);
    XmlAdapter adapter = getXmlAdapter(xjta);
    setter.invoke(target, adapter != null ? adapter.unmarshal(value) : value);
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlAdapter(javax.xml.bind.annotation.adapters.XmlAdapter)

Example 3 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class Utils method getMethodValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
static Object getMethodValue(Method m, Object target) throws Exception {
    XmlJavaTypeAdapter adapterAnnotation = getMethodXJTA(m);
    XmlAdapter adapter = getXmlAdapter(adapterAnnotation);
    return adapter != null ? adapter.marshal(m.invoke(target)) : m.invoke(target);
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlAdapter(javax.xml.bind.annotation.adapters.XmlAdapter)

Example 4 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class Utils method getFieldType.

static Class<?> getFieldType(Field f) {
    XmlJavaTypeAdapter adapter = getFieldXJTA(f);
    if (adapter == null && f.getGenericType() instanceof ParameterizedType && ((ParameterizedType) f.getGenericType()).getActualTypeArguments().length == 1) {
        return null;
    }
    Class<?> adapterType = (Class<?>) getTypeFromXmlAdapter(adapter);
    return adapterType != null ? adapterType : f.getType();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)

Example 5 with XmlJavaTypeAdapter

use of javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter in project cxf by apache.

the class Utils method setFieldValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
static void setFieldValue(Field f, Object target, Object value) throws Exception {
    XmlJavaTypeAdapter xjta = getFieldXJTA(f);
    XmlAdapter adapter = getXmlAdapter(xjta);
    f.set(target, adapter != null ? adapter.unmarshal(value) : value);
}
Also used : XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlAdapter(javax.xml.bind.annotation.adapters.XmlAdapter)

Aggregations

XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)28 Annotation (java.lang.annotation.Annotation)8 XmlList (javax.xml.bind.annotation.XmlList)6 QName (javax.xml.namespace.QName)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 XmlAdapter (javax.xml.bind.annotation.adapters.XmlAdapter)4 JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)4 Type (java.lang.reflect.Type)3 JAXBElement (javax.xml.bind.JAXBElement)3 XmlAttachmentRef (javax.xml.bind.annotation.XmlAttachmentRef)3 XmlElement (javax.xml.bind.annotation.XmlElement)3 Field (java.lang.reflect.Field)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Method (java.lang.reflect.Method)2 WildcardType (java.lang.reflect.WildcardType)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)2 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)2 XmlMimeType (javax.xml.bind.annotation.XmlMimeType)2 XmlType (javax.xml.bind.annotation.XmlType)2