You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by em...@apache.org on 2013/12/09 07:02:48 UTC

svn commit: r1549444 - in /cxf/branches/2.5.x-fixes: ./ common/common/src/main/java/org/apache/cxf/common/util/ rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/ systests/uncategorized/src/test/java/org/apache/cxf/systest/exception/

Author: ema
Date: Mon Dec  9 06:02:47 2013
New Revision: 1549444

URL: http://svn.apache.org/r1549444
Log:
Merged revisions 1549386 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes

................
  r1549386 | ema | 2013-12-09 13:32:10 +0800 (Mon, 09 Dec 2013) | 20 lines
  
  Merged revisions 1549376 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/branches/2.7.x-fixes
  
  ................
    r1549376 | ema | 2013-12-09 11:37:23 +0800 (Mon, 09 Dec 2013) | 13 lines
    
    Merged revisions 1548471,1548738 via svnmerge from 
    https://svn.apache.org/repos/asf/cxf/trunk
    
    ........
      r1548471 | ema | 2013-12-06 17:43:39 +0800 (Fri, 06 Dec 2013) | 1 line
      
      [CXF-5437]:JAXBDataBinding can not handle the exception with generic objects like ObjectWithGenerics<Boolean, Integer>
    ........
      r1548738 | dkulp | 2013-12-07 06:41:00 +0800 (Sat, 07 Dec 2013) | 1 line
      
      Fix test failure
    ........
  ................
................

Added:
    cxf/branches/2.5.x-fixes/systests/uncategorized/src/test/java/org/apache/cxf/systest/exception/
      - copied from r1549386, cxf/branches/2.6.x-fixes/systests/uncategorized/src/test/java/org/apache/cxf/systest/exception/
Modified:
    cxf/branches/2.5.x-fixes/   (props changed)
    cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ASMHelper.java
    cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
    cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
    cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
    cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
    cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/branches/2.7.x-fixes:r1549376
  Merged /cxf/trunk:r1548471,1548738
  Merged /cxf/branches/2.6.x-fixes:r1549386

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ASMHelper.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ASMHelper.java?rev=1549444&r1=1549443&r2=1549444&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ASMHelper.java (original)
+++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ASMHelper.java Mon Dec  9 06:02:47 2013
@@ -73,7 +73,7 @@ public class ASMHelper {
         return buf.toString();
     }
     
-    protected static String periodToSlashes(String s) {
+    public static String periodToSlashes(String s) {
         char ch[] = s.toCharArray();
         for (int x = 0; x < ch.length; x++) {
             if (ch[x] == '.') {

Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java?rev=1549444&r1=1549443&r2=1549444&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java (original)
+++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java Mon Dec  9 06:02:47 2013
@@ -89,6 +89,19 @@ public final class ReflectionUtil {
         
     }
     
+    public static <T> Constructor<T>[] getDeclaredConstructors(final Class<T> cls) {
+        return AccessController.doPrivileged(new PrivilegedAction<Constructor<T>[]>() {
+            @SuppressWarnings("unchecked")
+            public Constructor<T>[] run() {
+                try {
+                    return (Constructor<T>[])cls.getDeclaredConstructors();
+                } catch (SecurityException e) {
+                    return null;
+                } 
+            }
+        });      
+    }
+        
     public static Method[] getDeclaredMethods(final Class<?> cls) {
         return AccessController.doPrivileged(new PrivilegedAction<Method[]>() {
             public Method[] run() {

Modified: cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java?rev=1549444&r1=1549443&r2=1549444&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java (original)
+++ cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java Mon Dec  9 06:02:47 2013
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxb;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.GenericArrayType;
 import java.lang.reflect.Method;
@@ -31,6 +32,7 @@ import java.lang.reflect.TypeVariable;
 import java.lang.reflect.WildcardType;
 import java.util.Collection;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import javax.xml.bind.annotation.XmlAccessType;
@@ -44,6 +46,7 @@ import javax.xml.bind.annotation.adapter
 import javax.xml.namespace.QName;
 
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.common.util.ASMHelper;
 import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.service.ServiceModelVisitor;
@@ -52,6 +55,9 @@ import org.apache.cxf.service.model.Mess
 import org.apache.cxf.service.model.OperationInfo;
 import org.apache.cxf.service.model.ServiceInfo;
 import org.apache.cxf.service.model.UnwrappedOperationInfo;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
 
 /**
  * Walks the service model and sets up the classes for the context.
@@ -61,13 +67,16 @@ class JAXBContextInitializer extends Ser
     private Set<Class<?>> classes;
     private Collection<Object> typeReferences;
     private Set<Class<?>> globalAdapters = new HashSet<Class<?>>();
+    private Map<String, Object> unmarshallerProperties;
 
     public JAXBContextInitializer(ServiceInfo serviceInfo,
                                   Set<Class<?>> classes,
-                                  Collection<Object> typeReferences) {
+                                  Collection<Object> typeReferences, 
+                                  Map<String, Object> unmarshallerProperties) {
         super(serviceInfo);
         this.classes = classes;
         this.typeReferences = typeReferences;
+        this.unmarshallerProperties = unmarshallerProperties;
     }
 
     @Override
@@ -284,19 +293,24 @@ class JAXBContextInitializer extends Ser
         }
     }
 
-
-    private  void addClass(Class<?> cls) {
-        if (Throwable.class.isAssignableFrom(cls)) {
-            if (!Throwable.class.equals(cls)
-                && !Exception.class.equals(cls)) {
-                walkReferences(cls);
+    void addClass(Class<?> claz) {
+        if (Throwable.class.isAssignableFrom(claz)) {
+            if (!Throwable.class.equals(claz)
+                && !Exception.class.equals(claz)) {
+                walkReferences(claz);
             }
             addClass(String.class);
-        } else if (cls.getName().startsWith("java.")
-            || cls.getName().startsWith("javax.")) {
+        } else if (claz.getName().startsWith("java.")
+            || claz.getName().startsWith("javax.")) {
             return;
         } else {
-            cls = JAXBUtils.getValidClass(cls);
+            Class<?> cls = JAXBUtils.getValidClass(claz);
+            if (cls == null && ReflectionUtil.getDeclaredConstructors(claz).length > 0) {
+                //there is no init(), but other constructors
+                Object factory = createFactory(claz, ReflectionUtil.getDeclaredConstructors(claz)[0]);
+                unmarshallerProperties.put("com.sun.xml.bind.ObjectFactory", factory);
+                cls = claz;
+            }
             if (null != cls) {
                 if (classes.contains(cls)) {
                     return;
@@ -332,10 +346,10 @@ class JAXBContextInitializer extends Ser
                 if (!cls.isInterface()) {
                     walkReferences(cls);
                 }
-            }
+            } 
         }
     }
-    
+ 
     private void walkReferences(Class<?> cls) {
         if (cls == null) {
             return;
@@ -480,4 +494,54 @@ class JAXBContextInitializer extends Ser
         }
         return false;
     }
+
+    @SuppressWarnings("unused") 
+    private Object createFactory(Class<?> cls, Constructor<?> contructor) {       
+        String newClassName = cls.getName() + "Factory";
+        ASMHelper helper = new ASMHelper();
+        ClassWriter cw = helper.createClassWriter();
+        MethodVisitor mv;
+
+        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
+                 ASMHelper.periodToSlashes(newClassName), null, "java/lang/Object", null);
+
+        cw.visitSource(cls.getSimpleName() + "Factory" + ".java", null);
+
+        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
+        mv.visitCode();
+        mv.visitVarInsn(Opcodes.ALOAD, 0);
+        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
+        mv.visitInsn(Opcodes.RETURN);
+        mv.visitMaxs(1, 1);
+        mv.visitEnd();
+
+        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "create" + cls.getSimpleName(),
+                            "()L" + ASMHelper.periodToSlashes(cls.getName()) + ";", null, null);
+        mv.visitCode();
+        String name = cls.getName().replace(".", "/");
+        mv.visitTypeInsn(Opcodes.NEW, name);
+        mv.visitInsn(Opcodes.DUP);
+        StringBuilder paraString = new StringBuilder("(");
+       
+        for (Class<?> paraClass : contructor.getParameterTypes()) {
+            mv.visitInsn(Opcodes.ACONST_NULL);
+            paraString.append("Ljava/lang/Object;");      
+        }
+        paraString.append(")V");
+
+        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", paraString.toString());
+
+        mv.visitInsn(Opcodes.ARETURN);
+        mv.visitMaxs(1, 1);
+        mv.visitEnd();
+
+        cw.visitEnd();
+        Class<?> factoryClass = helper.loadClass(newClassName, cls, cw.toByteArray());
+        try {
+            return factoryClass.newInstance();
+        } catch (Exception e) {
+           //ignore
+        } 
+        return null;
+    }   
 }

Modified: cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java?rev=1549444&r1=1549443&r2=1549444&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java (original)
+++ cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java Mon Dec  9 06:02:47 2013
@@ -274,9 +274,12 @@ public class JAXBDataBinding extends Abs
 
 
         contextClasses = new LinkedHashSet<Class<?>>();
+        Map<String, Object> unmarshallerProps = new HashMap<String, Object>();
+        this.setUnmarshallerProperties(unmarshallerProps);
         for (ServiceInfo serviceInfo : service.getServiceInfos()) {
+            
             JAXBContextInitializer initializer
-                = new JAXBContextInitializer(serviceInfo, contextClasses, typeRefs);
+                = new JAXBContextInitializer(serviceInfo, contextClasses, typeRefs, this.getUnmarshallerProperties());
             initializer.walk();
             if (serviceInfo.getProperty("extra.class") != null) {
                 Set<Class<?>> exClasses = serviceInfo.getProperty("extra.class", Set.class);

Modified: cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java?rev=1549444&r1=1549443&r2=1549444&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java (original)
+++ cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java Mon Dec  9 06:02:47 2013
@@ -26,6 +26,7 @@ import java.lang.reflect.GenericArrayTyp
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -537,9 +538,13 @@ class JAXBSchemaInitializer extends Serv
             if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
                 type = f.getGenericType();
             }
-            JAXBBeanInfo beanInfo = getBeanInfo(type);
-            if (beanInfo != null) {
-                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type));
+            if (generateGenericType(type)) {
+                buildGenericElements(schema, seq, f);
+            } else {
+                JAXBBeanInfo beanInfo = getBeanInfo(type);
+                if (beanInfo != null) {
+                    addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type));
+                }
             }
         }
         for (Method m : Utils.getGetters(cls, accessType)) {
@@ -551,12 +556,17 @@ class JAXBSchemaInitializer extends Serv
             if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
                 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(schema, seq, beanInfo, new QName(namespace, name), isArray(type));
+            
+            if (generateGenericType(type)) {
+                buildGenericElements(schema, seq, m, type);
+            } else {
+                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));
+                }
             }
         }
         // Create element in xsd:sequence for Exception.class
@@ -585,6 +595,106 @@ class JAXBSchemaInitializer extends Serv
         part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
     }
     
+    private boolean generateGenericType(Type type) {
+        if (type instanceof ParameterizedType) {
+            ParameterizedType paramType = (ParameterizedType)type;
+            if (paramType.getActualTypeArguments().length > 1) {
+                return true;
+
+            }
+        }
+        return false;
+    }
+    
+    private void buildGenericElements(XmlSchema schema, XmlSchemaSequence seq, Field f) {
+        XmlSchemaComplexType generics = new XmlSchemaComplexType(schema, true);
+        Type type = f.getGenericType();
+        String rawType = ((ParameterizedType)type).getRawType().toString();
+        String typeName = StringUtils.uncapitalize(rawType.substring(rawType.lastIndexOf(".") + 1));
+        generics.setName(typeName);
+
+        Class<?> genericsClass = f.getType();
+        buildGenericSeq(schema, generics, genericsClass);   
+
+        String name = Character.toLowerCase(f.getName().charAt(0)) + f.getName().substring(1);
+        XmlSchemaElement newel = new XmlSchemaElement(schema, false);
+        newel.setName(name);
+        newel.setSchemaTypeName(generics.getQName());
+        newel.setMinOccurs(0);
+        if (!seq.getItems().contains(newel)) {
+            seq.getItems().add(newel);
+        }
+    }
+     
+    private void buildGenericElements(XmlSchema schema, XmlSchemaSequence seq, Method m, Type type) {       
+        String rawType = ((ParameterizedType)type).getRawType().toString();
+        String typeName = StringUtils.uncapitalize(rawType.substring(rawType.lastIndexOf(".") + 1));
+        
+        XmlSchemaComplexType generics = (XmlSchemaComplexType)schema.getTypeByName(typeName);        
+        if (generics == null) {
+            generics =  new XmlSchemaComplexType(schema, true);
+            generics.setName(typeName);
+        }
+        
+        Class<?> genericsClass = m.getReturnType();
+        buildGenericSeq(schema, generics, genericsClass);  
+          
+        int idx = m.getName().startsWith("get") ? 3 : 2;
+        String name = m.getName().substring(idx);
+        name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+        XmlSchemaElement newel = new XmlSchemaElement(schema, false);
+        newel.setName(name);
+        newel.setSchemaTypeName(generics.getQName());
+        newel.setMinOccurs(0);
+        if (!seq.getItems().contains(newel)) {
+            seq.getItems().add(newel);
+        }
+    }
+    
+    private void buildGenericSeq(XmlSchema schema, XmlSchemaComplexType generics, Class<?> genericsClass) {
+        XmlSchemaSequence genericsSeq = new XmlSchemaSequence();
+        generics.setParticle(genericsSeq);
+        XmlAccessType  accessType = Utils.getXmlAccessType(genericsClass);
+        
+        for (Field f : Utils.getFields(genericsClass, accessType)) {
+            if (f.getGenericType() instanceof TypeVariable) {               
+                String genericName = Character.toLowerCase(f.getName().charAt(0)) + f.getName().substring(1);
+                XmlSchemaElement genericEle = new XmlSchemaElement(schema, false);
+                genericEle.setName(genericName);
+                genericEle.setMinOccurs(0);
+                JAXBBeanInfo anyBean = getBeanInfo(context, f.getType()); 
+                Iterator<QName> itr = anyBean.getTypeNames().iterator();
+                if (!itr.hasNext()) {
+                    return;
+                }
+                QName typeName = itr.next();
+                genericEle.setSchemaTypeName(typeName);
+                genericsSeq.getItems().add(genericEle);
+            }
+        }
+               
+        for (Method genericMethod : Utils.getGetters(genericsClass, accessType)) {
+            if (genericMethod.getGenericReturnType() instanceof TypeVariable) {
+                int idx = genericMethod.getName().startsWith("get") ? 3 : 2;
+                String genericName = genericMethod.getName().substring(idx);
+                genericName = Character.toLowerCase(genericName.charAt(0)) + genericName.substring(1);
+                XmlSchemaElement genericEle = new XmlSchemaElement(schema, false);
+                genericEle.setName(genericName);
+                genericEle.setMinOccurs(0);
+                JAXBBeanInfo anyBean = getBeanInfo(context, genericMethod.getReturnType()); 
+                Iterator<QName> itr = anyBean.getTypeNames().iterator();
+                if (!itr.hasNext()) {
+                    return;
+                }
+                QName typeName = itr.next();
+                genericEle.setSchemaTypeName(typeName);
+                genericsSeq.getItems().add(genericEle);
+            }
+            
+        } 
+    }
+    
+    
     static boolean isArray(Type cls) {
         if (cls instanceof Class) {
             return ((Class<?>)cls).isArray();

Modified: cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java?rev=1549444&r1=1549443&r2=1549444&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java (original)
+++ cxf/branches/2.5.x-fixes/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/Utils.java Mon Dec  9 06:02:47 2013
@@ -205,7 +205,8 @@ final class Utils {
 
     static Class<?> getFieldType(Field f) {
         XmlJavaTypeAdapter adapter = getFieldXJTA(f);
-        if (adapter == null && f.getGenericType() instanceof ParameterizedType) {
+        if (adapter == null && f.getGenericType() instanceof ParameterizedType
+            && ((ParameterizedType)f.getGenericType()).getActualTypeArguments().length == 1) {
             return null;
         }
         Class<?> adapterType = (Class<?>)getTypeFromXmlAdapter(adapter);
@@ -217,7 +218,8 @@ final class Utils {
         // if there is no adapter, yet we have a collection make sure
         // we return the Generic type; if there is an annotation let the
         // adapter handle what gets populated
-        if (adapter == null && m.getGenericReturnType() instanceof ParameterizedType) {
+        if (adapter == null && m.getGenericReturnType() instanceof ParameterizedType 
+            && ((ParameterizedType)m.getGenericReturnType()).getActualTypeArguments().length < 2) {
             return null;
         }
         Class<?> adapterType = (Class<?>)getTypeFromXmlAdapter(adapter);