Package org.codehaus.groovy.reflection

# Class CachedMethod

java.lang.Object
org.codehaus.groovy.reflection.ParameterTypes
groovy.lang.MetaMethod
org.codehaus.groovy.reflection.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 for MOP and invokedynamic dispatch.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `final CachedClass`
  `cachedClass`
  The cached class that declares this method.
  `static final CachedMethod[]`
  `EMPTY_ARRAY`
  An empty array constant representing zero cached methods.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CachedMethod(Method method)`
  Constructs a `CachedMethod` for the given Java method.
  `CachedMethod(CachedClass clazz, Method method)`
  Constructs a `CachedMethod` for the given method within a cached class.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `canAccessLegally(Class<?> callerClass)`
  Checks whether the given caller class can legally access this method.
  `int`
  `compareTo(Object other)`
  Compares this cached method with another method-like object for ordering.
  `boolean`
  `equals(Object other)`
  Checks equality with another method.
  `static CachedMethod`
  `find(Method method)`
  Finds a `CachedMethod` corresponding to the specified Java method by searching the cached methods of its declaring class.
  `<T extends Annotation>
  T`
  `getAnnotation(Class<T> annotationClass)`
  Returns the annotation of the specified type on this method, if present.
  `Method`
  `getCachedMethod()`
  Returns the underlying Java method, making it accessible if necessary.
  `CachedClass`
  `getDeclaringClass()`
  Returns the cached class that declares this method.
  `String`
  `getDescriptor()`
  Returns the bytecode method descriptor for this method (e.g., "(II)Z").
  `int`
  `getModifiers()`
  Returns the modifiers of this method (e.g., public, static, synchronized).
  `String`
  `getName()`
  Returns the name of this method.
  `int`
  `getParamsCount()`
  Returns the number of parameters this method accepts.
  `ParameterTypes`
  `getParamTypes()`
  Returns the parameter type information for this method.
  `Class[]`
  `getPT()`
  Returns the native Java parameter types of this method.
  `Class`
  `getReturnType()`
  Returns the return type of this method.
  `String`
  `getSignature()`
  Returns the complete method signature (name + bytecode descriptor).
  `CachedMethod`
  `getTransformedMethod()`
  Returns the transformed version of this method, if one exists.
  `int`
  `hashCode()`
  Returns the hash code for this cached method based on the underlying Java method.
  `final Object`
  `invoke(Object object, Object[] arguments)`
  Invokes this method on the given object with the specified arguments.
  `boolean`
  `isCallerSensitive()`
  Indicates whether the underlying method is caller-sensitive, meaning its behaviour depends on the immediate caller — such methods must not be invoked through intermediaries that change the observed caller.
  `boolean`
  `isSynthetic()`
  Returns whether this method is synthetic (generated by the compiler).
  `final Method`
  `setAccessible()`
  Makes this method accessible and returns the underlying Java method.
  `void`
  `setTransformedMethod(CachedMethod transformedMethod)`
  Sets the transformed version of this method.
  `String`
  `toString()`
  Returns the string representation of the underlying Java method.

  ### Methods inherited from class groovy.lang.MetaMethod

  `checkParameters, clone, doMethodInvoke, equal, equal, getMopName, isAbstract, isCacheable, isDefault, isMethod, isSame, processDoMethodInvokeException`

  ### Methods inherited from class org.codehaus.groovy.reflection.ParameterTypes

  `coerceArgumentsToClasses, correctArguments, getNativeParameterTypes, getParameterTypes, isValidExactMethod, isValidExactMethod, isValidMethod, isValidMethod, isVargsMethod, isVargsMethod, setParametersTypes`

  ### Methods inherited from class java.lang.Object

  `finalize, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface groovy.lang.MetaMember

  `isFinal, isPackagePrivate, isPrivate, isProtected, isPublic, isStatic`

- ## 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

  - ### 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.

    After `groovy.cachedmethod.invoker.threshold` hits (default 1000) a `DirectInvoker` may be installed so the call is a JIT-constant `INVOKE*` / classData `invokeExact` rather than `Method.invoke(java.lang.Object, java.lang.Object...)`. Caller-sensitive and abstract methods stay reflective. This is the MOP "Groovy as caller" path — the trampoline must not be used as an invokedynamic target.

    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)
      `ClassCastException` - on the generated path when an argument cannot be `CHECKCAST` to the parameter type (the reflective path wraps `IllegalArgumentException` in `InvokerInvocationException` instead; same delta as DGM / `CallSiteGenerator`). Wrong arity does not take that shortcut: too few or extra arguments fall back to `Method.invoke(java.lang.Object, java.lang.Object...)` so the wrapped `IllegalArgumentException` is preserved.

  - ### isCallerSensitive

    public boolean isCallerSensitive()
    Indicates whether the underlying method is caller-sensitive, meaning its behaviour depends on the immediate caller — such methods must not be invoked through intermediaries that change the observed caller. This covers the `jdk.internal.reflect.CallerSensitive` annotation and declaring-class-based cases that are caller-context sensitive without the annotation (object serialization's stack-walking loader lookup). Computed once and cached; when the probe cannot decide, the method is conservatively treated as caller-sensitive. Note that an abstract method never carries the annotation itself: callers dispatching virtually should also probe the runtime receiver's implementation.
    Returns:
      `true` if the method is (or must be assumed) caller-sensitive
    Since:
      6.0.0

  - ### 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