Package emissary.util
Class ClassLookupCache
- java.lang.Object
-
- emissary.util.ClassLookupCache
-
public final class ClassLookupCache extends Object
This implements a simple caching mechanism forClass.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 useClass.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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>
get(String className)
Look up a class in the cache.static Class<?>
lookup(String className)
Look up a class by name.static void
put(String className, Class<?> clazz)
Store a class lookup in the cache.static void
unload()
Destroy the ThreadLocal cache object
-
-
-
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. Otherwisenull
.
-
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 matchclassName
.
-
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 ofClass.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 toclassName
. - Throws:
ClassNotFoundException
- If the class name could not be resolved.
-
unload
public static void unload()
Destroy the ThreadLocal cache object
-
-