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 java.lang.ref.ReferenceQueue<java.lang.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:
    TypeCache.WithInlineExpunction, TypeCache.SimpleKey
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  TypeCache.LookupKey
      A key used for looking up a previously inserted class loader cache.
      static class  TypeCache.SimpleKey
      A simple key based on a collection of types where no type is strongly referenced.
      static class  TypeCache.Sort
      Determines the storage format for a cached type.
      protected static class  TypeCache.StorageKey
      A key used for storing a class loader cache reference.
      static class  TypeCache.WithInlineExpunction<S>
      An implementation of a TypeCache where obsolete references are cleared upon any call.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.ConcurrentMap<TypeCache.StorageKey,​java.util.concurrent.ConcurrentMap<T,​java.lang.ref.Reference<java.lang.Class<?>>>> cache
      The underlying map containing cached objects.
      private static java.lang.Class<?> NOT_FOUND
      Indicates that a type was not found.
      protected TypeCache.Sort sort
      The reference type to use for stored types.
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeCache​(TypeCache.Sort sort)
      Creates a new type cache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the entire cache.
      void expungeStaleEntries()
      Removes any stale class loader entries from the cache.
      java.lang.Class<?> find​(java.lang.ClassLoader classLoader, T key)
      Finds a stored type or returns null if no type was stored.
      java.lang.Class<?> findOrInsert​(java.lang.ClassLoader classLoader, T key, java.util.concurrent.Callable<java.lang.Class<?>> lazy)
      Finds an existing type or inserts a new one if the previous type was not found.
      java.lang.Class<?> findOrInsert​(java.lang.ClassLoader classLoader, T key, java.util.concurrent.Callable<java.lang.Class<?>> lazy, java.lang.Object monitor)
      Finds an existing type or inserts a new one if the previous type was not found.
      java.lang.Class<?> insert​(java.lang.ClassLoader classLoader, T key, java.lang.Class<?> type)
      Inserts a new type into the cache.
      • Methods inherited from class java.lang.ref.ReferenceQueue

        poll, remove, remove
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NOT_FOUND

        private static final java.lang.Class<?> NOT_FOUND
        Indicates that a type was not found.
      • sort

        protected final TypeCache.Sort sort
        The reference type to use for stored types.
      • cache

        protected final java.util.concurrent.ConcurrentMap<TypeCache.StorageKey,​java.util.concurrent.ConcurrentMap<T,​java.lang.ref.Reference<java.lang.Class<?>>>> cache
        The underlying map containing cached objects.
    • Constructor Detail

      • TypeCache

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

      • find

        public java.lang.Class<?> find​(java.lang.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.
        key - The key for the type in question.
        Returns:
        The stored type or null if no type was stored.
      • insert

        public java.lang.Class<?> insert​(java.lang.ClassLoader classLoader,
                                         T key,
                                         java.lang.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.
        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 java.lang.Class<?> findOrInsert​(java.lang.ClassLoader classLoader,
                                               T key,
                                               java.util.concurrent.Callable<java.lang.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.
        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 java.lang.Class<?> findOrInsert​(java.lang.ClassLoader classLoader,
                                               T key,
                                               java.util.concurrent.Callable<java.lang.Class<?>> lazy,
                                               java.lang.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.
        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.