Class ReflectionCache<C,T>

java.lang.Object
com.vaadin.flow.internal.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 Details

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

    • clearedBeforeHotswap

      public static <C, T> ReflectionCache<C,T> clearedBeforeHotswap(SerializableFunction<Class<C>,T> valueProvider)
      Creates a new reflection cache whose values for a class are cleared before hotswappers are run, in addition to being cleared by clearAll(Class) like any other reflection cache.

      Regular reflection caches are cleared only after all hotswappers have run, so that hotswappers can still compare previous values with the changed class. Use this for caches whose values must already reflect the changed class while hotswappers run, and whose previous values are not needed by any hotswapper.

      Type Parameters:
      C - the class types that are used as the cache keys
      T - the cached value type
      Parameters:
      valueProvider - a function that computes the cached value for a class, not null
      Returns:
      a new reflection cache, not null
      See Also:
    • 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.
    • clear

      public void clear(Class<? extends C> type)
      Removes mappings for the given type.
      Parameters:
      type - the type to remove; null means all mappings.
      Since:
      25.0
    • 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
      Since:
      1.1
      See Also:
    • addClearAllAction

      public static Registration addClearAllAction(Consumer<Class<?>> action)
      Adds an action that will be run when all reflection caches are cleared for a specific type.

      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
      Since:
      25.0
      See Also:
    • clearAll

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

      public static void clearAll(Class<?> type)
      Clears mappings for give type from all reflection caches and related resources.
      Since:
      25.0
    • clearBeforeHotswap

      public static void clearBeforeHotswap(Class<?> type)
      Clears mappings for the given type from the reflection caches created with clearedBeforeHotswap(SerializableFunction). Other reflection caches are not affected.

      Called for changed classes before hotswappers are run.

      Parameters:
      type - the type to remove; null means all mappings.