From 0c5dbc4272d66de2c08a9a7078557121a8549be4 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Tue, 1 Jul 2025 10:33:05 +0200 Subject: [PATCH] clean up SerializationHelper this code was very old --- .../internal/util/SerializationHelper.java | 65 ++++--------------- 1 file changed, 12 insertions(+), 53 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java index d80a68b04cad..b7498d27e4c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java @@ -13,6 +13,7 @@ import java.io.ObjectStreamClass; import java.io.OutputStream; import java.io.Serializable; +import java.util.Objects; import org.hibernate.Hibernate; import org.hibernate.internal.CoreLogging; @@ -105,25 +106,12 @@ public static void serialize(Serializable obj, OutputStream outputStream) throws } } - ObjectOutputStream out = null; - try { - // stream closed in the finally - out = new ObjectOutputStream( outputStream ); + try ( var out = new ObjectOutputStream( outputStream ) ) { out.writeObject( obj ); - } catch (IOException ex) { throw new SerializationException( "could not serialize", ex ); } - finally { - try { - if ( out != null ) { - out.close(); - } - } - catch (IOException ignored) { - } - } } /** @@ -137,7 +125,7 @@ public static void serialize(Serializable obj, OutputStream outputStream) throws * @throws SerializationException (runtime) if the serialization fails */ public static byte[] serialize(Serializable obj) throws SerializationException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( 512 ); + final var byteArrayOutputStream = new ByteArrayOutputStream( 512 ); serialize( obj, byteArrayOutputStream ); return byteArrayOutputStream.toByteArray(); } @@ -199,7 +187,6 @@ public static Object deserialize(InputStream inputStream, ClassLoader loader) th return doDeserialize( inputStream, loader, defaultClassLoader(), hibernateClassLoader() ); } - @SuppressWarnings("unchecked") public static T doDeserialize( InputStream inputStream, ClassLoader loader, @@ -211,32 +198,11 @@ public static T doDeserialize( LOG.trace( "Starting deserialization of object" ); - try { - CustomObjectInputStream in = new CustomObjectInputStream( - inputStream, - loader, - fallbackLoader1, - fallbackLoader2 - ); - try { - return (T) in.readObject(); - } - catch (ClassNotFoundException e) { - throw new SerializationException( "could not deserialize", e ); - } - catch (IOException e) { - throw new SerializationException( "could not deserialize", e ); - } - finally { - try { - in.close(); - } - catch (IOException ignore) { - // ignore - } - } + try ( var in = new CustomObjectInputStream( inputStream, loader, fallbackLoader1, fallbackLoader2 ) ) { + //noinspection unchecked + return (T) in.readObject(); } - catch (IOException e) { + catch (ClassNotFoundException | IOException e) { throw new SerializationException( "could not deserialize", e ); } } @@ -310,7 +276,7 @@ private CustomObjectInputStream( } @Override - protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException { + protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException { final String className = v.getName(); LOG.tracev( "Attempting to locate class [{0}]", className ); @@ -321,7 +287,7 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo LOG.trace( "Unable to locate class using given classloader" ); } - if ( different( loader1, loader2 ) ) { + if ( !Objects.equals( loader1, loader2 ) ) { try { return Class.forName( className, false, loader2 ); } @@ -330,7 +296,7 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo } } - if ( different( loader1, loader3 ) && different( loader2, loader3 ) ) { + if ( !Objects.equals( loader1, loader3 ) && !Objects.equals( loader2, loader3 ) ) { try { return Class.forName( className, false, loader3 ); } @@ -339,16 +305,9 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo } } - // By default delegate to normal JDK deserialization which will use the class loader - // of the class which is calling this deserialization. + // By default, delegate to normal JDK deserialization which will use the + // class loader of the class which is calling this deserialization. return super.resolveClass( v ); } - - private boolean different(ClassLoader one, ClassLoader other) { - if ( one == null ) { - return other != null; - } - return !one.equals( other ); - } } }