[jbossws-commits] JBossWS SVN: r17234 - in thirdparty/cxf/branches/cxf-2.2.12: rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb and 1 other directory.

jbossws-commits at lists.jboss.org jbossws-commits at lists.jboss.org
Fri Jan 11 18:08:57 EST 2013


Author: klape
Date: 2013-01-11 18:08:57 -0500 (Fri, 11 Jan 2013)
New Revision: 17234

Added:
   thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java
Modified:
   thirdparty/cxf/branches/cxf-2.2.12/
   thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
   thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
   thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
Log:
[JBPAPP-10568][JBPAPP-10566] Fix @XmlTransient and @XmlJavaTypeAdapter issue for exception types. Merged -r16828:17225 from https://svn.jboss.org/repos/jbossws/thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129


Property changes on: thirdparty/cxf/branches/cxf-2.2.12
___________________________________________________________________
Added: svn:mergeinfo
   + /thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129:16821-17225

Modified: thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
===================================================================
--- thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java	2013-01-11 21:19:36 UTC (rev 17233)
+++ thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java	2013-01-11 23:08:57 UTC (rev 17234)
@@ -33,7 +33,6 @@
 import java.util.Set;
 
 import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlSeeAlso;
@@ -41,6 +40,7 @@
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
@@ -196,13 +196,19 @@
         if (anns != null) {
             for (Annotation a : anns) {
                 if (XmlJavaTypeAdapter.class.isAssignableFrom(a.annotationType())) {
-                    inspectTypeAdapter(((XmlJavaTypeAdapter)a).value());
+                    Type t = Utils.getTypeFromXmlAdapter((XmlJavaTypeAdapter)a);
+                    if (t != null) {
+                        addType(t);
+                    }
                 }
             }
         }
         XmlJavaTypeAdapter xjta = clazz.getAnnotation(XmlJavaTypeAdapter.class);
         if (xjta != null) {
-            inspectTypeAdapter(xjta.value());
+            Type t = Utils.getTypeFromXmlAdapter(xjta);
+            if (t != null) {
+                addType(t);
+            }
         }
         
     }
@@ -286,8 +292,11 @@
                     }
                     XmlJavaTypeAdapter xjta = cls.getAnnotation(XmlJavaTypeAdapter.class);
                     if (xjta != null) {
-                        Class<? extends XmlAdapter> c2 = xjta.value();
-                        inspectTypeAdapter(c2);
+                     Type t = Utils.getTypeFromXmlAdapter(xjta);
+                       if (t != null) {
+                        addType(t);
+                       }
+                     return;
                     }
                 } else if (classes.contains(cls)) {
                     return;
@@ -299,16 +308,23 @@
         }
     }
 
-    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 getFieldXJTA(final Field f) {
+        XmlJavaTypeAdapter adapter = f.getAnnotation(XmlJavaTypeAdapter.class);
+        if (adapter == null) {
+            adapter = f.getType().getAnnotation(XmlJavaTypeAdapter.class);
         }
-        if (sp instanceof ParameterizedType) {
-            addType(((ParameterizedType)sp).getActualTypeArguments()[0]);
+        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 walkReferences(Class<?> cls) {
@@ -325,11 +341,7 @@
         //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.
 
-        XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
-        if (accessorType == null && cls.getPackage() != null) {
-            accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
-        }
-        XmlAccessType accessType = accessorType != null ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;
+        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
@@ -368,9 +380,6 @@
             && !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());
@@ -397,12 +406,6 @@
             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;
@@ -428,7 +431,7 @@
      * @param annotations the array of annotations from the class member
      * @return true if JAXB annotations are present, false otherwise
      */
-    private static boolean checkJaxbAnnotation(Annotation[] annotations) {
+    static boolean checkJaxbAnnotation(Annotation[] annotations) {
         // must check if there are any jaxb annotations
         Package jaxbAnnotationsPackage = XmlElement.class.getPackage();
         for (Annotation annotation : annotations) {

Modified: thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
===================================================================
--- thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java	2013-01-11 21:19:36 UTC (rev 17233)
+++ thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java	2013-01-11 23:08:57 UTC (rev 17234)
@@ -49,7 +49,10 @@
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAccessType;
 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;
@@ -65,7 +68,6 @@
 import javax.xml.transform.stream.StreamResult;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
-
 import org.apache.cxf.common.i18n.Message;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.SOAPConstants;
@@ -335,12 +337,7 @@
         try {
             writer.writeStartElement("ns1", qn.getLocalPart(), qn.getNamespaceURI());
             Class<?> cls = part.getTypeClass();
-            XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
-            if (accessorType == null && cls.getPackage() != null) {
-                accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
-            }
-            XmlAccessType accessType = accessorType != null
-                ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;
+            XmlAccessType accessType = Utils.getXmlAccessType(cls);
             String namespace = part.getElementQName().getNamespaceURI();
             
             SchemaInfo sch = part.getMessageInfo().getOperation().getInterface()
@@ -353,33 +350,31 @@
                 LOG.warning("Schema associated with " + namespace + " is null");
             }
             
-            for (Field f : cls.getDeclaredFields()) {
-                if (JAXBContextInitializer.isFieldAccepted(f, accessType)) {
+            for (Field f : Utils.getFields(cls, accessType)) {
+                XmlAttribute at = f.getAnnotation(XmlAttribute.class);
+                if (at == null) {
                     QName fname = new QName(namespace, f.getName());
                     f.setAccessible(true);
                     if (JAXBSchemaInitializer.isArray(f.getGenericType())) {
-                        writeArrayObject(marshaller, writer, fname, f.get(elValue));                        
+                        writeArrayObject(marshaller, writer, fname, f.get(elValue));
                     } else {
-                        writeObject(marshaller, writer, new JAXBElement(fname, String.class, 
-                                                                        f.get(elValue)));
+                        Object o = Utils.getFieldValue(f, elValue); 
+                        writeObject(marshaller, writer, new JAXBElement(fname, String.class, o));
                     }
                 }
             }
-            for (Method m : cls.getMethods()) {
-                if (JAXBContextInitializer.isMethodAccepted(m, accessType)) {
-                    int idx = m.getName().startsWith("get") ? 3 : 2;
-                    String name = m.getName().substring(idx);
-                    name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
-                    QName mname = new QName(namespace, name);
-                    if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
-                        writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
-                    } else {
-                        writeObject(marshaller, writer, new JAXBElement(mname, String.class, 
-                                                                        m.invoke(elValue)));
+            for (Method m : Utils.getGetters(cls, accessType)) {
+                int idx = m.getName().startsWith("get") ? 3 : 2;
+                String name = m.getName().substring(idx);
+                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+                QName mname = new QName(namespace, name);
+                if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
+                    writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
+                } else {
+                    Object o = Utils.getMethodValue(m, elValue); 
+                    writeObject(marshaller, writer, new JAXBElement(mname, String.class, o));
                     }
-                }
             }
-
             writer.writeEndElement();
             writer.flush();
         } catch (Exception e) {
@@ -448,55 +443,45 @@
                 obj = cons.newInstance(new Object[1]);
             }
 
-            XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
-            if (accessorType == null && cls.getPackage() != null) {
-                accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
-            }
-            XmlAccessType accessType = accessorType != null
-                ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;
+            XmlAccessType accessType = Utils.getXmlAccessType(cls);
             reader.nextTag();
             while (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
                 QName q = reader.getName();
-                try {
-                    Field f = cls.getField(q.getLocalPart());
+                  String fieldName = q.getLocalPart();
+                  Field f = Utils.getField(cls, accessType, fieldName);
+                  if (f != null) {
                     Type type = f.getGenericType();
-                    if (JAXBContextInitializer.isFieldAccepted(f, accessType)) {
-                        f.setAccessible(true);
-                        if (JAXBSchemaInitializer.isArray(type)) {
-                            Class<?> compType = JAXBSchemaInitializer
-                                .getArrayComponentType(type);
-                            List<Object> ret = unmarshallArray(u, reader,
-                                                               q,
-                                                               compType,
-                                                               createList(type));
-                            Object o = ret;
-                            if (!isList(type)) {
-                                if (compType.isPrimitive()) {
-                                    o = java.lang.reflect.Array.newInstance(compType, ret.size());
-                                    for (int x = 0; x < ret.size(); x++) {
-                                        Array.set(o, x, ret.get(x));
-                                    }
-                                } else {
-                                    o = ret.toArray((Object[])Array.newInstance(compType, ret.size()));
-                                }
+                    f.setAccessible(true);
+                    if (JAXBSchemaInitializer.isArray(type)) {
+                        Class<?> compType = JAXBSchemaInitializer.getArrayComponentType(type);
+                        List<Object> ret = unmarshallArray(u, reader, q, compType, createList(type));
+                        Object o = ret;
+                        if (!isList(type)) {
+                            if (compType.isPrimitive()) {
+                                o = java.lang.reflect.Array.newInstance(compType, ret.size());
+                                for (int x = 0; x < ret.size(); x++) {
+                                    Array.set(o, x, ret.get(x));
+                                 }
+                            } else {
+                                o = ret.toArray((Object[]) Array.newInstance(compType, ret.size()));
+                              }
                             }
 
                             f.set(obj, o);
                         } else {
-                            f.set(obj, u.unmarshal(reader, f.getType()));
+                            Object o = getElementValue(u.unmarshal(reader, Utils.getFieldType(f)));
+                            Utils.setFieldValue(f, obj, o);
                         }
-                    }
-                } catch (NoSuchFieldException ex) {
+                    
+                } else {
                     String s = Character.toUpperCase(q.getLocalPart().charAt(0))
                                + q.getLocalPart().substring(1);
-                    Method m = null;
-                    try {
-                        m = cls.getMethod("get" + s);
-                    } catch (NoSuchMethodException mex) {
-                        m = cls.getMethod("is" + s);
+                    Method m = Utils.getMethod(cls, accessType, "get" + s);
+                    if (m == null) {
+                        m = Utils.getMethod(cls, accessType, "is" + s);
                     }
                     Type type = m.getGenericReturnType();
-                    Method m2 = cls.getMethod("set" + s, m.getReturnType());
+                    Method m2 = Utils.getMethod(cls, accessType, "set" + s, m.getReturnType());
                     if (JAXBSchemaInitializer.isArray(type)) {
                         Class<?> compType = JAXBSchemaInitializer
                             .getArrayComponentType(type);
@@ -518,16 +503,17 @@
 
                         m2.invoke(obj, o);
                     } else {
-                        Object o = getElementValue(u.unmarshal(reader, m.getReturnType()));
-                        m2.invoke(obj, o);
+                        Object o = getElementValue(u.unmarshal(reader, Utils.getMethodReturnType(m)));
+                        Utils.setMethodValue(m, m2, obj, o);
                     }
                 }
-            }
-            return (Exception)obj;
+             }
+             return (Exception)obj;
         } catch (Exception e) {
-            throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
+             throw new Fault(new Message("MARSHAL_ERROR", LOG, e.getMessage()), e);
         }
-    }
+    } 
+    
 
     private static void writeObject(Marshaller u, Object source, Object mObj) throws Fault, JAXBException {
         if (source instanceof XMLStreamWriter) {

Modified: thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
===================================================================
--- thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java	2013-01-11 21:19:36 UTC (rev 17233)
+++ thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java	2013-01-11 23:08:57 UTC (rev 17234)
@@ -31,7 +31,6 @@
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlList;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@@ -517,35 +516,25 @@
         ct.setParticle(seq);
         String namespace = part.getElementQName().getNamespaceURI();
         
-        XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
-        if (accessorType == null && cls.getPackage() != null) {
-            accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
-        }
-        XmlAccessType accessType = accessorType != null ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;
-
-        
-        for (Field f : cls.getDeclaredFields()) {
-            if (JAXBContextInitializer.isFieldAccepted(f, accessType)) {
-                //map field
-                Type type = f.getGenericType();
-                JAXBBeanInfo beanInfo = getBeanInfo(type);
-                if (beanInfo != null) {
-                    addElement(seq, beanInfo, new QName(namespace, f.getName()), isArray(type));
+        XmlAccessType accessType = Utils.getXmlAccessType(cls);
+        for (Field f : Utils.getFields(cls, accessType)) {
+            //map field
+            Type type = Utils.getFieldType(f);
+            JAXBBeanInfo beanInfo = getBeanInfo(type);
+            if (beanInfo != null) {
+                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type));
                 }                
-            }
         }
-        for (Method m : cls.getMethods()) {
-            if (JAXBContextInitializer.isMethodAccepted(m, accessType)) {
-                //map field
-                Type type = m.getGenericReturnType();
-                JAXBBeanInfo beanInfo = getBeanInfo(type);
-                if (beanInfo != null) {
-                    int idx = m.getName().startsWith("get") ? 3 : 2;
-                    String name = m.getName().substring(idx);
-                    name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
-                    addElement(seq, beanInfo, new QName(namespace, name), isArray(type));
+        for (Method m : Utils.getGetters(cls, accessType)) {
+            //map method
+            Type type = Utils.getMethodReturnType(m);
+            JAXBBeanInfo beanInfo = getBeanInfo(type);
+            if (beanInfo != null) {
+                int idx = m.getName().startsWith("get") ? 3 : 2;
+                String name = m.getName().substring(idx);
+                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+                addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type));
                 }                
-            }
         }
         part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
     }
@@ -561,7 +550,8 @@
         return false;
     }
 
-    public void addElement(XmlSchemaSequence seq, JAXBBeanInfo beanInfo,
+    public void addElement(XmlSchema schema, 
+                           XmlSchemaSequence seq, JAXBBeanInfo beanInfo,
                            QName name, boolean isArray) {    
         XmlSchemaElement el = new XmlSchemaElement();
         el.setName(name.getLocalPart());

Copied: thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java (from rev 17225, thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java)
===================================================================
--- thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java	                        (rev 0)
+++ thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java	2013-01-11 23:08:57 UTC (rev 17234)
@@ -0,0 +1,305 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxb;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
+
+/**
+ * JAXB reflection utilities.
+ */
+final class Utils {
+    private Utils() {
+    }
+    
+    static XmlAccessType getXmlAccessType(Class<?> cls) {
+        XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
+        if (accessorType == null && cls.getPackage() != null) {
+            accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
+        }
+        return accessorType != null
+            ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;
+    }
+
+    static Collection<Field> getFields(Class<?> cls, XmlAccessType accessType) {
+        return getFieldsInternal(cls, accessType);
+    }
+
+    private static Collection<Field> getFieldsInternal(Class<?> cls, XmlAccessType accessType) {
+        Set<Field> fields = new HashSet<Field>();
+        Class<?> superClass = cls.getSuperclass();
+        if (superClass != null && !superClass.equals(Object.class) && !superClass.equals(Throwable.class)) {
+            // process super class until java.lang.Object or java.lang.Throwable is not reached
+            fields.addAll(getFieldsInternal(superClass, accessType));
+        }
+        // process current class
+        for (Field field : cls.getDeclaredFields()) {
+            if (JAXBContextInitializer.isFieldAccepted(field, accessType)) {
+                fields.add(field);
+            }
+        }
+        return fields;
+    }
+
+    private static Collection<Method> getMethods(Class<?> cls, XmlAccessType accessType, boolean acceptSetters) {
+        return getMethodsInternal(cls, accessType, acceptSetters);
+    }
+    
+    private static Collection<Method> getMethodsInternal(Class<?> cls, XmlAccessType accessType,
+            boolean acceptSetters) {
+        Set<Method> methods = new HashSet<Method>();
+        Class<?> superClass = cls.getSuperclass();
+        if (superClass != null && !superClass.equals(Object.class) && !superClass.equals(Throwable.class)) {
+            // process super class until java.lang.Object or java.lang.Throwable is not reached
+            methods.addAll(getMethodsInternal(superClass, accessType, acceptSetters));
+        }
+        // process current class
+        for (Method method : cls.getDeclaredMethods()) {
+            if (isMethodAccepted(method, accessType, acceptSetters)) {
+                methods.add(method);
+            }
+        }
+        return methods;
+    }
+    
+    static Method getMethod(Class<?> cls, XmlAccessType accessType, String methodName,
+            Class<?>... paramTypes) {
+        for (Method m : getMethods(cls, accessType, true)) {
+            if (m.getName().equals(methodName) && Arrays.equals(m.getParameterTypes(), paramTypes)) {
+                return m;
+            }
+        }
+        return null;
+    }
+    
+    static Field getField(Class<?> cls, XmlAccessType accessType, String fieldName) {
+        for (final Field f : getFields(cls, accessType)) {
+            if (f.getName().equals(fieldName)) {
+                return f;
+            }
+        }
+        return null;
+    }
+    
+    static Collection<Method> getGetters(Class<?> cls, XmlAccessType accessType) {
+        return getMethods(cls, accessType, false);
+    }
+
+    static boolean isMethodAccepted(Method method, XmlAccessType accessType, boolean acceptSetters) {
+        // ignore bridge, static, @XmlTransient methods plus methods declared in Throwable
+        if (method.isBridge()
+                || Modifier.isStatic(method.getModifiers())
+                || method.isAnnotationPresent(XmlTransient.class)
+                || method.getDeclaringClass().equals(Throwable.class)) {
+            return false;
+        }
+        // Allow only public methods if PUBLIC_MEMBER access is requested
+        if (accessType == XmlAccessType.PUBLIC_MEMBER && !Modifier.isPublic(method.getModifiers())) { 
+            return false;
+        }
+        if (isGetter(method)) {
+            // does nothing
+        } else if (isSetter(method)) {
+            if (!acceptSetters) {
+                return false;
+            }
+        } else {
+            // we accept only getters and setters
+            return false;
+        }
+        // let JAXB annotations decide if NONE or FIELD access is requested
+        if (accessType == XmlAccessType.NONE || accessType == XmlAccessType.FIELD) {
+            return JAXBContextInitializer.checkJaxbAnnotation(method.getAnnotations());
+        }
+        // method accepted
+        return true;
+    }
+    
+    private static boolean isGetter(Method m) {
+        if (m.getReturnType() != Void.class && m.getReturnType() != Void.TYPE && m.getParameterTypes().length == 0) {
+            Method setter = getSetter(m);
+            if (setter != null) {
+                return !setter.isAnnotationPresent(XmlTransient.class);
+            }
+        }
+        return false;
+    }
+    
+    private static Method getSetter(Method m) {
+        final int index = getterIndex(m.getName());
+        if (index != -1) {
+            String setterName = "set" + m.getName().substring(index);
+            Class<?> paramTypes = m.getReturnType();
+            return getDeclaredMethod(m.getDeclaringClass(), setterName, paramTypes);
+        }
+        return null;
+    }
+    
+    private static boolean isSetter(Method m) {
+        Class<?> declaringClass = m.getDeclaringClass();
+        boolean isVoidReturnType = m.getReturnType() == Void.class || m.getReturnType() == Void.TYPE; 
+        if (isVoidReturnType && m.getParameterTypes().length == 1 && m.getName().startsWith("set")) {
+            String getterName = "get" + m.getName().substring(3);
+            Class<?> setterParamType = m.getParameterTypes()[0];
+            Method getter = getDeclaredMethod(declaringClass, getterName);
+            if (getter != null && getter.getReturnType().equals(setterParamType)
+                    && !getter.isAnnotationPresent(XmlTransient.class)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static int getterIndex(String methodName) {
+        if (methodName.startsWith("is")) {
+            return 2;
+        }
+        if (methodName.startsWith("get")) {
+            return 3;
+        }
+        return -1;
+    }
+
+    private static Method getDeclaredMethod(Class<?> cls, String methodName, Class<?>... paramTypes) {
+        try {
+            return cls.getDeclaredMethod(methodName, paramTypes);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    static Class<?> getFieldType(Field f) {
+        XmlJavaTypeAdapter adapter = getFieldXJTA(f);
+        Class<?> adapterType = getTypeFromXmlAdapter(adapter);
+        return adapterType != null ? adapterType : f.getType();
+    }
+
+    static Class<?> getMethodReturnType(Method m) {
+        XmlJavaTypeAdapter adapter = getMethodXJTA(m);
+        Class<?> adapterType = getTypeFromXmlAdapter(adapter);
+        return adapterType != null ? adapterType : m.getReturnType(); 
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    static Object getFieldValue(Field f, Object target) throws Exception {
+        XmlJavaTypeAdapter adapterAnnotation = getFieldXJTA(f);
+        XmlAdapter adapter = getXmlAdapter(adapterAnnotation);
+        return adapter != null ? adapter.marshal(f.get(target)) : f.get(target); 
+    }
+
+    @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); 
+    }
+
+    @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);
+    }
+
+    @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);
+    }
+
+    @SuppressWarnings("rawtypes") 
+    static XmlAdapter getXmlAdapter(XmlJavaTypeAdapter adapterAnnotation)
+        throws InstantiationException, IllegalAccessException {
+        return adapterAnnotation != null ? adapterAnnotation.value().newInstance() : null;
+    }
+
+    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;
+    }
+
+    static XmlJavaTypeAdapter getMethodXJTA(final Method m) {
+        XmlJavaTypeAdapter adapter = m.getAnnotation(XmlJavaTypeAdapter.class);
+        if (adapter == null) {
+            Method setter = getSetter(m);
+            adapter = setter.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;
+                    }
+                }
+            }
+        }
+        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;
+    }
+
+}



More information about the jbossws-commits mailing list