Skip to content

Commit 8ff60c3

Browse files
author
Niall Pemberton
committed
IO-260 Fix ClassLoaderObjectInputStream does not handle Proxy classes - thanks to John Carrino
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1080833 13f79535-47bb-0310-9956-ffa450edef68
1 parent 476ccd8 commit 8ff60c3

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/main/java/org/apache/commons/io/input/ClassLoaderObjectInputStream.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.ObjectInputStream;
2222
import java.io.ObjectStreamClass;
2323
import java.io.StreamCorruptedException;
24+
import java.lang.reflect.Proxy;
2425

2526
/**
2627
* A special ObjectInputStream that loads a class based on a specified
@@ -75,4 +76,27 @@ protected Class<?> resolveClass(ObjectStreamClass objectStreamClass)
7576
return super.resolveClass(objectStreamClass);
7677
}
7778
}
79+
80+
/**
81+
* Create a proxy class that implements the specified interfaces using
82+
* the specified ClassLoader or the super ClassLoader.
83+
*
84+
* @param interfaces the interfaces to implemnt
85+
* @return the class
86+
* @see java.io.ObjectInputStream#resolveProxyClass(java.lang.String[])
87+
*/
88+
@Override
89+
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException,
90+
ClassNotFoundException {
91+
Class<?>[] interfaceClasses = new Class[interfaces.length];
92+
for (int i = 0; i < interfaces.length; i++) {
93+
interfaceClasses[i] = Class.forName(interfaces[i], false, classLoader);
94+
}
95+
try {
96+
return Proxy.getProxyClass(classLoader, interfaceClasses);
97+
} catch (IllegalArgumentException e) {
98+
return super.resolveProxyClass(interfaces);
99+
}
100+
}
101+
78102
}

src/test/java/org/apache/commons/io/input/ClassLoaderObjectInputStreamTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,19 @@ public void testExpected() throws Exception {
5454

5555
assertTrue( !result.booleanValue() );
5656
}
57+
58+
public void testResolveProxyClass() throws Exception {
59+
60+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
61+
ObjectOutputStream oos = new ObjectOutputStream(baos);
62+
oos.writeObject( Boolean.FALSE );
63+
InputStream bais = new ByteArrayInputStream(baos.toByteArray());
64+
65+
ClassLoaderObjectInputStream clois =
66+
new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais);
67+
String[] interfaces = new String[] { Comparable.class.getName() };
68+
Class<?> result = clois.resolveProxyClass(interfaces);
69+
assertTrue("Assignable", Comparable.class.isAssignableFrom(result));
70+
}
5771

5872
}

0 commit comments

Comments
 (0)