Skip to content

Commit bda2c64

Browse files
committed
clean up SerializationHelper
this code was very old
1 parent 36625c1 commit bda2c64

File tree

1 file changed

+12
-53
lines changed

1 file changed

+12
-53
lines changed

hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.ObjectStreamClass;
1414
import java.io.OutputStream;
1515
import java.io.Serializable;
16+
import java.util.Objects;
1617

1718
import org.hibernate.Hibernate;
1819
import org.hibernate.internal.CoreLogging;
@@ -105,25 +106,12 @@ public static void serialize(Serializable obj, OutputStream outputStream) throws
105106
}
106107
}
107108

108-
ObjectOutputStream out = null;
109-
try {
110-
// stream closed in the finally
111-
out = new ObjectOutputStream( outputStream );
109+
try ( var out = new ObjectOutputStream( outputStream ) ) {
112110
out.writeObject( obj );
113-
114111
}
115112
catch (IOException ex) {
116113
throw new SerializationException( "could not serialize", ex );
117114
}
118-
finally {
119-
try {
120-
if ( out != null ) {
121-
out.close();
122-
}
123-
}
124-
catch (IOException ignored) {
125-
}
126-
}
127115
}
128116

129117
/**
@@ -137,7 +125,7 @@ public static void serialize(Serializable obj, OutputStream outputStream) throws
137125
* @throws SerializationException (runtime) if the serialization fails
138126
*/
139127
public static byte[] serialize(Serializable obj) throws SerializationException {
140-
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( 512 );
128+
final var byteArrayOutputStream = new ByteArrayOutputStream( 512 );
141129
serialize( obj, byteArrayOutputStream );
142130
return byteArrayOutputStream.toByteArray();
143131
}
@@ -199,7 +187,6 @@ public static Object deserialize(InputStream inputStream, ClassLoader loader) th
199187
return doDeserialize( inputStream, loader, defaultClassLoader(), hibernateClassLoader() );
200188
}
201189

202-
@SuppressWarnings("unchecked")
203190
public static <T> T doDeserialize(
204191
InputStream inputStream,
205192
ClassLoader loader,
@@ -211,32 +198,11 @@ public static <T> T doDeserialize(
211198

212199
LOG.trace( "Starting deserialization of object" );
213200

214-
try {
215-
CustomObjectInputStream in = new CustomObjectInputStream(
216-
inputStream,
217-
loader,
218-
fallbackLoader1,
219-
fallbackLoader2
220-
);
221-
try {
222-
return (T) in.readObject();
223-
}
224-
catch (ClassNotFoundException e) {
225-
throw new SerializationException( "could not deserialize", e );
226-
}
227-
catch (IOException e) {
228-
throw new SerializationException( "could not deserialize", e );
229-
}
230-
finally {
231-
try {
232-
in.close();
233-
}
234-
catch (IOException ignore) {
235-
// ignore
236-
}
237-
}
201+
try ( var in = new CustomObjectInputStream( inputStream, loader, fallbackLoader1, fallbackLoader2 ) ) {
202+
//noinspection unchecked
203+
return (T) in.readObject();
238204
}
239-
catch (IOException e) {
205+
catch (ClassNotFoundException | IOException e) {
240206
throw new SerializationException( "could not deserialize", e );
241207
}
242208
}
@@ -310,7 +276,7 @@ private CustomObjectInputStream(
310276
}
311277

312278
@Override
313-
protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException {
279+
protected Class<?> resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException {
314280
final String className = v.getName();
315281
LOG.tracev( "Attempting to locate class [{0}]", className );
316282

@@ -321,7 +287,7 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo
321287
LOG.trace( "Unable to locate class using given classloader" );
322288
}
323289

324-
if ( different( loader1, loader2 ) ) {
290+
if ( !Objects.equals( loader1, loader2 ) ) {
325291
try {
326292
return Class.forName( className, false, loader2 );
327293
}
@@ -330,7 +296,7 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo
330296
}
331297
}
332298

333-
if ( different( loader1, loader3 ) && different( loader2, loader3 ) ) {
299+
if ( !Objects.equals( loader1, loader3 ) && !Objects.equals( loader2, loader3 ) ) {
334300
try {
335301
return Class.forName( className, false, loader3 );
336302
}
@@ -339,16 +305,9 @@ protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFo
339305
}
340306
}
341307

342-
// By default delegate to normal JDK deserialization which will use the class loader
343-
// of the class which is calling this deserialization.
308+
// By default, delegate to normal JDK deserialization which will use the
309+
// class loader of the class which is calling this deserialization.
344310
return super.resolveClass( v );
345311
}
346-
347-
private boolean different(ClassLoader one, ClassLoader other) {
348-
if ( one == null ) {
349-
return other != null;
350-
}
351-
return !one.equals( other );
352-
}
353312
}
354313
}

0 commit comments

Comments
 (0)