public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
        Preconditions.checkNotNull(clazz, "Class must not be null");
        try {
            ClassLoader target = clazz.getClassLoader();
            if (target == null) {
                return true;
            }
            ClassLoader cur = classLoader;
            if (cur == target) {
                return true;
            }
            while (cur != null) {
                cur = cur.getParent();
                if (cur == target) {
                    return true;
                }
            }
            return false;
        } catch (SecurityException ex) {
            // Probably from the system ClassLoader - let's consider it safe.
            return true;
        }
    }
public static boolean isCacheSafe ( Class < ? > clazz , ClassLoader classLoader ) { Preconditions . checkNotNull ( clazz , "Class must not be null" ) ; try { ClassLoader target = clazz . getClassLoader ( ) ; if ( target == null ) { return true ; } ClassLoader cur = classLoader ; if ( cur == target ) { return true ; } while ( cur != null ) { cur = cur . getParent ( ) ; if ( cur == target ) { return true ; } } return false ; } catch ( SecurityException ex ) { return true ; } }
Check whether the given class is cache-safe in the given context,
i.e. whether it is loaded by the given ClassLoader or a parent of it.

@param clazz       the class to analyze
@param classLoader the ClassLoader to potentially cache metadata in