Package: org.codehaus.groovy.reflection

## [Java] Class CachedMethod

- org.codehaus.groovy.reflection.CachedMethod

- All Implemented Interfaces and Traits:
    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
    Modifiers | Name | Description
    --- | --- | ---
    `static CachedMethod[]` | `EMPTY_ARRAY` | An empty array constant representing zero cached methods.
    `CachedClass` | `cachedClass` | The cached class that declares this method.

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class MetaMethod` | `EMPTY_ARRAY`

  - ### Constructor Summary

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

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public boolean` | `canAccessLegally(Class<?> callerClass)` Checks whether the given caller class can legally access this method.
    `` | `public int` | `compareTo(Object other)` Compares this cached method with another method-like object for ordering.
    `` | `public boolean` | `equals(Object other)` Checks equality with another method.
    `` | `public 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>` | `public T` | `getAnnotation(Class<T> annotationClass)` Returns the annotation of the specified type on this method, if present.
    `` | `public Method` | `getCachedMethod()` Returns the underlying Java method, making it accessible if necessary.
    `` | `public CachedClass` | `getDeclaringClass()` Returns the cached class that declares this method.
    `` | `public String` | `getDescriptor()` Returns the bytecode method descriptor for this method (e.g., "(II)Z").
    `` | `public int` | `getModifiers()` Returns the modifiers of this method (e.g., public, static, synchronized).
    `` | `public String` | `getName()` Returns the name of this method.
    `` | `public Class[]` | `getPT()` Returns the native Java parameter types of this method.
    `` | `public ParameterTypes` | `getParamTypes()` Returns the parameter type information for this method.
    `` | `public int` | `getParamsCount()` Returns the number of parameters this method accepts.
    `` | `public Class` | `getReturnType()` Returns the return type of this method.
    `` | `public String` | `getSignature()` Returns the complete method signature (name + bytecode descriptor).
    `` | `public CachedMethod` | `getTransformedMethod()` Returns the transformed version of this method, if one exists.
    `` | `public int` | `hashCode()` Returns the hash code for this cached method based on the underlying Java method.
    `` | `public final Object` | `invoke(Object object, Object[] arguments)` Invokes this method on the given object with the specified arguments.
    `` | `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.
    `` | `public boolean` | `isSynthetic()` Returns whether this method is synthetic (generated by the compiler).
    `` | `public final Method` | `setAccessible()` Makes this method accessible and returns the underlying Java method.
    `` | `public void` | `setTransformedMethod(CachedMethod transformedMethod)` Sets the transformed version of this method.
    `` | `public String` | `toString()` Returns the string representation of the underlying Java method.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class MetaMethod` | `checkParameters, clone, doMethodInvoke, equal, equal, getDeclaringClass, getDescriptor, getModifiers, getMopName, getName, getReturnType, getSignature, invoke, isAbstract, isCacheable, isDefault, isMethod, isSame, processDoMethodInvokeException, toString`
    `class ParameterTypes` | `coerceArgumentsToClasses, correctArguments, getNativeParameterTypes, getPT, getParameterTypes, isValidExactMethod, isValidExactMethod, isValidMethod, isValidMethod, isVargsMethod, isVargsMethod, setParametersTypes`

- - ### Field Detail

    - #### public static final CachedMethod[] EMPTY_ARRAY

      An empty array constant representing zero cached methods.

    - #### public final CachedClass cachedClass

      The cached class that declares this method.

  - ### Constructor Detail

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

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

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

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

      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

    - #### public boolean equals(Object other)

      Checks equality with another method. Two methods are equal if they represent the same underlying Java method.

      Parameters:
        `other` - the object to compare with
      Returns:
        `true` if this method equals the other object; `false` otherwise

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

    - #### <T extends Annotation> public 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

    - #### public Method getCachedMethod()

      Returns the underlying Java method, making it accessible if necessary.

      Returns:
        the cached method with accessibility ensured

    - #### public CachedClass getDeclaringClass()

      Returns the cached class that declares this method.

      Returns:
        the declaring cached class

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

      Returns:
        the method descriptor string

    - #### public int getModifiers()

      Returns the modifiers of this method (e.g., public, static, synchronized). See Modifier for modifier constants.

      Returns:
        the method modifiers

    - #### public String getName()

      Returns the name of this method.

      Returns:
        the method name

    - #### public Class[] getPT()

      Returns the native Java parameter types of this method.

      Returns:
        an array of parameter classes

    - #### public ParameterTypes getParamTypes()

      Returns the parameter type information for this method.

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

      Returns:
        `null`

    - #### public int getParamsCount()

      Returns the number of parameters this method accepts.

      Returns:
        the parameter count

    - #### public Class getReturnType()

      Returns the return type of this method.

      Returns:
        the method's return class

    - #### public String getSignature()

      Returns the complete method signature (name + bytecode descriptor). For example: "toString()Ljava/lang/String;"

      Returns:
        the method signature

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

    - #### public int hashCode()

      Returns the hash code for this cached method based on the underlying Java method.

      Returns:
        the hash code

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

      throws:
        InvokerInvocationException if the method raises a checked exception or illegal access occurs
      throws:
        RuntimeException if the method raises an uncaught exception (except MissingMethodException)
      throws:
        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 so the wrapped `IllegalArgumentException` is preserved.
      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

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

    - #### public boolean isSynthetic()

      Returns whether this method is synthetic (generated by the compiler).

      Returns:
        `true` if this method is synthetic; `false` otherwise

    - #### public final Method setAccessible()

      Makes this method accessible and returns the underlying Java method. Synonym for getCachedMethod().

      Returns:
        the cached method with accessibility ensured

    - #### public void setTransformedMethod(CachedMethod transformedMethod)

      Sets the transformed version of this method.

      Parameters:
        `transformedMethod` - the transformed method, or `null` to clear

    - #### public String toString()

      Returns the string representation of the underlying Java method.

      Returns:
        a string representation of this cached method