Class TypeCache<T>
- Type Parameters:
T
- The type of the key that is used for identifying stored classes per class loader. Such keys must not strongly reference any types or class loaders without potentially corrupting the garbage eligibility of stored classes. As the storage is segmented by class loader, it is normally sufficient to store types by their name.
- Direct Known Subclasses:
TypeCache.WithInlineExpunction
A cache for storing types without strongly referencing any class loader or type.
Note: In order to clean obsolete class loader references from the map, expungeStaleEntries()
must be called
regularly. This can happen in a different thread, in custom intervals or on every use of the cache by creating an instance of
TypeCache.WithInlineExpunction
. This cache is fully thread-safe.
Important: The behavior of a type cache might not be as expected. A class is only eligible for garbage collection once its class loader is eligible for garbage collection. At the same time, a garbage collector is only eligible for garbage collection once all of its classes are eligible for garbage collection. If this cache referenced the cached type strongly, this would never be the case which is why this cache maintains either strong or weak references. In the latter case, a type is typically retained until the last instance of the type is not eligible for garbage collection. With soft references, the type is typically retained until the next full garbage collection where all instances of the type are eligible for garbage collection.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
A key used for looking up a previously inserted class loader cache.static class
A simple key based on a collection of types where no type is strongly referenced.static enum
Determines the storage format for a cached type.protected static class
A key used for storing a class loader cache reference.static class
An implementation of aTypeCache
where obsolete references are cleared upon any call. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConcurrentMap
<TypeCache.StorageKey, ConcurrentMap<T, Object>> The underlying map containing cached objects.private static final Class
<?> Indicates that a type was not found.protected final TypeCache.Sort
The reference type to use for stored types. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new type cache with strong references to the stored types.TypeCache
(TypeCache.Sort sort) Creates a new type cache. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the entire cache.void
Removes any stale class loader entries from the cache.Class
<?> find
(ClassLoader classLoader, T key) Finds a stored type or returnsnull
if no type was stored.Class
<?> findOrInsert
(ClassLoader classLoader, T key, Callable<Class<?>> lazy) Finds an existing type or inserts a new one if the previous type was not found.Class
<?> findOrInsert
(ClassLoader classLoader, T key, Callable<Class<?>> lazy, Object monitor) Finds an existing type or inserts a new one if the previous type was not found.Class
<?> insert
(ClassLoader classLoader, T key, Class<?> type) Inserts a new type into the cache.Methods inherited from class java.lang.ref.ReferenceQueue
poll, remove, remove
-
Field Details
-
NOT_FOUND
Indicates that a type was not found. -
sort
The reference type to use for stored types. -
cache
The underlying map containing cached objects.
-
-
Constructor Details
-
TypeCache
public TypeCache()Creates a new type cache with strong references to the stored types. -
TypeCache
Creates a new type cache.- Parameters:
sort
- The reference type to use for stored types.
-
-
Method Details
-
find
Finds a stored type or returnsnull
if no type was stored.- Parameters:
classLoader
- The class loader for which this type is stored ornull
for the bootstrap loader.key
- The key for the type in question.- Returns:
- The stored type or
null
if no type was stored.
-
insert
Inserts a new type into the cache. If a type with the same class loader and key was inserted previously, the cache is not updated.- Parameters:
classLoader
- The class loader for which this type is stored ornull
for the bootstrap loader.key
- The key for the type in question.type
- The type to insert of no previous type was stored in the cache.- Returns:
- The supplied type or a previously submitted type for the same class loader and key combination.
-
findOrInsert
Finds an existing type or inserts a new one if the previous type was not found.- Parameters:
classLoader
- The class loader for which this type is stored ornull
for the bootstrap loader.key
- The key for the type in question.lazy
- A lazy creator for the type to insert of no previous type was stored in the cache.- Returns:
- The lazily created type or a previously submitted type for the same class loader and key combination.
-
findOrInsert
public Class<?> findOrInsert(@MaybeNull ClassLoader classLoader, T key, Callable<Class<?>> lazy, Object monitor) Finds an existing type or inserts a new one if the previous type was not found.- Parameters:
classLoader
- The class loader for which this type is stored ornull
for the bootstrap loader.key
- The key for the type in question.lazy
- A lazy creator for the type to insert of no previous type was stored in the cache.monitor
- A monitor to lock before creating the lazy type.- Returns:
- The lazily created type or a previously submitted type for the same class loader and key combination.
-
expungeStaleEntries
public void expungeStaleEntries()Removes any stale class loader entries from the cache. -
clear
public void clear()Clears the entire cache.
-