From 26a173ff8e333863f9c7ec77c19775c9a3d973ca Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Thu, 16 Mar 2023 22:08:49 +0100 Subject: [PATCH] HHH-15108 - tweak to not ignore exceptions in AggregateClassLoader.findClass() Signed-off-by: Jan Schatteman --- .../internal/AggregatedClassLoader.java | 12 +++++++++--- .../hibernate/internal/util/ExceptionHelper.java | 13 +++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/AggregatedClassLoader.java b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/AggregatedClassLoader.java index 1ecf93ca45..5e214414b1 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/AggregatedClassLoader.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/AggregatedClassLoader.java @@ -11,6 +11,9 @@ import java.net.URL; import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.Optional; + +import org.hibernate.internal.util.ExceptionHelper; public class AggregatedClassLoader extends ClassLoader { private final ClassLoader[] individualClassLoaders; @@ -196,18 +199,21 @@ public class AggregatedClassLoader extends ClassLoader { @Override protected Class findClass(String name) throws ClassNotFoundException { final Iterator clIterator = newClassLoaderIterator(); + Throwable t = null; while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); try { return classLoader.loadClass( name ); } - catch (Exception ignore) { + catch (Exception ex) { + ExceptionHelper.combine( ( t == null ? t = new Throwable() : t ), ex ); } - catch (LinkageError ignore) { + catch (LinkageError le) { + ExceptionHelper.combine( ( t == null ? t = new Throwable() : t ), le ); } } - throw new ClassNotFoundException( "Could not load requested class : " + name ); + throw new ClassNotFoundException( "Could not load requested class : " + name, ( t != null && t.getSuppressed().length > 0 ? t : null ) ); } private static ClassLoader locateSystemClassLoader() { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ExceptionHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ExceptionHelper.java index cae8c37240..d3ae83d02e 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/ExceptionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ExceptionHelper.java @@ -29,6 +29,19 @@ public final class ExceptionHelper { return toProcess; } + public static T combine(T throwable, T otherThrowable) { + T toThrow = throwable; + if ( otherThrowable != null ) { + if ( toThrow != null ) { + toThrow.addSuppressed( otherThrowable ); + } + else { + toThrow = otherThrowable; + } + } + return toThrow; + } + @SuppressWarnings("unchecked") private static void doThrow0(Throwable e) throws T { throw (T) e;