Package com.vaadin.flow.internal
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 keysT- the cached value type
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 Summary
ConstructorsConstructorDescriptionReflectionCache(SerializableFunction<Class<C>, T> valueProvider) Creates a new reflection cache with the given value provider. -
Method Summary
Modifier and TypeMethodDescriptionstatic RegistrationaddClearAllAction(Runnable action) Adds an action that will be run when all reflection caches are cleared.static RegistrationaddClearAllAction(Consumer<Class<?>> action) Adds an action that will be run when all reflection caches are cleared for a specific type.voidclear()Removes all mappings from this cache.voidRemoves mappings for the given type.static voidclearAll()Clears all mappings from all reflection caches and related resources.static voidClears mappings for give type from all reflection caches and related resources.static voidclearBeforeHotswap(Class<?> type) Clears mappings for the given type from the reflection caches created withclearedBeforeHotswap(SerializableFunction).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 byclearAll(Class)like any other reflection cache.booleanChecks whether this cache contains an entry for the given type.Gets a cached value.
-
Constructor Details
-
ReflectionCache
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 noCurrentInstanceis 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, notnull
-
-
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 byclearAll(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 keysT- the cached value type- Parameters:
valueProvider- a function that computes the cached value for a class, notnull- Returns:
- a new reflection cache, not
null - See Also:
-
get
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
Checks whether this cache contains an entry for the given type.- Parameters:
type- the type to check for- Returns:
trueif there is a mapping for the type,falseif there is no mapping
-
clear
public void clear()Removes all mappings from this cache. -
clear
Removes mappings for the given type.- Parameters:
type- the type to remove; null means all mappings.- Since:
- 25.0
-
addClearAllAction
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
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
Clears mappings for give type from all reflection caches and related resources.- Since:
- 25.0
-
clearBeforeHotswap
Clears mappings for the given type from the reflection caches created withclearedBeforeHotswap(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.
-