From 833343f17ccac95cf195dfa56a3cd52c3cc31654 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 9 Jul 2018 14:31:45 +0200 Subject: [PATCH] Specifically rethrow IllegalAccessError in isPresent/resolveClassName Issue: SPR-17018 --- .../org/springframework/util/ClassUtils.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 8f4bd30f03..d489c522c3 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -301,6 +301,10 @@ public abstract class ClassUtils { * @return a class instance for the supplied name * @throws IllegalArgumentException if the class name was not resolvable * (that is, the class could not be found or the class file could not be loaded) + * @throws IllegalStateException if the corresponding class is resolvable but + * there was a readability mismatch in the inheritance hierarchy of the class + * (typically a missing dependency declaration in a Jigsaw module definition + * for a superclass or interface implemented by the class to be loaded here) * @see #forName(String, ClassLoader) */ public static Class resolveClassName(String className, @Nullable ClassLoader classLoader) @@ -309,12 +313,16 @@ public abstract class ClassUtils { try { return forName(className, classLoader); } - catch (ClassNotFoundException ex) { - throw new IllegalArgumentException("Could not find class [" + className + "]", ex); + catch (IllegalAccessError err) { + throw new IllegalStateException("Readability mismatch in inheritance hierarchy of class [" + + className + "]: " + err.getMessage(), err); } catch (LinkageError err) { throw new IllegalArgumentException("Unresolvable class definition for class [" + className + "]", err); } + catch (ClassNotFoundException ex) { + throw new IllegalArgumentException("Could not find class [" + className + "]", ex); + } } /** @@ -324,15 +332,24 @@ public abstract class ClassUtils { * @param className the name of the class to check * @param classLoader the class loader to use * (may be {@code null} which indicates the default class loader) - * @return whether the specified class is present + * @return whether the specified class is present (including all of its + * superclasses and interfaces) + * @throws IllegalStateException if the corresponding class is resolvable but + * there was a readability mismatch in the inheritance hierarchy of the class + * (typically a missing dependency declaration in a Jigsaw module definition + * for a superclass or interface implemented by the class to be checked here) */ public static boolean isPresent(String className, @Nullable ClassLoader classLoader) { try { forName(className, classLoader); return true; } + catch (IllegalAccessError err) { + throw new IllegalStateException("Readability mismatch in inheritance hierarchy of class [" + + className + "]: " + err.getMessage(), err); + } catch (Throwable ex) { - // Class or one of its dependencies is not present... + // Typically ClassNotFoundException or NoClassDefFoundError... return false; } } -- GitLab