Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}
}

/**
Expand All @@ -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();
}
Expand Down Expand Up @@ -199,7 +187,6 @@ public static Object deserialize(InputStream inputStream, ClassLoader loader) th
return doDeserialize( inputStream, loader, defaultClassLoader(), hibernateClassLoader() );
}

@SuppressWarnings("unchecked")
public static <T> T doDeserialize(
InputStream inputStream,
ClassLoader loader,
Expand All @@ -211,32 +198,11 @@ public static <T> 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 );
}
}
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );
}
Expand All @@ -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 );
}
Expand All @@ -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 );
}
}
}