Skip to content

Commit

Permalink
Support byte[] in JaxbMarshaller under JDK 7
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
poutsma committed Feb 27, 2012
1 parent 7ca5fba commit 83f3750
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +76,7 @@
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;
Expand Down Expand Up @@ -502,10 +503,17 @@ public boolean supports(Type genericType) {
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);
}
Expand Down Expand Up @@ -867,13 +875,13 @@ public boolean isXOPPackage() {
*/
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;
Expand Down

0 comments on commit 83f3750

Please sign in to comment.