Class CachedClass

java.lang.Object
org.codehaus.groovy.reflection.CachedClass
Direct Known Subclasses:
ArrayCachedClass, BooleanCachedClass, CachedClosureClass, CachedSAMClass, CharacterCachedClass, NumberCachedClass, ObjectCachedClass, StringCachedClass

public class CachedClass extends Object
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.

  • Field Details

    • EMPTY_ARRAY

      public static final CachedClass[] EMPTY_ARRAY
      An empty array constant representing zero cached classes.
    • classInfo

      public ClassInfo classInfo
      ClassInfo object for this class, holding metadata about the class.
    • isArray

      public final boolean isArray
      true if the cached class is an array type, false otherwise.
    • isPrimitive

      public final boolean isPrimitive
      true if the cached class is a primitive type, false otherwise.
    • modifiers

      public final int modifiers
      The modifiers of this class (e.g., public, final, abstract). See Modifier for constants.
    • isInterface

      public final boolean isInterface
      true if the cached class is an interface, false otherwise.
    • isNumber

      public final boolean isNumber
      true if the cached class is assignable from Number, false otherwise.
    • mopMethods

      public CachedMethod[] mopMethods
      Array of MOP (Meta-Object Protocol) methods, including inherited methods. Updated when methods are added via setNewMopMethods(List) or addNewMopMethods(List).
  • Constructor Details

    • CachedClass

      public CachedClass(Class<?> klazz, ClassInfo classInfo)
      Constructs a CachedClass for the given class with the specified ClassInfo.
      Parameters:
      klazz - the class to cache reflection information for
      classInfo - the ClassInfo object associated with this cached class
  • Method Details

    • getCachedSuperClass

      public CachedClass getCachedSuperClass()
      Returns the cached superclass of this class, if any.
      Returns:
      the cached superclass, or null if this is Object or an interface
    • getInterfaces

      public Set<CachedClass> 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

      public Set<CachedClass> getDeclaredInterfaces()
      Returns a set of interfaces directly declared by this class (not including inherited ones).
      Returns:
      a set of directly declared cached interfaces
    • getMethods

      public CachedMethod[] getMethods()
      Returns all public and protected methods of this class and its superclasses.
      Returns:
      an array of cached methods, sorted by name
    • getFields

      public CachedField[] getFields()
      Returns all public and protected fields declared in this class.
      Returns:
      an array of cached fields
    • getConstructors

      public CachedConstructor[] getConstructors()
      Returns all public and protected constructors declared in this class.
      Returns:
      an array of cached constructors
    • searchMethods

      public CachedMethod searchMethods(String name, CachedClass[] parameterTypes)
      Searches for a method with the specified name and parameter types in this class.
      Parameters:
      name - the name of the method to search for
      parameterTypes - the parameter types of the method
      Returns:
      the matching cached method, or null if 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

      public Object coerceArgument(Object argument)
      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 to Object.
      Returns:
      the superclass distance (1 for Object, 2 for direct subclasses of Object, etc.)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • isPrimitive

      public boolean isPrimitive()
      Returns whether this class represents a primitive type.
      Returns:
      true if this is a primitive type (int, double, etc.), false otherwise
    • isVoid

      public boolean isVoid()
      Returns whether this class represents the void type.
      Returns:
      true if this class is void, false otherwise
    • isInterface

      public boolean isInterface()
      Returns whether this class represents an interface type.
      Returns:
      true if this is an interface, false otherwise
    • getName

      public String getName()
      Returns the fully qualified name of this class.
      Returns:
      the class name
    • getTypeDescription

      public String getTypeDescription()
      Returns the bytecode type descriptor for this class.
      Returns:
      the type descriptor string
    • getTheClass

      public final Class<?> getTheClass()
      Returns the underlying Java Class object wrapped by this cached class.
      Returns:
      the class object
    • getNewMetaMethods

      public MetaMethod[] 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

      public void setNewMopMethods(List<MetaMethod> arr)
      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, or null to revert to default
      Throws:
      GroovyRuntimeException - if a strong custom meta-class is already set
    • addNewMopMethods

      public void addNewMopMethods(List<MetaMethod> arr)
      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

      public boolean isAssignableFrom(Class<?> argument)
      Returns whether the argument class is assignable to this class. Returns true for null arguments.
      Parameters:
      argument - the class to check
      Returns:
      true if argument is null or assignable to this class
    • isDirectlyAssignable

      public boolean isDirectlyAssignable(Object argument)
      Returns whether an object instance is directly assignable to this class.
      Parameters:
      argument - the object to check
      Returns:
      true if the object's class is assignable to this class
    • getCallSiteLoader

      public CallSiteClassLoader getCallSiteLoader()
      Returns the class loader used for generating call site classes for this cached class.
      Returns:
      a call site class loader
    • getHierarchy

      public Collection<ClassInfo> getHierarchy()
      Returns the complete type hierarchy for this class, including superclasses and interfaces.
      Returns:
      a collection of ClassInfo objects in the hierarchy
    • getCachedClass

      public CachedClass getCachedClass()
      Returns this cached class (for compatibility).
      Returns:
      this CachedClass
    • toString

      public String toString()
      Overrides:
      toString in class Object