Class ReflectionCache<C,​T>

  • Type Parameters:
    C - the class types that are used as the cache keys
    T - the cached value type

    public class ReflectionCache<C,​T>
    extends Object
    A thread-safe cache for the result of doing some reflection lookup based on a class. Cached values never expire since it's assumed that the there is a finite number of classes for which reflection results are used.

    For internal use only. May be renamed or removed in a future release.

    Since:
    1.0
    Author:
    Vaadin Ltd
    • Constructor Detail

      • ReflectionCache

        public ReflectionCache​(SerializableFunction<Class<C>,​T> valueProvider)
        Creates a new reflection cache with the given value provider. The value provider will be used to produce a new cached value whenever there is a cache miss. It will be run in a context where no CurrentInstance is available to prevent accidentally caching values that are computed differently depending on external circumstances.
        Parameters:
        valueProvider - a function that computes the cached value for a class, not null
    • Method Detail

      • get

        public T get​(Class<? extends C> type)
        Gets a cached value. If this cache does not contain a value for the key, the value is computed using the configured value provider and the cache is populated with the new value.
        Parameters:
        type - the type for which to get reflection results
        Returns:
        the reflection results
      • contains

        public boolean contains​(Class<? extends C> type)
        Checks whether this cache contains an entry for the given type.
        Parameters:
        type - the type to check for
        Returns:
        true if there is a mapping for the type, false if there is no mapping
      • clear

        public void clear()
        Removes all mappings from this cache.
      • addClearAllAction

        public static Registration addClearAllAction​(Runnable action)
        Adds an action that will be run when all reflection caches are cleared.

        The actions are held with a weak reference, which typically means that the action will be ignored if the returned registration is garbage collected.

        Parameters:
        action - the action to run
        Returns:
        a registration for removing the action
        See Also:
        clearAll()
      • clearAll

        public static void clearAll()
        Clears all mappings from all reflection caches and related resources.