Package org.codehaus.groovy.reflection
Class CachedClass
java.lang.Object
org.codehaus.groovy.reflection.CachedClass
- Direct Known Subclasses:
ArrayCachedClass,BooleanCachedClass,CachedClosureClass,CachedSAMClass,CharacterCachedClass,NumberCachedClass,ObjectCachedClass,StringCachedClass
Caches reflection information for a class including its fields, methods, constructors, and interfaces.
Lazily initializes cached members on first access and provides utility methods for searching methods,
checking assignability, and managing MOP methods. Instances are typically obtained via ReflectionCache.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classComparator for ordering cached methods by name.static classComparator for ordering cached methods against string names. -
Field Summary
FieldsModifier and TypeFieldDescriptionClassInfoobject for this class, holding metadata about the class.static final CachedClass[]An empty array constant representing zero cached classes.final booleantrueif the cached class is an array type,falseotherwise.final booleantrueif the cached class is an interface,falseotherwise.final booleanfinal booleantrueif the cached class is a primitive type,falseotherwise.final intThe modifiers of this class (e.g., public, final, abstract).Array of MOP (Meta-Object Protocol) methods, including inherited methods. -
Constructor Summary
ConstructorsConstructorDescriptionCachedClass(Class<?> klazz, ClassInfo classInfo) Constructs aCachedClassfor the given class with the specifiedClassInfo. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNewMopMethods(List<MetaMethod> arr) Adds a list of new meta-methods to this class's existing MOP methods.coerceArgument(Object argument) Coerces an argument to a form suitable for this class.Returns this cached class (for compatibility).Returns the cached superclass of this class, if any.Returns the class loader used for generating call site classes for this cached class.Returns all public and protected constructors declared in this class.Returns a set of interfaces directly declared by this class (not including inherited ones).Returns all public and protected fields declared in this class.Returns the complete type hierarchy for this class, including superclasses and interfaces.Returns a set of all interfaces implemented or extended by this class, including inherited interfaces.Returns all public and protected methods of this class and its superclasses.intReturns the access modifier flags for this class.getName()Returns the fully qualified name of this class.Returns a list of new meta-methods added to this class's meta-class.intComputes the distance from this class to the superclass hierarchy, used for type matching.final Class<?>Returns the underlying JavaClassobject wrapped by this cached class.Returns the bytecode type descriptor for this class.inthashCode()booleanisAssignableFrom(Class<?> argument) Returns whether the argument class is assignable to this class.booleanisDirectlyAssignable(Object argument) Returns whether an object instance is directly assignable to this class.booleanReturns whether this class represents an interface type.booleanReturns whether this class represents a primitive type.booleanisVoid()Returns whether this class represents the void type.searchMethods(String name, CachedClass[] parameterTypes) Searches for a method with the specified name and parameter types in this class.voidsetNewMopMethods(List<MetaMethod> arr) Replaces the current MOP methods with the specified list of meta-methods.toString()
-
Field Details
-
EMPTY_ARRAY
An empty array constant representing zero cached classes. -
classInfo
ClassInfoobject for this class, holding metadata about the class. -
isArray
public final boolean isArraytrueif the cached class is an array type,falseotherwise. -
isPrimitive
public final boolean isPrimitivetrueif the cached class is a primitive type,falseotherwise. -
modifiers
public final int modifiersThe modifiers of this class (e.g., public, final, abstract). SeeModifierfor constants. -
isInterface
public final boolean isInterfacetrueif the cached class is an interface,falseotherwise. -
isNumber
public final boolean isNumber -
mopMethods
Array of MOP (Meta-Object Protocol) methods, including inherited methods. Updated when methods are added viasetNewMopMethods(List)oraddNewMopMethods(List).
-
-
Constructor Details
-
CachedClass
Constructs aCachedClassfor the given class with the specifiedClassInfo.- Parameters:
klazz- the class to cache reflection information forclassInfo- theClassInfoobject associated with this cached class
-
-
Method Details
-
getCachedSuperClass
Returns the cached superclass of this class, if any.- Returns:
- the cached superclass, or
nullif this isObjector an interface
-
getInterfaces
Returns a set of all interfaces implemented or extended by this class, including inherited interfaces. If this class is an interface, it includes itself in the set.- Returns:
- a set of cached interfaces
-
getDeclaredInterfaces
Returns a set of interfaces directly declared by this class (not including inherited ones).- Returns:
- a set of directly declared cached interfaces
-
getMethods
Returns all public and protected methods of this class and its superclasses.- Returns:
- an array of cached methods, sorted by name
-
getFields
Returns all public and protected fields declared in this class.- Returns:
- an array of cached fields
-
getConstructors
Returns all public and protected constructors declared in this class.- Returns:
- an array of cached constructors
-
searchMethods
Searches for a method with the specified name and parameter types in this class.- Parameters:
name- the name of the method to search forparameterTypes- the parameter types of the method- Returns:
- the matching cached method, or
nullif not found. If multiple matches exist, returns the one with the most specific return type
-
getModifiers
public int getModifiers()Returns the access modifier flags for this class.- Returns:
- the modifiers as an integer, see
Modifier
-
coerceArgument
Coerces an argument to a form suitable for this class.By default, returns the argument unchanged. Subclasses may override to provide type-specific coercion logic.
- Parameters:
argument- the argument to coerce- Returns:
- the coerced argument
-
getSuperClassDistance
public int getSuperClassDistance()Computes the distance from this class to the superclass hierarchy, used for type matching. The distance is the number of steps up the inheritance chain from this class toObject.- Returns:
- the superclass distance (1 for Object, 2 for direct subclasses of Object, etc.)
-
hashCode
public int hashCode() -
isPrimitive
public boolean isPrimitive()Returns whether this class represents a primitive type.- Returns:
trueif this is a primitive type (int, double, etc.),falseotherwise
-
isVoid
public boolean isVoid()Returns whether this class represents the void type.- Returns:
trueif this class is void,falseotherwise
-
isInterface
public boolean isInterface()Returns whether this class represents an interface type.- Returns:
trueif this is an interface,falseotherwise
-
getName
Returns the fully qualified name of this class.- Returns:
- the class name
-
getTypeDescription
Returns the bytecode type descriptor for this class.- Returns:
- the type descriptor string
-
getTheClass
Returns the underlying JavaClassobject wrapped by this cached class.- Returns:
- the class object
-
getNewMetaMethods
Returns a list of new meta-methods added to this class's meta-class. Includes expando methods and methods from all superclasses and interfaces.- Returns:
- an array of meta-methods
-
setNewMopMethods
Replaces the current MOP methods with the specified list of meta-methods. Reinitializes the meta-class to reflect the new methods.- Parameters:
arr- the new list of meta-methods to use, ornullto revert to default- Throws:
GroovyRuntimeException- if a strong custom meta-class is already set
-
addNewMopMethods
Adds a list of new meta-methods to this class's existing MOP methods. Reinitializes the meta-class to incorporate the new methods alongside existing ones.- Parameters:
arr- the list of meta-methods to add- Throws:
GroovyRuntimeException- if a strong custom meta-class is already set
-
isAssignableFrom
Returns whether the argument class is assignable to this class. Returnstruefornullarguments.- Parameters:
argument- the class to check- Returns:
trueifargumentisnullor assignable to this class
-
isDirectlyAssignable
Returns whether an object instance is directly assignable to this class.- Parameters:
argument- the object to check- Returns:
trueif the object's class is assignable to this class
-
getCallSiteLoader
Returns the class loader used for generating call site classes for this cached class.- Returns:
- a call site class loader
-
getHierarchy
Returns the complete type hierarchy for this class, including superclasses and interfaces.- Returns:
- a collection of
ClassInfoobjects in the hierarchy
-
getCachedClass
Returns this cached class (for compatibility).- Returns:
- this
CachedClass
-
toString
-