Skip to content

Commit 83f3750

Browse files
committed
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).
1 parent 7ca5fba commit 83f3750

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.oxm.jaxb;
1818

19-
import java.awt.*;
19+
import java.awt.Image;
2020
import java.io.ByteArrayInputStream;
2121
import java.io.IOException;
2222
import java.io.InputStream;
@@ -76,6 +76,7 @@
7676
import org.springframework.beans.factory.BeanClassLoaderAware;
7777
import org.springframework.beans.factory.InitializingBean;
7878
import org.springframework.context.ResourceLoaderAware;
79+
import org.springframework.core.JdkVersion;
7980
import org.springframework.core.annotation.AnnotationUtils;
8081
import org.springframework.core.io.Resource;
8182
import org.springframework.core.io.ResourceLoader;
@@ -502,10 +503,17 @@ public boolean supports(Type genericType) {
502503
Type typeArgument = parameterizedType.getActualTypeArguments()[0];
503504
if (typeArgument instanceof Class) {
504505
Class<?> classArgument = (Class<?>) typeArgument;
505-
return (isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) ||
506-
supportsInternal(classArgument, false));
506+
if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_17 && classArgument.isArray()) {
507+
return classArgument.getComponentType().equals(Byte.TYPE);
508+
}
509+
else {
510+
return (isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) ||
511+
supportsInternal(classArgument, false));
512+
}
507513
}
508-
else if (typeArgument instanceof GenericArrayType) {
514+
else if (JdkVersion.getMajorJavaVersion() <= JdkVersion.JAVA_16 &&
515+
typeArgument instanceof GenericArrayType) {
516+
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041784
509517
GenericArrayType arrayType = (GenericArrayType) typeArgument;
510518
return arrayType.getGenericComponentType().equals(Byte.TYPE);
511519
}
@@ -867,13 +875,13 @@ public boolean isXOPPackage() {
867875
*/
868876
private static class ByteArrayDataSource implements DataSource {
869877

870-
private byte[] data;
878+
private final byte[] data;
871879

872-
private String contentType;
880+
private final String contentType;
873881

874-
private int offset;
882+
private final int offset;
875883

876-
private int length;
884+
private final int length;
877885

878886
private ByteArrayDataSource(String contentType, byte[] data, int offset, int length) {
879887
this.contentType = contentType;

0 commit comments

Comments
 (0)