private static Class getClassByNameOrNull()

in src/main/java/org/apache/commons/crypto/utils/ReflectionUtils.java [86:126]


    private static Class<?> getClassByNameOrNull(final String name) {
        final Set<String> set = INIT_ERROR_CLASSES.computeIfAbsent(CLASS_LOADER, k -> Collections.synchronizedSet(new HashSet<>()));

        if (set.contains(name)) {
            return null;
        }

        final Map<String, WeakReference<Class<?>>> map;

        synchronized (CACHE_CLASSES) {
            map = CACHE_CLASSES.computeIfAbsent(CLASS_LOADER, k -> Collections.synchronizedMap(new WeakHashMap<>()));
        }

        Class<?> clazz = null;
        final WeakReference<Class<?>> ref = map.get(name);
        if (ref != null) {
            clazz = ref.get();
        }

        if (clazz == null) {
            try {
                clazz = Class.forName(name, true, CLASS_LOADER);
            } catch (final ClassNotFoundException e) {
                // Leave a marker that the class isn't found
                map.put(name, new WeakReference<>(NEGATIVE_CACHE_SENTINEL));
                return null;
            } catch (final ExceptionInInitializerError error) {
                // Leave a marker that the class initialization failed
                set.add(name);
                return null;
            }
            // two putters can race here, but they'll put the same class
            map.put(name, new WeakReference<>(clazz));
            return clazz;
        }
        if (clazz == NEGATIVE_CACHE_SENTINEL) {
            return null; // not found
        }
        // cache hit
        return clazz;
    }