diff options
Diffstat (limited to 'sca-java-1.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java')
-rw-r--r-- | sca-java-1.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java | 62 |
1 files changed, 53 insertions, 9 deletions
diff --git a/sca-java-1.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java b/sca-java-1.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java index d129ae6256..7e42c660e7 100644 --- a/sca-java-1.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java +++ b/sca-java-1.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java @@ -24,8 +24,10 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import javax.xml.bind.JAXBContext; @@ -343,29 +345,54 @@ public class JAXBContextHelper { WrapperInfo inputWrapperInfo = op.getInputWrapper(); WrapperInfo outputWrapperInfo = op.getOutputWrapper(); + // TUSCANY-3298: Add the wrapper type instead of individual elements + // if possible. JAXB will implicitly add all types that are statically + // reachable from the wrapper class, with the exception of type arguments + // for parameterized types that aren't collections. + DataType dt1 = null; if (useWrapper && (inputWrapperInfo != null)) { - DataType dt1 = inputWrapperInfo.getWrapperType(); + dt1 = inputWrapperInfo.getWrapperType(); if (dt1 != null) { dataTypes.add(dt1); + for (DataType in : op.getInputType().getLogical()) { + if (isParameterizedNonCollectionType(in)) { + // JAXB won't add the type arguments, so we need to add them + dataTypes.add(in); + } + } + } + } + if (dt1 == null) { + // We couldn't add the wrapper, so add the elements individually + for (DataType dt : op.getInputType().getLogical()) { + dataTypes.add(dt); } } + + // TUSCANY-3298: Add the wrapper type instead of the output type + // if possible. JAXB will implicitly add all types that are statically + // reachable from the wrapper class, with the exception of type arguments + // for parameterized types that aren't collections or maps. + DataType dt2 = null; if (useWrapper && (outputWrapperInfo != null)) { - DataType dt2 = outputWrapperInfo.getWrapperType(); + dt2 = outputWrapperInfo.getWrapperType(); if (dt2 != null) { dataTypes.add(dt2); + DataType out = op.getOutputType(); + if (out != null && isParameterizedNonCollectionType(out)) { + // JAXB won't add the type arguments, so we need to add them + dataTypes.add(out); + } } } - // FIXME: [rfeng] We may need to find the referenced classes in the child types - // else - { - for (DataType dt1 : op.getInputType().getLogical()) { - dataTypes.add(dt1); - } - DataType dt2 = op.getOutputType(); + if (dt2 == null) { + // We couldn't add the wrapper, so add the output type directly + dt2 = op.getOutputType(); if (dt2 != null) { dataTypes.add(dt2); } } + for (DataType<DataType> dt3 : op.getFaultTypes()) { DataType dt4 = dt3.getLogical(); if (dt4 != null) { @@ -374,6 +401,23 @@ public class JAXBContextHelper { } } + /* + * We need to add parameterized non-collection types to the JAXB context + * explicitly, because type argument information for these types is erased + * from the generated wrapper bean. + */ + private static boolean isParameterizedNonCollectionType(DataType dt) { + Type type = dt.getGenericType(); + if (type instanceof ParameterizedType) { + Class physical = dt.getPhysical(); + if (!Collection.class.isAssignableFrom(physical) && + !Map.class.isAssignableFrom(physical)) { + return true; + } + } + return false; + } + @SuppressWarnings("unchecked") public static Class<?> getJavaType(DataType<?> dataType) { if (dataType == null) { |