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, TypeCache#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
WithInlineExpunction. This cache is fully thread-safe.
Important Note
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.
WithInlineExpunction, SimpleKey
| Modifier and Type | Class and Description |
|---|---|
| protected static class | TypeCache.LookupKey
A key used for looking up a previously inserted class loader cache. |
| public static class | TypeCache.SimpleKey
A simple key based on a collection of types where no type is strongly referenced. |
| public static enum | 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. |
| public static class | TypeCache.WithInlineExpunction<
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. S>An implementation of a |
| Modifier and Type | Field and Description |
|---|---|
| protected final ConcurrentMap | cache
The underlying map containing cached objects. |
| private static final Class | NOT_FOUND
Indicates that a type was not found. |
| protected final TypeCache. | sort
The reference type to use for stored types. |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public Class | Returns: The stored type ornull if no type was stored.The class loader for which this type is stored or classLoader, T null for the bootstrap loader.The key for the type in question. key)Finds a stored type or returns |
| public Class | Returns: The lazily created type or a previously submitted type for the same class loader and key combination.The class loader for which this type is stored or classLoader, T null for the bootstrap loader.The key for the type in question. key, Callable<Class<?>> A lazy creator for the type to insert of no previous type was stored in the cache. lazy)Finds an existing type or inserts a new one if the previous type was not found. |
| public Class | Returns: The lazily created type or a previously submitted type for the same class loader and key combination.The class loader for which this type is stored or classLoader, T null for the bootstrap loader.The key for the type in question. key, Callable<Class<?>> A lazy creator for the type to insert of no previous type was stored in the cache. lazy, Object A monitor to lock before creating the lazy type. monitor)Finds an existing type or inserts a new one if the previous type was not found. |
| public Class | Returns: The supplied type or a previously submitted type for the same class loader and key combination.The class loader for which this type is stored or classLoader, T null for the bootstrap loader.The key for the type in question. key, Class<?> The type to insert of no previous type was stored in the cache. type)Inserts a new type into the cache. |
| cache | back to summary |
|---|---|
| protected final ConcurrentMap<TypeCache. The underlying map containing cached objects. | |
| NOT_FOUND | back to summary |
|---|---|
| private static final Class<?> NOT_FOUND Indicates that a type was not found. | |
| sort | back to summary |
|---|---|
| protected final TypeCache. The reference type to use for stored types. | |
| TypeCache | back to summary |
|---|---|
| public TypeCache() Creates a new type cache with strong references to the stored types. | |
| TypeCache | back to summary |
|---|---|
| public TypeCache(TypeCache. Creates a new type cache. | |
| clear | back to summary |
|---|---|
| public void clear() Clears the entire cache. | |
| expungeStaleEntries | back to summary |
|---|---|
| public void expungeStaleEntries() Removes any stale class loader entries from the cache. | |
| find | back to summary |
|---|---|
| public Class Finds a stored type or returns
| |
| findOrInsert | back to summary |
|---|---|
| public Class Finds an existing type or inserts a new one if the previous type was not found.
| |
| findOrInsert | back to summary |
|---|---|
| public Class Finds an existing type or inserts a new one if the previous type was not found.
| |
| insert | back to summary |
|---|---|
| public Class 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.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final ClassLoader | classLoader
The referenced class loader or |
| private final int | hashCode
The class loader's identity hash code. |
| Access | Constructor and Description |
|---|---|
| protected | LookupKey(ClassLoader
The represented class loader or classLoader)null for the bootstrap loader.Creates a new lookup key. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. other)Overrides java. |
| public int |
| classLoader | back to summary |
|---|---|
| private final ClassLoader classLoader The referenced class loader or | |
| hashCode | back to summary |
|---|---|
| private final int hashCode The class loader's identity hash code. | |
| LookupKey | back to summary |
|---|---|
| protected LookupKey(ClassLoader classLoader) Creates a new lookup key.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| Modifier and Type | Field and Description |
|---|---|
| private final Set | types
The referenced types. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | SimpleKey(Class<?>
The first type to be represented by this key. type, Collection<? extends Class<?>> Any additional types to be represented by this key. additionalTypes)Creates a simple cache key.. |
| public | SimpleKey(Collection<? extends Class<?>>
Any types to be represented by this key. types)Creates a simple cache key.. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. other)Overrides java. |
| public int |
| types | back to summary |
|---|---|
| private final Set<String> types The referenced types. | |
| SimpleKey | back to summary |
|---|---|
| public SimpleKey(Class<?> type, Class<?>... additionalType) Creates a simple cache key.. | |
| SimpleKey | back to summary |
|---|---|
| public SimpleKey(Class<?> type, Collection<? extends Class<?>> additionalTypes) Creates a simple cache key..
| |
| SimpleKey | back to summary |
|---|---|
| public SimpleKey(Collection<? extends Class<?>> types) Creates a simple cache key..
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final TypeCache. | SOFT
Creates a cache where cached types are wrapped by |
| public static final TypeCache. | STRONG
Creates a cache where cached types are strongly referenced. |
| public static final TypeCache. | WEAK
Creates a cache where cached types are wrapped by |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static TypeCache. | |
| public static TypeCache. | |
| protected abstract Object |
| SOFT | back to summary |
|---|---|
| public static final TypeCache. Creates a cache where cached types are wrapped by | |
| STRONG | back to summary |
|---|---|
| public static final TypeCache. Creates a cache where cached types are strongly referenced. | |
| WEAK | back to summary |
|---|---|
| public static final TypeCache. Creates a cache where cached types are wrapped by | |
| Sort | back to summary |
|---|---|
| private Sort() | |
| valueOf | back to summary |
|---|---|
| public static TypeCache. | |
| values | back to summary |
|---|---|
| public static TypeCache. | |
| wrap | back to summary |
|---|---|
| protected abstract Object wrap(Class<?> type) Wraps a type as a | |
| Modifier and Type | Field and Description |
|---|---|
| private final int | hashCode
The class loader's identity hash code. |
| Access | Constructor and Description |
|---|---|
| protected | StorageKey(ClassLoader
The represented class loader or classLoader, ReferenceQueue<? super ClassLoader> null for the bootstrap loader.The reference queue to notify upon a garbage collection. referenceQueue)Creates a new storage key. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. other)Overrides java. |
| public int |
| hashCode | back to summary |
|---|---|
| private final int hashCode The class loader's identity hash code. | |
| StorageKey | back to summary |
|---|---|
| protected StorageKey(ClassLoader classLoader, ReferenceQueue<? super ClassLoader> referenceQueue) Creates a new storage key.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
TypeCache where obsolete references are cleared upon any call.
TypeCache
| Access | Constructor and Description |
|---|---|
| public | WithInlineExpunction()
Creates a new type cache with inlined expunction and strong references to the stored types. |
| public | WithInlineExpunction(TypeCache.
The reference type to use for stored types. sortCreates a new type cache with inlined expunction. |
| Modifier and Type | Method and Description |
|---|---|
| public Class | find(ClassLoader
The class loader for which this type is stored or classLoader, S null for the bootstrap loader.The key for the type in question. key)Overrides net. null if no type was stored.
|
| public Class | findOrInsert(ClassLoader
The class loader for which this type is stored or classLoader, S null for the bootstrap loader.The key for the type in question. key, Callable<Class<?>> A lazy creator for the type to insert of no previous type was stored in the cache. builder)Overrides net. |
| public Class | findOrInsert(ClassLoader
The class loader for which this type is stored or classLoader, S null for the bootstrap loader.The key for the type in question. key, Callable<Class<?>> A lazy creator for the type to insert of no previous type was stored in the cache. builder, Object A monitor to lock before creating the lazy type. monitor)Overrides net. |
| public Class | insert(ClassLoader
The class loader for which this type is stored or classLoader, S null for the bootstrap loader.The key for the type in question. key, Class<?> The type to insert of no previous type was stored in the cache. type)Overrides net. |
| WithInlineExpunction | back to summary |
|---|---|
| public WithInlineExpunction() Creates a new type cache with inlined expunction and strong references to the stored types. | |
| WithInlineExpunction | back to summary |
|---|---|
| public WithInlineExpunction(TypeCache. Creates a new type cache with inlined expunction. | |
| find | back to summary |
|---|---|
| public Class Overrides net. Doc from net. Finds a stored type or returns
| |
| findOrInsert | back to summary |
|---|---|
| public Class Overrides net. Doc from net. Finds an existing type or inserts a new one if the previous type was not found.
| |
| findOrInsert | back to summary |
|---|---|
| public Class Overrides net. Doc from net. Finds an existing type or inserts a new one if the previous type was not found.
| |
| insert | back to summary |
|---|---|
| public Class Overrides net. Doc from net. 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.
| |