cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1394206 - in /cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb: JAXBContextInitializer.java JAXBEncoderDecoder.java JAXBSchemaInitializer.java
Date Thu, 04 Oct 2012 18:52:26 GMT
Author: dkulp
Date: Thu Oct  4 18:52:25 2012
New Revision: 1394206

URL: http://svn.apache.org/viewvc?rev=1394206&view=rev
Log:
Merged revisions 1394194 via  git cherry-pick from
https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes

........
  r1394194 | dkulp | 2012-10-04 14:27:32 -0400 (Thu, 04 Oct 2012) | 19 lines

  Merged revisions 1394184 via  git cherry-pick from
  https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes

  ........
    r1394184 | dkulp | 2012-10-04 14:07:45 -0400 (Thu, 04 Oct 2012) | 11 lines

    Merged revisions 1394181 via  git cherry-pick from
    https://svn.apache.org/repos/asf/cxf/trunk

    ........
      r1394181 | dkulp | 2012-10-04 14:01:52 -0400 (Thu, 04 Oct 2012) | 3 lines

      [CXF-4542] More work on examining all the XmlJavaTypeAdapter things
      Patch from Richard Opalka applied

    ........

  ........

........

Modified:
    cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
    cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
    cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java

Modified: cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java?rev=1394206&r1=1394205&r2=1394206&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
Thu Oct  4 18:52:25 2012
@@ -196,15 +196,34 @@ class JAXBContextInitializer extends Ser
         if (anns != null) {
             for (Annotation a : anns) {
                 if (XmlJavaTypeAdapter.class.isAssignableFrom(a.annotationType())) {
-                    inspectTypeAdapter(((XmlJavaTypeAdapter)a).value());
+                    Type t = getTypeFromXmlAdapter((XmlJavaTypeAdapter)a);
+                    if (t != null) {
+                        addType(t);
+                    }
                 }
             }
         }
         XmlJavaTypeAdapter xjta = clazz.getAnnotation(XmlJavaTypeAdapter.class);
         if (xjta != null) {
-            inspectTypeAdapter(xjta.value());
+            Type t = getTypeFromXmlAdapter(xjta);
+            if (t != null) {
+                addType(t);
+            }
+        }
+        if (clazz.getPackage() != null) {
+            XmlJavaTypeAdapters adapt = clazz.getPackage().getAnnotation(XmlJavaTypeAdapters.class);
+            if (adapt != null) {
+                for (XmlJavaTypeAdapter a: adapt.value()) {
+                    globalAdapters.add(a.type());
+                }
+                for (XmlJavaTypeAdapter a: adapt.value()) {
+                    Type t = getTypeFromXmlAdapter(a);
+                    if (t != null) {
+                        addType(t);
+                    }
+                }
+            }
         }
-        
     }
 
     private void addType(Type cls) {
@@ -288,9 +307,10 @@ class JAXBContextInitializer extends Ser
                     //has an adapter.   We need to inspect the adapter and then
                     //return as the adapter will handle the superclass
                     //and interfaces and such
-                    @SuppressWarnings("rawtypes")
-                    Class<? extends XmlAdapter> c2 = xjta.value();
-                    inspectTypeAdapter(c2);
+                    Type t = getTypeFromXmlAdapter(xjta);
+                    if (t != null) {
+                        addType(t);
+                    }
                     return;
                 }
                 
@@ -306,17 +326,64 @@ class JAXBContextInitializer extends Ser
             }
         }
     }
+    
+    static XmlJavaTypeAdapter getFieldXJTA(final Field f) {
+        XmlJavaTypeAdapter adapter = f.getAnnotation(XmlJavaTypeAdapter.class);
+        if (adapter == null) {
+            adapter = f.getType().getAnnotation(XmlJavaTypeAdapter.class);
+        }
+        if (adapter == null) {
+            XmlJavaTypeAdapters adapters = f.getDeclaringClass().getPackage().getAnnotation(XmlJavaTypeAdapters.class);
+            if (adapters != null) {
+                for (XmlJavaTypeAdapter candidate : adapters.value()) {
+                    if (candidate != null && candidate.type().equals(f.getType()))
{
+                        adapter = candidate;
+                        break;
+                    }
+                }
+            }
+        }
+        return adapter;
+    }
 
-    private void inspectTypeAdapter(Class<? extends XmlAdapter> aclass) {
-        Class<?> c2 = aclass;
-        Type sp = c2.getGenericSuperclass();
-        while (!XmlAdapter.class.equals(c2) && c2 != null) {
-            sp = c2.getGenericSuperclass();
-            c2 = c2.getSuperclass();
+    static XmlJavaTypeAdapter getMethodXJTA(final Method m) {
+        XmlJavaTypeAdapter adapter = m.getAnnotation(XmlJavaTypeAdapter.class);
+        if (adapter == null) {
+            adapter = m.getReturnType().getAnnotation(XmlJavaTypeAdapter.class);
+        }
+        if (adapter == null) {
+            XmlJavaTypeAdapters adapters = m.getDeclaringClass().getPackage().getAnnotation(XmlJavaTypeAdapters.class);
+            if (adapters != null) {
+                for (XmlJavaTypeAdapter candidate : adapters.value()) {
+                    if (candidate != null && candidate.type().equals(m.getGenericReturnType()))
{
+                        adapter = candidate;
+                        break;
+                    }
+                }
+            }
         }
-        if (sp instanceof ParameterizedType) {
-            addType(((ParameterizedType)sp).getActualTypeArguments()[0]);
+        return adapter;
+    }
+
+    static Class<?> getTypeFromXmlAdapter(XmlJavaTypeAdapter xjta) {
+        if (xjta != null) {
+            Class<?> c2 = xjta.value();
+            Type sp = c2.getGenericSuperclass();
+            while (!XmlAdapter.class.equals(c2) && c2 != null) {
+                sp = c2.getGenericSuperclass();
+                c2 = c2.getSuperclass();
+            }
+            if (sp instanceof ParameterizedType) {
+                return (Class<?>)((ParameterizedType)sp).getActualTypeArguments()[0];
+            }
         }
+        return null;
+    }
+
+    @SuppressWarnings("rawtypes") 
+    static XmlAdapter getXmlAdapter(XmlJavaTypeAdapter adapterAnnotation)
+        throws InstantiationException, IllegalAccessException {
+        return adapterAnnotation != null ? adapterAnnotation.value().newInstance() : null;
     }
 
     private void walkReferences(Class<?> cls) {
@@ -376,9 +443,6 @@ class JAXBContextInitializer extends Ser
             && !Modifier.isPublic(field.getModifiers())) {
             return false;
         }
-        if (field.getAnnotation(XmlJavaTypeAdapter.class) != null) {
-            return false;
-        }
         if (accessType == XmlAccessType.NONE
             || accessType == XmlAccessType.PROPERTY) {
             return checkJaxbAnnotation(field.getAnnotations());
@@ -405,12 +469,6 @@ class JAXBContextInitializer extends Ser
             return false;
         }
 
-        boolean isPropGetter = method.getName().startsWith("get") || method.getName().startsWith("is");
-
-        if (!isPropGetter 
-            || method.getAnnotation(XmlJavaTypeAdapter.class) != null) {
-            return false;
-        }
         int beginIndex = 3;
         if (method.getName().startsWith("is")) {
             beginIndex = 2;
@@ -452,4 +510,4 @@ class JAXBContextInitializer extends Ser
         }
         return false;
     }
-}
\ No newline at end of file
+}

Modified: cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java?rev=1394206&r1=1394205&r2=1394206&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
Thu Oct  4 18:52:25 2012
@@ -51,6 +51,8 @@ import javax.xml.bind.annotation.XmlAcce
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 import javax.xml.bind.attachment.AttachmentMarshaller;
 import javax.xml.bind.attachment.AttachmentUnmarshaller;
 import javax.xml.namespace.NamespaceContext;
@@ -366,8 +368,8 @@ public final class JAXBEncoderDecoder {
                         if (JAXBSchemaInitializer.isArray(f.getGenericType())) {
                             writeArrayObject(marshaller, writer, fname, f.get(elValue));
                         } else {
-                            writeObject(marshaller, writer, new JAXBElement(fname, String.class,

-                                                                            f.get(elValue)));
+                            Object o = getFieldValue(f, elValue);
+                            writeObject(marshaller, writer, new JAXBElement(fname, String.class,
o));
                         }
                     }
                 }
@@ -381,8 +383,8 @@ public final class JAXBEncoderDecoder {
                     if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
                         writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
                     } else {
-                        writeObject(marshaller, writer, new JAXBElement(mname, String.class,

-                                                                        m.invoke(elValue)));
+                        Object o = getMethodValue(m, elValue);
+                        writeObject(marshaller, writer, new JAXBElement(mname, String.class,
o));
                     }
                 }
             }
@@ -495,7 +497,8 @@ public final class JAXBEncoderDecoder {
 
                             f.set(obj, o);
                         } else {
-                            f.set(obj, getElementValue(u.unmarshal(reader, f.getType())));
+                            Object o = getElementValue(u.unmarshal(reader, getFieldType(f)));
+                            setFieldValue(f, obj, o);
                         }
                     } else {
                         throw new NoSuchFieldException("No accessible field " + q.getLocalPart());
@@ -532,8 +535,8 @@ public final class JAXBEncoderDecoder {
 
                         m2.invoke(obj, o);
                     } else {
-                        Object o = getElementValue(u.unmarshal(reader, m.getReturnType()));
-                        m2.invoke(obj, o);
+                        Object o = getElementValue(u.unmarshal(reader, getMethodReturnType(m)));
+                        setMethodValue(m, m2, obj, o);
                     }
                 }
             }
@@ -543,6 +546,46 @@ public final class JAXBEncoderDecoder {
         }
     }
 
+    private static Class<?> getFieldType(final Field f) {
+        XmlJavaTypeAdapter adapter = JAXBContextInitializer.getFieldXJTA(f);
+        Class<?> adapterType = JAXBContextInitializer.getTypeFromXmlAdapter(adapter);
+        return adapterType != null ? adapterType : f.getType();
+    }
+
+    private static Class<?> getMethodReturnType(final Method m) {
+        XmlJavaTypeAdapter adapter = JAXBContextInitializer.getMethodXJTA(m);
+        Class<?> adapterType = JAXBContextInitializer.getTypeFromXmlAdapter(adapter);
+        return adapterType != null ? adapterType : m.getReturnType(); 
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private static Object getFieldValue(Field f, Object target) throws Exception {
+        XmlJavaTypeAdapter adapterAnnotation = JAXBContextInitializer.getFieldXJTA(f);
+        XmlAdapter adapter = JAXBContextInitializer.getXmlAdapter(adapterAnnotation);
+        return adapter != null ? adapter.marshal(f.get(target)) : f.get(target); 
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private static Object getMethodValue(Method m, Object target) throws Exception {
+        XmlJavaTypeAdapter adapterAnnotation = JAXBContextInitializer.getMethodXJTA(m);
+        XmlAdapter adapter = JAXBContextInitializer.getXmlAdapter(adapterAnnotation);
+        return adapter != null ? adapter.marshal(m.invoke(target)) : m.invoke(target); 
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private static void setFieldValue(Field f, Object target, Object value) throws Exception
{
+        XmlJavaTypeAdapter xjta = JAXBContextInitializer.getFieldXJTA(f);
+        XmlAdapter adapter = JAXBContextInitializer.getXmlAdapter(xjta);
+        f.set(target, adapter != null ? adapter.unmarshal(value) : value);
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private static void setMethodValue(Method getter, Method setter, Object target, Object
value) throws Exception {
+        XmlJavaTypeAdapter xjta = JAXBContextInitializer.getMethodXJTA(getter);
+        XmlAdapter adapter = JAXBContextInitializer.getXmlAdapter(xjta);
+        setter.invoke(target, adapter != null ? adapter.unmarshal(value) : value);
+    }
+
     private static void writeObject(Marshaller u, Object source, Object mObj) throws Fault,
JAXBException {
         if (source instanceof XMLStreamWriter) {
             u.marshal(mObj, (XMLStreamWriter)source);

Modified: cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java?rev=1394206&r1=1394205&r2=1394206&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
Thu Oct  4 18:52:25 2012
@@ -523,7 +523,7 @@ class JAXBSchemaInitializer extends Serv
         for (Field f : cls.getDeclaredFields()) {
             if (JAXBContextInitializer.isFieldAccepted(f, accessType)) {
                 //map field
-                Type type = f.getGenericType();
+                Type type = getFieldType(f);
                 JAXBBeanInfo beanInfo = getBeanInfo(type);
                 if (beanInfo != null) {
                     addElement(schema, seq, beanInfo, new QName(namespace, f.getName()),
isArray(type));
@@ -532,8 +532,8 @@ class JAXBSchemaInitializer extends Serv
         }
         for (Method m : cls.getMethods()) {
             if (JAXBContextInitializer.isMethodAccepted(m, accessType)) {
-                //map field
-                Type type = m.getGenericReturnType();
+                //map method
+                Type type = getMethodReturnType(m);
                 JAXBBeanInfo beanInfo = getBeanInfo(type);
                 if (beanInfo != null) {
                     int idx = m.getName().startsWith("get") ? 3 : 2;
@@ -556,6 +556,18 @@ class JAXBSchemaInitializer extends Serv
         schemas.addCrossImports();
         part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
     }
+    
+    private static Type getFieldType(final Field f) {
+        XmlJavaTypeAdapter adapter = JAXBContextInitializer.getFieldXJTA(f);
+        Class<?> adapterType = JAXBContextInitializer.getTypeFromXmlAdapter(adapter);
+        return adapterType != null ? adapterType : f.getGenericType();
+    }
+
+    private static Type getMethodReturnType(final Method m) {
+        XmlJavaTypeAdapter adapter = JAXBContextInitializer.getMethodXJTA(m);
+        Class<?> adapterType = JAXBContextInitializer.getTypeFromXmlAdapter(adapter);
+        return adapterType != null ? adapterType : m.getGenericReturnType(); 
+    }
 
     static boolean isArray(Type cls) {
         if (cls instanceof Class) {



Mime
View raw message