diff --git a/src/main/java/org/apache/commons/lang3/SerializationUtils.java b/src/main/java/org/apache/commons/lang3/SerializationUtils.java index b766b2145..7c89b61d7 100644 --- a/src/main/java/org/apache/commons/lang3/SerializationUtils.java +++ b/src/main/java/org/apache/commons/lang3/SerializationUtils.java @@ -25,6 +25,8 @@ import java.io.ObjectOutputStream; import java.io.ObjectStreamClass; import java.io.OutputStream; import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; /** *

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.

*/ static class ClassLoaderAwareObjectInputStream extends ObjectInputStream { + private static final Map> primitiveTypes = + new HashMap>(); private ClassLoader classLoader; - + /** * Constructor. * @param in The InputStream. @@ -246,6 +250,16 @@ public class SerializationUtils { public ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException { super(in); 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 { return Class.forName(name, false, classLoader); } 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; + } } } diff --git a/src/site/changes/changes.xml b/src/site/changes/changes.xml index e859411ae..ec922c60c 100644 --- a/src/site/changes/changes.xml +++ b/src/site/changes/changes.xml @@ -22,6 +22,7 @@ + SerializationUtils throws ClassNotFoundException when cloning primitive classes StringUtils equals() relies on undefined behavior Documentation bug: StringUtils.split TypeUtilsTest contains incorrect type assignability assertion diff --git a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java index 01c98cd2d..9bf9c0d79 100644 --- a/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/SerializationUtilsTest.java @@ -364,6 +364,16 @@ public class SerializationUtilsTest extends TestCase { } 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); + } + } }