Package net.bytebuddy

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

public class TypeCache<T> extends ReferenceQueue<ClassLoader>

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:
  • Field Details

  • Constructor Details

    • TypeCache

      public TypeCache()
      Creates a new type cache with strong references to the stored types.
    • TypeCache

      public TypeCache(TypeCache.Sort sort)
      Creates a new type cache.
      Parameters:
      sort - The reference type to use for stored types.
  • Method Details

    • find

      @MaybeNull public Class<?> find(@MaybeNull ClassLoader classLoader, T key)
      Finds a stored type or returns null if no type was stored.
      Parameters:
      classLoader - The class loader for which this type is stored or null for the bootstrap loader.
      key - The key for the type in question.
      Returns:
      The stored type or null if no type was stored.
    • insert

      public Class<?> insert(@MaybeNull ClassLoader classLoader, T key, Class<?> type)
      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 or null 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

      public Class<?> findOrInsert(@MaybeNull ClassLoader classLoader, T key, Callable<Class<?>> lazy)
      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 or null 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 or null 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.