Package emissary.util

Class ClassLookupCache


  • public final class ClassLookupCache
    extends Object
    This implements a simple caching mechanism for Class.forName(String). For example if the same class name is looked up repeatedly, using this cache may be able to avoid a lot of JVM reflection overhead.

    To use this, just call lookup(String) where you would normally use Class.forName(String). There are also methods for directly manipulating the cache but most uses can avoid those.

    Note that the cache implementation may have a small capacity and/or be thread-specific, so storing something in the cache does not guarantee that it will be indefinitely cached or that the cached value will be visible to other threads.

    • Method Detail

      • get

        @Nullable
        public static Class<?> get​(String className)
        Look up a class in the cache.
        Parameters:
        className - The class name to find.
        Returns:
        If the class name is currently known to the cache, the corresponding Class object is returned. Otherwise null.
      • put

        public static void put​(String className,
                               Class<?> clazz)
        Store a class lookup in the cache.
        Parameters:
        className - The class name.
        clazz - The class. Assumed to match className.
      • lookup

        public static Class<?> lookup​(String className)
                               throws ClassNotFoundException
        Look up a class by name. This is basically a utility method that can be called instead of Class.forName(String), and will try to use the cache to speed up the lookups.
        Parameters:
        className - The class name to get.
        Returns:
        The Class object corresponding to className.
        Throws:
        ClassNotFoundException - If the class name could not be resolved.
      • unload

        public static void unload()
        Destroy the ThreadLocal cache object