From 83f3750fb3a74d0ac6c7c513b29028b7d49f7b30 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 27 Feb 2012 11:35:26 +0100 Subject: [PATCH] Support byte[] in JaxbMarshaller under JDK 7 JDK7 changed its reflections API in order to resolve http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041784 In short, JDK 5 and 6 (wrongly) return GenericArrayTypes in certain reflection scenarios, whereas JDK 7 changed this to return a normal array type. For Jaxb2Marshaller, this meant that marshaling byte arrays was not supported under JDK 7. This change fixes that, so that Jaxb2Marhsaller supports marshalling byte arrays again (under JDK 5, 6 or 7). --- .../oxm/jaxb/Jaxb2Marshaller.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index e16ba7e6e9..9c6d621c4c 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -16,7 +16,7 @@ package org.springframework.oxm.jaxb; -import java.awt.*; +import java.awt.Image; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -76,6 +76,7 @@ import org.xml.sax.helpers.XMLReaderFactory; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ResourceLoaderAware; +import org.springframework.core.JdkVersion; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; @@ -502,10 +503,17 @@ public class Jaxb2Marshaller Type typeArgument = parameterizedType.getActualTypeArguments()[0]; if (typeArgument instanceof Class) { Class classArgument = (Class) typeArgument; - return (isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) || - supportsInternal(classArgument, false)); + if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_17 && classArgument.isArray()) { + return classArgument.getComponentType().equals(Byte.TYPE); + } + else { + return (isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) || + supportsInternal(classArgument, false)); + } } - else if (typeArgument instanceof GenericArrayType) { + else if (JdkVersion.getMajorJavaVersion() <= JdkVersion.JAVA_16 && + typeArgument instanceof GenericArrayType) { + // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041784 GenericArrayType arrayType = (GenericArrayType) typeArgument; return arrayType.getGenericComponentType().equals(Byte.TYPE); } @@ -867,13 +875,13 @@ public class Jaxb2Marshaller */ private static class ByteArrayDataSource implements DataSource { - private byte[] data; + private final byte[] data; - private String contentType; + private final String contentType; - private int offset; + private final int offset; - private int length; + private final int length; private ByteArrayDataSource(String contentType, byte[] data, int offset, int length) { this.contentType = contentType;