/** * Convert the desired object into the new object instance assignable from provided ClassLoader. * NOTE: This method is very expensive and provided object to convert must be {@link * java.io.Serializable}. * * @param obj the object to convert. Can be array as well. * @param classLoader the desired ClassLoader to convert the passed object. * @return the new instance of object derived from passed ClassLoader. * @throws IOException failed to convert **/ static public Object convertToAssignableClassLoader(Object obj, ClassLoader classLoader) throws IOException, ClassNotFoundException { GSByteArrayOutputStream oos = new GSByteArrayOutputStream(); MarshalOutputStream mos = new MarshalOutputStream(oos); mos.writeObject(obj); oos.flush(); GSByteArrayInputStream bais = new GSByteArrayInputStream(oos.toByteArray()); ClassLoader origCL = Thread.currentThread().getContextClassLoader(); try { ClassLoaderHelper.setContextClassLoader(classLoader, true); MarshalInputStream mis = new MarshalInputStream(bais); return mis.readObject(); } finally { ClassLoaderHelper.setContextClassLoader(origCL, true); } }