Class CachedMethod

All Implemented Interfaces:
MetaMember, Cloneable, Comparable

public class CachedMethod extends MetaMethod implements Comparable
Caches reflection information about a single method for fast lookup and invocation.

Extends MetaMethod and implements Comparable for method sorting. Provides efficient access to method metadata and supports call site optimization.

  • Field Details

    • EMPTY_ARRAY

      public static final CachedMethod[] EMPTY_ARRAY
      An empty array constant representing zero cached methods.
    • cachedClass

      public final CachedClass cachedClass
      The cached class that declares this method.
  • Constructor Details

    • CachedMethod

      public CachedMethod(CachedClass clazz, Method method)
      Constructs a CachedMethod for the given method within a cached class.
      Parameters:
      clazz - the cached class that declares this method
      method - the Java method to cache
    • CachedMethod

      public CachedMethod(Method method)
      Constructs a CachedMethod for the given Java method. Automatically resolves the cached class from the method's declaring class.
      Parameters:
      method - the Java method to cache
  • Method Details

    • find

      public static CachedMethod find(Method method)
      Finds a CachedMethod corresponding to the specified Java method by searching the cached methods of its declaring class.
      Parameters:
      method - the method to find
      Returns:
      the cached method, or null if not found
    • compareTo

      public int compareTo(Object other)
      Compares this cached method with another method-like object for ordering. Comparison is based on method name, return type, parameters, and declaring class.
      Specified by:
      compareTo in interface Comparable
      Parameters:
      other - the object to compare with (typically another CachedMethod or Method)
      Returns:
      a negative, zero, or positive value if this method is less than, equal to, or greater than the other
    • equals

      public boolean equals(Object other)
      Checks equality with another method. Two methods are equal if they represent the same underlying Java method.
      Overrides:
      equals in class Object
      Parameters:
      other - the object to compare with
      Returns:
      true if this method equals the other object; false otherwise
    • hashCode

      public int hashCode()
      Returns the hash code for this cached method based on the underlying Java method.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • toString

      public String toString()
      Returns the string representation of the underlying Java method.
      Overrides:
      toString in class MetaMethod
      Returns:
      a string representation of this cached method
    • canAccessLegally

      public boolean canAccessLegally(Class<?> callerClass)
      Checks whether the given caller class can legally access this method.
      Parameters:
      callerClass - the class attempting to access this method
      Returns:
      true if access is allowed; false otherwise
    • getAnnotation

      public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
      Returns the annotation of the specified type on this method, if present.
      Type Parameters:
      T - the annotation type
      Parameters:
      annotationClass - the class of the annotation to retrieve
      Returns:
      the annotation instance, or null if not present
    • getCachedMethod

      public Method getCachedMethod()
      Returns the underlying Java method, making it accessible if necessary.
      Returns:
      the cached method with accessibility ensured
    • getDeclaringClass

      public CachedClass getDeclaringClass()
      Returns the cached class that declares this method.
      Specified by:
      getDeclaringClass in class MetaMethod
      Returns:
      the declaring cached class
    • getDescriptor

      public String getDescriptor()
      Returns the bytecode method descriptor for this method (e.g., "(II)Z"). The descriptor encodes parameter and return types in JVM format.
      Overrides:
      getDescriptor in class MetaMethod
      Returns:
      the method descriptor string
    • getModifiers

      public int getModifiers()
      Returns the modifiers of this method (e.g., public, static, synchronized). See Modifier for modifier constants.
      Specified by:
      getModifiers in interface MetaMember
      Specified by:
      getModifiers in class MetaMethod
      Returns:
      the method modifiers
    • getName

      public String getName()
      Returns the name of this method.
      Specified by:
      getName in interface MetaMember
      Specified by:
      getName in class MetaMethod
      Returns:
      the method name
    • getParamsCount

      public int getParamsCount()
      Returns the number of parameters this method accepts.
      Returns:
      the parameter count
    • getParamTypes

      public ParameterTypes getParamTypes()
      Returns the parameter type information for this method.

      Note: This method always returns null and is retained for compatibility.

      Returns:
      null
    • getPT

      public Class[] getPT()
      Returns the native Java parameter types of this method.
      Overrides:
      getPT in class ParameterTypes
      Returns:
      an array of parameter classes
    • getReturnType

      public Class getReturnType()
      Returns the return type of this method.
      Specified by:
      getReturnType in class MetaMethod
      Returns:
      the method's return class
    • getSignature

      public String getSignature()
      Returns the complete method signature (name + bytecode descriptor). For example: "toString()Ljava/lang/String;"
      Overrides:
      getSignature in class MetaMethod
      Returns:
      the method signature
    • isSynthetic

      public boolean isSynthetic()
      Returns whether this method is synthetic (generated by the compiler).
      Specified by:
      isSynthetic in interface MetaMember
      Returns:
      true if this method is synthetic; false otherwise
    • getTransformedMethod

      public CachedMethod getTransformedMethod()
      Returns the transformed version of this method, if one exists. Transformed methods are compiler-optimized variants.
      Returns:
      the transformed method, or null if no transformation exists
    • setTransformedMethod

      public void setTransformedMethod(CachedMethod transformedMethod)
      Sets the transformed version of this method.
      Parameters:
      transformedMethod - the transformed method, or null to clear
    • createPogoMetaMethodSite

      public CallSite createPogoMetaMethodSite(CallSite site, MetaClassImpl metaClass, Class[] params)
      Creates an optimized call site for invoking this method on POGO (Groovy) objects. Attempts to compile the method for maximum performance.
      Parameters:
      site - the call site being created
      metaClass - the metaclass of the target object's class
      params - the parameter types of the call
      Returns:
      a call site optimized for POGO invocation
    • createPojoMetaMethodSite

      public CallSite createPojoMetaMethodSite(CallSite site, MetaClassImpl metaClass, Class[] params)
      Creates an optimized call site for invoking this method on POJO (plain Java) objects. Attempts to compile the method for maximum performance.
      Parameters:
      site - the call site being created
      metaClass - the metaclass of the target object's class
      params - the parameter types of the call
      Returns:
      a call site optimized for POJO invocation
    • createStaticMetaMethodSite

      public CallSite createStaticMetaMethodSite(CallSite site, MetaClassImpl metaClass, Class[] params)
      Creates an optimized call site for invoking this static method. Attempts to compile the method for maximum performance.
      Parameters:
      site - the call site being created
      metaClass - the metaclass associated with the static method
      params - the parameter types of the call
      Returns:
      a call site optimized for static method invocation
    • invoke

      public final Object invoke(Object object, Object[] arguments)
      Invokes this method on the given object with the specified arguments. Handles accessibility, exception wrapping, and method invocation.
      Specified by:
      invoke in class MetaMethod
      Parameters:
      object - the target object (may be null for static methods)
      arguments - the arguments to pass to the method
      Returns:
      the result of the method invocation
      Throws:
      InvokerInvocationException - if the method raises a checked exception or illegal access occurs
      RuntimeException - if the method raises an uncaught exception (except MissingMethodException)
    • setAccessible

      public final Method setAccessible()
      Makes this method accessible and returns the underlying Java method. Synonym for getCachedMethod().
      Returns:
      the cached method with accessibility ensured