@@ -301,6 +301,10 @@ public static Class<?> forName(String name, @Nullable ClassLoader classLoader)
301
301
* @return a class instance for the supplied name
302
302
* @throws IllegalArgumentException if the class name was not resolvable
303
303
* (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)
304
308
* @see #forName(String, ClassLoader)
305
309
*/
306
310
public static Class <?> resolveClassName (String className , @ Nullable ClassLoader classLoader )
@@ -309,12 +313,16 @@ public static Class<?> resolveClassName(String className, @Nullable ClassLoader
309
313
try {
310
314
return forName (className , classLoader );
311
315
}
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 );
314
319
}
315
320
catch (LinkageError err ) {
316
321
throw new IllegalArgumentException ("Unresolvable class definition for class [" + className + "]" , err );
317
322
}
323
+ catch (ClassNotFoundException ex ) {
324
+ throw new IllegalArgumentException ("Could not find class [" + className + "]" , ex );
325
+ }
318
326
}
319
327
320
328
/**
@@ -324,15 +332,24 @@ public static Class<?> resolveClassName(String className, @Nullable ClassLoader
324
332
* @param className the name of the class to check
325
333
* @param classLoader the class loader to use
326
334
* (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)
328
341
*/
329
342
public static boolean isPresent (String className , @ Nullable ClassLoader classLoader ) {
330
343
try {
331
344
forName (className , classLoader );
332
345
return true ;
333
346
}
347
+ catch (IllegalAccessError err ) {
348
+ throw new IllegalStateException ("Readability mismatch in inheritance hierarchy of class [" +
349
+ className + "]: " + err .getMessage (), err );
350
+ }
334
351
catch (Throwable ex ) {
335
- // Class or one of its dependencies is not present ...
352
+ // Typically ClassNotFoundException or NoClassDefFoundError ...
336
353
return false ;
337
354
}
338
355
}
0 commit comments