Skip to content

Commit 833343f

Browse files
committed
Specifically rethrow IllegalAccessError in isPresent/resolveClassName
Issue: SPR-17018
1 parent fd0220b commit 833343f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ public static Class<?> forName(String name, @Nullable ClassLoader classLoader)
301301
* @return a class instance for the supplied name
302302
* @throws IllegalArgumentException if the class name was not resolvable
303303
* (that is, the class could not be found or the class file could not be loaded)
304+
* @throws IllegalStateException if the corresponding class is resolvable but
305+
* there was a readability mismatch in the inheritance hierarchy of the class
306+
* (typically a missing dependency declaration in a Jigsaw module definition
307+
* for a superclass or interface implemented by the class to be loaded here)
304308
* @see #forName(String, ClassLoader)
305309
*/
306310
public static Class<?> resolveClassName(String className, @Nullable ClassLoader classLoader)
@@ -309,12 +313,16 @@ public static Class<?> resolveClassName(String className, @Nullable ClassLoader
309313
try {
310314
return forName(className, classLoader);
311315
}
312-
catch (ClassNotFoundException ex) {
313-
throw new IllegalArgumentException("Could not find class [" + className + "]", ex);
316+
catch (IllegalAccessError err) {
317+
throw new IllegalStateException("Readability mismatch in inheritance hierarchy of class [" +
318+
className + "]: " + err.getMessage(), err);
314319
}
315320
catch (LinkageError err) {
316321
throw new IllegalArgumentException("Unresolvable class definition for class [" + className + "]", err);
317322
}
323+
catch (ClassNotFoundException ex) {
324+
throw new IllegalArgumentException("Could not find class [" + className + "]", ex);
325+
}
318326
}
319327

320328
/**
@@ -324,15 +332,24 @@ public static Class<?> resolveClassName(String className, @Nullable ClassLoader
324332
* @param className the name of the class to check
325333
* @param classLoader the class loader to use
326334
* (may be {@code null} which indicates the default class loader)
327-
* @return whether the specified class is present
335+
* @return whether the specified class is present (including all of its
336+
* superclasses and interfaces)
337+
* @throws IllegalStateException if the corresponding class is resolvable but
338+
* there was a readability mismatch in the inheritance hierarchy of the class
339+
* (typically a missing dependency declaration in a Jigsaw module definition
340+
* for a superclass or interface implemented by the class to be checked here)
328341
*/
329342
public static boolean isPresent(String className, @Nullable ClassLoader classLoader) {
330343
try {
331344
forName(className, classLoader);
332345
return true;
333346
}
347+
catch (IllegalAccessError err) {
348+
throw new IllegalStateException("Readability mismatch in inheritance hierarchy of class [" +
349+
className + "]: " + err.getMessage(), err);
350+
}
334351
catch (Throwable ex) {
335-
// Class or one of its dependencies is not present...
352+
// Typically ClassNotFoundException or NoClassDefFoundError...
336353
return false;
337354
}
338355
}

0 commit comments

Comments
 (0)