@Override @SuppressWarnings({ "unchecked" }) public S deepCopyNotNull(S value) { return (S) SerializationHelper.clone( value ); }
/** * <p>Deep clone an <code>Object</code> using serialization.</p> * <p/> * <p>This is many times slower than writing clone methods by hand * on all objects in your object graph. However, for complex object * graphs, or for those that don't support deep cloning this can * be a simple alternative implementation. Of course all the objects * must be <code>Serializable</code>.</p> * * @param object the <code>Serializable</code> object to clone * * @return the cloned object * * @throws SerializationException (runtime) if the serialization fails */ public static Object clone(Serializable object) throws SerializationException { LOG.trace( "Starting clone through serialization" ); if ( object == null ) { return null; } return deserialize( serialize( object ), object.getClass().getClassLoader() ); }
/** * Deserializes an object from an array of bytes. * <p/> * Delegates to {@link #deserialize(java.io.InputStream, ClassLoader)} using a * {@link ByteArrayInputStream} to wrap the array. * * @param objectData the serialized object, must not be null * @param loader The classloader to use * * @return the deserialized object * * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(byte[] objectData, ClassLoader loader) throws SerializationException { return doDeserialize( wrap( objectData ), loader, defaultClassLoader(), hibernateClassLoader() ); }
/** * Deserializes an object from the specified stream using the Thread Context * ClassLoader (TCCL). * <p/> * Delegates to {@link #doDeserialize} * * @param inputStream the serialized object input stream, must not be null * * @return the deserialized object * * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static <T> T deserialize(InputStream inputStream) throws SerializationException { return doDeserialize( inputStream, defaultClassLoader(), hibernateClassLoader(), null ); }
public void testSerDeserClassUnknownToCustomLoader() throws Exception { Object instance = LockMode.OPTIMISTIC; assertSame( SerializationHelper.hibernateClassLoader(), instance.getClass().getClassLoader() ); // SerializableType.toBytes() logic, as called from SerializableType.disassemble() byte[] bytes = SerializationHelper.serialize( (Serializable) instance ); // SerializableType.fromBytes() logic, as called from SerializableType.assemble // NOTE : specifically we use custom so that LockType.class is not found // until the 3rd loader (because loader1 == loader2, the custom classloader) Object instance2 = SerializationHelper.deserialize( bytes, custom ); assertSame( instance.getClass(), instance2.getClass() ); assertSame( instance.getClass().getClassLoader(), instance2.getClass().getClassLoader() ); }
protected byte[] toBytes(T value) { return SerializationHelper.serialize( value ); }
@SuppressWarnings({ "unchecked" }) protected T fromBytes(byte[] bytes) { return (T) SerializationHelper.deserialize( bytes, getJavaType().getClassLoader() ); } }
/** * Deserializes an object from the specified stream using the Thread Context * ClassLoader (TCCL). If there is no TCCL set, the classloader of the calling * class is used. * <p/> * The stream will be closed once the object is read. This avoids the need * for a finally clause, and maybe also exception handling, in the application * code. * <p/> * The stream passed in is not buffered internally within this method. This is * the responsibility of the caller, if desired. * * @param inputStream the serialized object input stream, must not be null * @param loader The classloader to use * * @return the deserialized object * * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(InputStream inputStream, ClassLoader loader) throws SerializationException { return doDeserialize( inputStream, loader, defaultClassLoader(), hibernateClassLoader() ); }
/** * <p>Serializes an <code>Object</code> to a byte array for * storage/serialization.</p> * * @param obj the object to serialize to bytes * * @return a byte[] with the converted Serializable * * @throws SerializationException (runtime) if the serialization fails */ public static byte[] serialize(Serializable obj) throws SerializationException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( 512 ); serialize( obj, byteArrayOutputStream ); return byteArrayOutputStream.toByteArray(); }
private <T> T readSerFile() throws SerializationException, FileNotFoundException { log.readingCachedMappings( serFile ); return SerializationHelper.deserialize( new FileInputStream( serFile ) ); }
/** * Deserializes an object from an array of bytes using the Thread Context * ClassLoader (TCCL). If there is no TCCL set, the classloader of the calling * class is used. * <p/> * Delegates to {@link #deserialize(byte[], ClassLoader)} * * @param objectData the serialized object, must not be null * * @return the deserialized object * * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(byte[] objectData) throws SerializationException { return doDeserialize( wrap( objectData ), defaultClassLoader(), hibernateClassLoader(), null ); }
/** * Deserializes an object from the specified stream using the Thread Context * ClassLoader (TCCL). * <p/> * Delegates to {@link #doDeserialize} * * @param inputStream the serialized object input stream, must not be null * * @return the deserialized object * * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(InputStream inputStream) throws SerializationException { return doDeserialize( inputStream, defaultClassLoader(), hibernateClassLoader(), null ); }
@Test public void testSerializeDeserialize() throws Exception { Class clazz = Thread.currentThread().getContextClassLoader().loadClass( "org.hibernate.test.util.SerializableThing" ); Object instance = clazz.newInstance(); // SerializableType.toBytes() logic, as called from SerializableType.disassemble() byte[] bytes = SerializationHelper.serialize( (Serializable) instance ); // SerializableType.fromBytes() logic, as called from SerializableType.assemble // NOTE : specifically we use Serializable.class.getClassLoader for the CL in many cases // which are the problematic cases Object instance2 = SerializationHelper.deserialize( bytes, Serializable.class.getClassLoader() ); assertEquals( instance.getClass(), instance2.getClass() ); assertEquals( instance.getClass().getClassLoader(), instance2.getClass().getClassLoader() ); assertEquals( custom, instance2.getClass().getClassLoader() ); }
private void doResultTransformerTest(ResultTransformer transformer, boolean isSingleton) { Map transformerMap = new HashMap(); transformerMap.put( transformer, "" ); assert transformerMap.size() == 1 : "really messed up"; Object old = transformerMap.put( transformer, "value" ); assert old != null && transformerMap.size() == 1 : "apparent QueryKey equals/hashCode issue"; // finally, lets serialize it and see what happens ResultTransformer transformer2 = ( ResultTransformer ) SerializationHelper.clone( transformer ); old = transformerMap.put( transformer2, "new value" ); assert old != null && transformerMap.size() == 1 : "deserialization did not set hashCode or equals properly"; if ( isSingleton ) { assert transformer == transformer2: "deserialization issue for singleton transformer"; } else { assert transformer != transformer2: "deserialization issue for non-singleton transformer"; } assert transformer.equals( transformer2 ): "deep copy issue"; }
private static void writeSerFile(Serializable binding, File xmlFile, File serFile) { try { log.debugf( "Writing cache file for: %s to: %s", xmlFile.getAbsolutePath(), serFile.getAbsolutePath() ); SerializationHelper.serialize( binding, new FileOutputStream( serFile ) ); boolean success = serFile.setLastModified( System.currentTimeMillis() ); if ( !success ) { log.warn( "Could not update cacheable hbm.xml bin file timestamp" ); } } catch ( Exception e ) { log.unableToWriteCachedFile( serFile.getAbsolutePath(), e.getMessage() ); } }
@Override @SuppressWarnings("unchecked") public <X> T wrap(X value, SharedSessionContractImplementor session) { if ( value == null ) { return null; } if ( value.getClass().equals( byte[].class ) ) { throw new UnsupportedOperationException( "Cannot unwrap Serializable to format other than byte[]" ); } final byte[] bytes = (byte[]) value; return (T) SerializationHelper.deserialize( bytes ); } }
/** * Deserializes an object from an array of bytes using the Thread Context * ClassLoader (TCCL). If there is no TCCL set, the classloader of the calling * class is used. * <p/> * Delegates to {@link #deserialize(byte[], ClassLoader)} * * @param objectData the serialized object, must not be null * * @return the deserialized object * * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(byte[] objectData) throws SerializationException { return doDeserialize( wrap( objectData ), defaultClassLoader(), hibernateClassLoader(), null ); }
/** * Deserializes an object from the specified stream using the Thread Context * ClassLoader (TCCL). * <p/> * Delegates to {@link #doDeserialize} * * @param inputStream the serialized object input stream, must not be null * * @return the deserialized object * * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code> * @throws SerializationException (runtime) if the serialization fails */ public static Object deserialize(InputStream inputStream) throws SerializationException { return doDeserialize( inputStream, defaultClassLoader(), hibernateClassLoader(), null ); }
s.close(); foo = (Foo) SerializationHelper.deserialize( SerializationHelper.serialize(foo) );
@Test public void testClassType() { final Class original = TypeTest.class; final Class copy = (Class) SerializationHelper.clone( original ); final Class different = String.class; runBasicTests( ClassType.INSTANCE, original, copy, different ); }