While testing spring-core
within a GraalVM native image (which uses a single, system-wide ClassLoader
), it became apparent that the visibility check within SynthesizedMergedAnnotationInvocationHandler.isVisible(ClassLoader, Class<?>)
could be short circuited to avoid an unnecessary invocation of Class.forName
.
@jhoeller suggested something along the lines of the following.
private static boolean isVisible(ClassLoader classLoader, Class<?> interfaceClass) {
// short circuit?
if (interfaceClass.getClassLoader() == classLoader) {
return true;
}
try {
return Class.forName(interfaceClass.getName(), false, classLoader) == interfaceClass;
}
catch (ClassNotFoundException ex) {
return false;
}
}