Class ClassInfo
- All Implemented Interfaces:
Finalizable
This class handles caching internally and it's advisable to not store
references directly to objects of this class. The static factory method
getClassInfo(Class) should be used to retrieve an instance
from the cache. Internally the Class associated with a ClassInfo
instance is kept as WeakReference, so it not safe to reference
and instance without the Class being either strongly or softly reachable.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for performing actions onClassInfoinstances. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears all modifiedExpandoMetaClassinstances for all classes.voidPerforms any cleanup required after the associated reference has been cleared.static intfullSize()Returns the total size including soft-referenced (potentially garbage-collectable) entries.static Collection<ClassInfo>Returns all cached class information across the runtime.Returns the class loader for loading class artifacts (e.g., compiled bytecode).Returns the cached class representation for this class.static ClassInfogetClassInfo(Class cls) Retrieves theClassInfofor the given class.final MetaClassReturns theMetaClassfor theClassassociated with thisClassInfo.getMetaClass(Object obj) Returns the metaclass for an object.Returns the most appropriate metaclass for this class.Returns the modifiedExpandoMetaClassfor this class, if one exists.Returns the per-instance metaclass for the given object, if one has been set.Returns the strong (immutable) metaclass for this class, if one has been set.final Class<?>Returns theClassassociated with thisClassInfo.intReturns the version number of this class information.Returns the weak (mutable) metaclass for this class, if one has been set.booleanReturns whether this class has any per-instance metaclasses associated with its objects.voidIncrements the version number and invalidates call sites.voidlock()Acquires a lock for this class information.static voidExecutes the given action on all cached class information.static voidRemoves aClassInfofrom the cache.voidsetPerInstanceMetaClass(Object obj, MetaClass metaClass) Sets the per-instance metaclass for the given object.voidsetStrongMetaClass(MetaClass answer) Sets the strong (immutable) metaclass for this class.voidsetWeakMetaClass(MetaClass answer) Sets the weak (mutable) metaclass for this class.static intsize()Returns the number of cached class information entries.voidunlock()Releases the lock for this class information.
-
Field Details
-
hash
public final int hash- See Also:
-
-
Method Details
-
getVersion
public int getVersion()Returns the version number of this class information. The version increments when the metaclass is modified (e.g., methods/properties added).- Returns:
- the current version
-
incVersion
public void incVersion()Increments the version number and invalidates call sites. Called when metaclass modifications occur (e.g., adding methods to anExpandoMetaClass). -
getModifiedExpando
Returns the modifiedExpandoMetaClassfor this class, if one exists.- Returns:
- the modified expando metaclass, or
nullif not modified or not an ExpandoMetaClass
-
clearModifiedExpandos
public static void clearModifiedExpandos()Clears all modifiedExpandoMetaClassinstances for all classes. Removes strong references to metaclasses and disassociates them from their class information. -
getTheClass
Returns theClassassociated with thisClassInfo.This method can return
nullif theClassis no longer reachable through any strong or soft references. A non-null return value indicates that thisClassInfois valid.- Returns:
- the
Classassociated with thisClassInfo, elsenull
-
getCachedClass
Returns the cached class representation for this class. Lazily initializes on first access.- Returns:
- the cached class
-
getArtifactClassLoader
Returns the class loader for loading class artifacts (e.g., compiled bytecode). Lazily initializes on first access.- Returns:
- the artifact class loader
-
getClassInfo
Retrieves theClassInfofor the given class. Lazily creates one if not already cached.- Parameters:
cls- the class to get information for- Returns:
- the class information, or
nullifclsisnull
-
remove
Removes aClassInfofrom the cache. This is useful in cases where the Class is parsed from a script, such as when using GroovyClassLoader#parseClass, and is executed for its result but the Class is not retained or cached. Removing theClassInfoassociated with the Class will make the Class and its ClassLoader eligible for garbage collection sooner that it would otherwise.- Parameters:
cls- the Class associated with the ClassInfo to remove from cache
-
getAllClassInfo
Returns all cached class information across the runtime.- Returns:
- a collection of all cached class info objects
-
onAllClassInfo
Executes the given action on all cached class information. Allows processing of all classes currently tracked by the runtime.- Parameters:
action- the action to execute on each ClassInfo
-
getStrongMetaClass
Returns the strong (immutable) metaclass for this class, if one has been set. A strong reference keeps the metaclass in memory regardless of garbage collection.- Returns:
- the strong metaclass, or
nullif not set
-
setStrongMetaClass
Sets the strong (immutable) metaclass for this class. Increments the version number and manages ExpandoMetaClass registry.- Parameters:
answer- the metaclass to set, ornullto clear
-
getWeakMetaClass
Returns the weak (mutable) metaclass for this class, if one has been set. A weak reference allows garbage collection of the metaclass when no longer needed.- Returns:
- the weak metaclass, or
nullif not set or if it has been garbage collected
-
setWeakMetaClass
Sets the weak (mutable) metaclass for this class. Clears the strong metaclass and increments the version number.- Parameters:
answer- the metaclass to set, ornullto clear
-
getMetaClassForClass
Returns the most appropriate metaclass for this class. Prefers strong metaclass if available, then weak metaclass if valid, otherwise the default.- Returns:
- the metaclass for this class
-
getMetaClass
Returns theMetaClassfor theClassassociated with thisClassInfo. If noMetaClassexists one will be created.It is not safe to call this method without a
Classassociated with thisClassInfo. It is advisable to always retrieve a ClassInfo instance from the cache by using the static factory methodgetClassInfo(Class)to ensure the referenced Class is strongly reachable.- Returns:
- a
MetaClassinstance
-
getMetaClass
Returns the metaclass for an object. If the object has a per-instance metaclass, returns that; otherwise returns this class's metaclass.- Parameters:
obj- the object to get the metaclass for- Returns:
- the metaclass for the object
-
size
public static int size()Returns the number of cached class information entries.- Returns:
- the count of cached ClassInfo instances
-
fullSize
public static int fullSize()Returns the total size including soft-referenced (potentially garbage-collectable) entries.- Returns:
- the full count of class information entries
-
lock
public void lock()Acquires a lock for this class information. Used to synchronize modifications to metaclass and per-instance metaclass maps. -
unlock
public void unlock()Releases the lock for this class information. -
getPerInstanceMetaClass
Returns the per-instance metaclass for the given object, if one has been set.- Parameters:
obj- the object to get the per-instance metaclass for- Returns:
- the per-instance metaclass, or
nullif not set
-
setPerInstanceMetaClass
Sets the per-instance metaclass for the given object. Per-instance metaclasses override the class-level metaclass for that specific object.- Parameters:
obj- the object to associate with a metaclassmetaClass- the metaclass to set, ornullto remove the association
-
hasPerInstanceMetaClasses
public boolean hasPerInstanceMetaClasses()Returns whether this class has any per-instance metaclasses associated with its objects.- Returns:
trueif one or more per-instance metaclasses have been set;falseotherwise
-
finalizeReference
public void finalizeReference()Description copied from interface:FinalizablePerforms any cleanup required after the associated reference has been cleared.- Specified by:
finalizeReferencein interfaceFinalizable
-