in xstream/src/java/com/thoughtworks/xstream/converters/reflection/PureJavaReflectionProvider.java [102:148]
private Object instantiateUsingSerialization(final Class<?> type) {
ObjectAccessException oaex = null;
try {
if (Reflections.newInstance != null) {
final ObjectStreamClass osClass = objectStreamClassCache
.computeIfAbsent(type, t -> ObjectStreamClass.lookup(type));
return Reflections.newInstance.invoke(osClass);
}
final byte[] data = serializedDataCache.computeIfAbsent(type, t -> {
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final DataOutputStream stream = new DataOutputStream(bytes);
try {
stream.writeShort(ObjectStreamConstants.STREAM_MAGIC);
stream.writeShort(ObjectStreamConstants.STREAM_VERSION);
stream.writeByte(ObjectStreamConstants.TC_OBJECT);
stream.writeByte(ObjectStreamConstants.TC_CLASSDESC);
stream.writeUTF(t.getName());
stream.writeLong(ObjectStreamClass.lookup(t).getSerialVersionUID());
stream.writeByte(2); // classDescFlags (2 = Serializable)
stream.writeShort(0); // field count
stream.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
stream.writeByte(ObjectStreamConstants.TC_NULL);
} catch (final IOException e) {
throw new ObjectAccessException("Cannot prepare data to create type by JDK serialization", e);
}
return bytes.toByteArray();
});
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data)) {
@Override
protected Class<?> resolveClass(final ObjectStreamClass desc) throws ClassNotFoundException {
return Class.forName(desc.getName(), false, type.getClassLoader());
}
};
return in.readObject();
} catch (final ObjectAccessException e) {
oaex = e;
} catch (final IOException e) {
oaex = new ObjectAccessException("Cannot create type by JDK serialization", e);
} catch (final ClassNotFoundException e) {
oaex = new ObjectAccessException("Cannot find class", e);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
oaex = new ObjectAccessException("Cannot create type by JDK object stream data", e);
}
oaex.add("construction-type", type.getName());
throw oaex;
}