[LANG-788] SerializationUtils throws ClassNotFoundException when cloning primitive classes

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1295134 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2012-02-29 14:39:01 +00:00
parent 81797ed440
commit e9429c05e1
3 changed files with 35 additions and 2 deletions

View File

@ -25,6 +25,8 @@ import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass; import java.io.ObjectStreamClass;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/** /**
* <p>Assists with the serialization process and performs additional functionality based * <p>Assists with the serialization process and performs additional functionality based
@ -234,8 +236,10 @@ public class SerializationUtils {
* class here is a workaround, see the JIRA issue LANG-626. </p> * class here is a workaround, see the JIRA issue LANG-626. </p>
*/ */
static class ClassLoaderAwareObjectInputStream extends ObjectInputStream { static class ClassLoaderAwareObjectInputStream extends ObjectInputStream {
private static final Map<String, Class<?>> primitiveTypes =
new HashMap<String, Class<?>>();
private ClassLoader classLoader; private ClassLoader classLoader;
/** /**
* Constructor. * Constructor.
* @param in The <code>InputStream</code>. * @param in The <code>InputStream</code>.
@ -246,6 +250,16 @@ public class SerializationUtils {
public ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException { public ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException {
super(in); super(in);
this.classLoader = classLoader; this.classLoader = classLoader;
primitiveTypes.put("byte", byte.class);
primitiveTypes.put("short", short.class);
primitiveTypes.put("int", int.class);
primitiveTypes.put("long", long.class);
primitiveTypes.put("float", float.class);
primitiveTypes.put("double", double.class);
primitiveTypes.put("boolean", boolean.class);
primitiveTypes.put("char", char.class);
primitiveTypes.put("void", void.class);
} }
/** /**
@ -262,7 +276,15 @@ public class SerializationUtils {
try { try {
return Class.forName(name, false, classLoader); return Class.forName(name, false, classLoader);
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
return Class.forName(name, false, Thread.currentThread().getContextClassLoader()); try {
return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException cnfe) {
Class<?> cls = primitiveTypes.get(name);
if (cls != null)
return cls;
else
throw cnfe;
}
} }
} }

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.2" date="TBA" description="Next release"> <release version="3.2" date="TBA" description="Next release">
<action type="fix" issue="LANG-788">SerializationUtils throws ClassNotFoundException when cloning primitive classes</action>
<action type="fix" issue="LANG-786">StringUtils equals() relies on undefined behavior</action> <action type="fix" issue="LANG-786">StringUtils equals() relies on undefined behavior</action>
<action type="fix" issue="LANG-783">Documentation bug: StringUtils.split</action> <action type="fix" issue="LANG-783">Documentation bug: StringUtils.split</action>
<action type="fix" issue="LANG-776">TypeUtilsTest contains incorrect type assignability assertion</action> <action type="fix" issue="LANG-776">TypeUtilsTest contains incorrect type assignability assertion</action>

View File

@ -364,6 +364,16 @@ public class SerializationUtilsTest extends TestCase {
} }
fail(); fail();
} }
public void testPrimitiveTypeClassSerialization() {
Class<?>[] primitiveTypes = { byte.class, short.class, int.class, long.class, float.class, double.class,
boolean.class, char.class, void.class };
for (Class<?> primitiveType : primitiveTypes) {
Class<?> clone = SerializationUtils.clone(primitiveType);
assertEquals(primitiveType, clone);
}
}
} }