public class ClassInfo
extends Object
implements Finalizable
Handle for all information we want to keep about the class
This class handles caching internally and it's advisable to not store
references directly to objects of this class. The static factory method
ClassInfo.getClassInfo 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.
| Modifiers | Name | Description |
|---|---|---|
interface |
ClassInfo.ClassInfoAction |
Functional interface for performing actions on ClassInfo instances. |
| Modifiers | Name | Description |
|---|---|---|
int |
hash |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static void |
clearModifiedExpandos()Clears all modified ExpandoMetaClass instances for all classes.
|
|
public void |
finalizeReference() |
|
public static int |
fullSize()Returns the total size including soft-referenced (potentially garbage-collectable) entries. |
|
public static Collection<ClassInfo> |
getAllClassInfo()Returns all cached class information across the runtime. |
|
public ClassLoaderForClassArtifacts |
getArtifactClassLoader()Returns the class loader for loading class artifacts (e.g., compiled bytecode). |
|
public CachedClass |
getCachedClass()Returns the cached class representation for this class. |
|
public static ClassInfo |
getClassInfo(Class cls)Retrieves the ClassInfo for the given class.
|
|
public final MetaClass |
getMetaClass()Returns the MetaClass for the Class associated with this ClassInfo.
|
|
public MetaClass |
getMetaClass(Object obj)Returns the metaclass for an object. |
|
public MetaClass |
getMetaClassForClass()Returns the most appropriate metaclass for this class. |
|
public ExpandoMetaClass |
getModifiedExpando()Returns the modified ExpandoMetaClass for this class, if one exists. |
|
public MetaClass |
getPerInstanceMetaClass(Object obj)Returns the per-instance metaclass for the given object, if one has been set. |
|
public MetaClass |
getStrongMetaClass()Returns the strong (immutable) metaclass for this class, if one has been set. |
|
public final Class<?> |
getTheClass()Returns the Class associated with this ClassInfo. |
|
public int |
getVersion()Returns the version number of this class information. |
|
public MetaClass |
getWeakMetaClass()Returns the weak (mutable) metaclass for this class, if one has been set. |
|
public boolean |
hasPerInstanceMetaClasses()Returns whether this class has any per-instance metaclasses associated with its objects. |
|
public void |
incVersion()Increments the version number and invalidates call sites. |
|
public void |
lock()Acquires a lock for this class information. |
|
public static void |
onAllClassInfo(ClassInfo.ClassInfoAction action)Executes the given action on all cached class information. |
|
public static void |
remove(Class<?> cls)Removes a ClassInfo from the cache. |
|
public void |
setPerInstanceMetaClass(Object obj, MetaClass metaClass)Sets the per-instance metaclass for the given object. |
|
public void |
setStrongMetaClass(MetaClass answer)Sets the strong (immutable) metaclass for this class. |
|
public void |
setWeakMetaClass(MetaClass answer)Sets the weak (mutable) metaclass for this class. |
|
public static int |
size()Returns the number of cached class information entries. |
|
public void |
unlock()Releases the lock for this class information. |
Clears all modified ExpandoMetaClass instances for all classes.
Removes strong references to metaclasses and disassociates them from their class information.
Returns the total size including soft-referenced (potentially garbage-collectable) entries.
Returns all cached class information across the runtime.
Returns the class loader for loading class artifacts (e.g., compiled bytecode). Lazily initializes on first access.
Returns the cached class representation for this class. Lazily initializes on first access.
Retrieves the ClassInfo for the given class.
Lazily creates one if not already cached.
cls - the class to get information fornull if cls is null Returns the MetaClass for the Class associated with this ClassInfo.
If no MetaClass exists one will be created.
It is not safe to call this method without a Class associated with this ClassInfo.
It is advisable to always retrieve a ClassInfo instance from the cache by using the static
factory method ClassInfo.getClassInfo to ensure the referenced Class is
strongly reachable.
MetaClass instanceReturns the metaclass for an object. If the object has a per-instance metaclass, returns that; otherwise returns this class's metaclass.
obj - the object to get the metaclass forReturns the most appropriate metaclass for this class. Prefers strong metaclass if available, then weak metaclass if valid, otherwise the default.
Returns the modified ExpandoMetaClass for this class, if one exists.
null if not modified or not an ExpandoMetaClassReturns the per-instance metaclass for the given object, if one has been set.
obj - the object to get the per-instance metaclass fornull if not setReturns the strong (immutable) metaclass for this class, if one has been set. A strong reference keeps the metaclass in memory regardless of garbage collection.
null if not set Returns the Class associated with this ClassInfo.
This method can return null if the Class is no longer reachable
through any strong or soft references. A non-null return value indicates that this
ClassInfo is valid.
Class associated with this ClassInfo, else nullReturns the version number of this class information. The version increments when the metaclass is modified (e.g., methods/properties added).
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.
null if not set or if it has been garbage collectedReturns whether this class has any per-instance metaclasses associated with its objects.
true if one or more per-instance metaclasses have been set; false otherwise Increments the version number and invalidates call sites.
Called when metaclass modifications occur (e.g., adding methods to an ExpandoMetaClass).
Acquires a lock for this class information. Used to synchronize modifications to metaclass and per-instance metaclass maps.
Executes the given action on all cached class information. Allows processing of all classes currently tracked by the runtime.
action - the action to execute on each ClassInfo Removes a ClassInfo from 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 the ClassInfo associated with the Class
will make the Class and its ClassLoader eligible for garbage collection sooner that
it would otherwise.
cls - the Class associated with the ClassInfo to remove
from cacheSets the per-instance metaclass for the given object. Per-instance metaclasses override the class-level metaclass for that specific object.
obj - the object to associate with a metaclassmetaClass - the metaclass to set, or null to remove the associationSets the strong (immutable) metaclass for this class. Increments the version number and manages ExpandoMetaClass registry.
answer - the metaclass to set, or null to clearSets the weak (mutable) metaclass for this class. Clears the strong metaclass and increments the version number.
answer - the metaclass to set, or null to clearReturns the number of cached class information entries.
Releases the lock for this class information.