Skip to content

Commit ff94776

Browse files
committed
clean up some error throwing code
1 parent ee10173 commit ff94776

File tree

4 files changed

+24
-38
lines changed

4 files changed

+24
-38
lines changed

hibernate-core/src/main/java/org/hibernate/boot/registry/classloading/internal/AggregatedClassLoader.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.Iterator;
1111
import java.util.LinkedHashSet;
1212

13-
import org.hibernate.internal.util.ExceptionHelper;
1413

1514
public class AggregatedClassLoader extends ClassLoader {
1615
private final ClassLoader[] individualClassLoaders;
@@ -122,7 +121,7 @@ else if ( !sysCLReturned && systemClassLoader != null ) {
122121

123122
private Iterator<ClassLoader> newTcclNeverIterator() {
124123
final ClassLoader systemClassLoader = locateSystemClassLoader();
125-
return new Iterator<ClassLoader>() {
124+
return new Iterator<>() {
126125
private int currentIndex = 0;
127126
private boolean sysCLReturned = false;
128127

@@ -182,7 +181,7 @@ public URL nextElement() {
182181

183182
@Override
184183
protected URL findResource(String name) {
185-
final Iterator<ClassLoader> clIterator = newClassLoaderIterator();
184+
final var clIterator = newClassLoaderIterator();
186185
while ( clIterator.hasNext() ) {
187186
final ClassLoader classLoader = clIterator.next();
188187
final URL resource = classLoader.getResource( name );
@@ -195,22 +194,18 @@ protected URL findResource(String name) {
195194

196195
@Override
197196
protected Class<?> findClass(String name) throws ClassNotFoundException {
198-
final Iterator<ClassLoader> clIterator = newClassLoaderIterator();
199-
Throwable t = null;
197+
final var clIterator = newClassLoaderIterator();
198+
final var exception = new ClassNotFoundException( "Could not load requested class: " + name );
200199
while ( clIterator.hasNext() ) {
201200
final ClassLoader classLoader = clIterator.next();
202201
try {
203202
return classLoader.loadClass( name );
204203
}
205-
catch (Exception ex) {
206-
ExceptionHelper.combine( ( t == null ? t = new Throwable() : t ), ex );
207-
}
208-
catch (LinkageError le) {
209-
ExceptionHelper.combine( ( t == null ? t = new Throwable() : t ), le );
204+
catch (Exception|LinkageError ex) {
205+
exception.addSuppressed( ex );
210206
}
211207
}
212-
213-
throw new ClassNotFoundException( "Could not load requested class : " + name, ( t != null && t.getSuppressed().length > 0 ? t : null ) );
208+
throw exception;
214209
}
215210

216211
private static ClassLoader locateSystemClassLoader() {

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ private void firePersist(final PersistEvent event) {
721721
}
722722
}
723723
if ( originalException != null ) {
724-
ExceptionHelper.doThrow( originalException );
724+
ExceptionHelper.rethrow( originalException );
725725
}
726726
}
727727

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,28 @@ private ExceptionHelper() {
1111
}
1212

1313
/**
14-
* Throws the given throwable even if it is a checked exception.
14+
* Throws the given {@link Throwable}, even if it's a checked exception.
1515
*
16-
* @param e The throwable to throw.
16+
* @param throwable The {@code Throwable} to throw.
1717
*/
18-
public static void doThrow(Throwable e) {
19-
ExceptionHelper.doThrow0(e);
18+
public static void rethrow(Throwable throwable) {
19+
sneakyThrow( throwable );
2020
}
2121

22-
public static Throwable getRootCause(Throwable error) {
23-
Throwable toProcess = error;
24-
while ( toProcess.getCause() != null ) {
25-
toProcess = toProcess.getCause();
26-
}
27-
return toProcess;
22+
@SuppressWarnings("unchecked")
23+
private static <T extends Throwable> void sneakyThrow(Throwable e)
24+
throws T {
25+
throw (T) e;
2826
}
2927

30-
public static <T extends Throwable> T combine(T throwable, T otherThrowable) {
31-
T toThrow = throwable;
32-
if ( otherThrowable != null ) {
33-
if ( toThrow != null ) {
34-
toThrow.addSuppressed( otherThrowable );
35-
}
36-
else {
37-
toThrow = otherThrowable;
28+
public static Throwable getRootCause(Throwable error) {
29+
var next = error;
30+
while ( true ) {
31+
final var current = next;
32+
next = current.getCause();
33+
if ( next == null ) {
34+
return current;
3835
}
3936
}
40-
return toThrow;
41-
}
42-
43-
@SuppressWarnings("unchecked")
44-
private static <T extends Throwable> void doThrow0(Throwable e) throws T {
45-
throw (T) e;
4637
}
4738
}

hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void release() {
122122
}
123123
}
124124
if ( originalException != null ) {
125-
ExceptionHelper.doThrow( originalException );
125+
ExceptionHelper.rethrow( originalException );
126126
}
127127
}
128128
}

0 commit comments

Comments
 (0)