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
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
FieldsModifier and TypeFieldDescriptionfinal CachedClassThe cached class that declares this method.static final CachedMethod[]An empty array constant representing zero cached methods. -
Constructor Summary
ConstructorsConstructorDescriptionCachedMethod(Method method) Constructs aCachedMethodfor the given Java method.CachedMethod(CachedClass clazz, Method method) Constructs aCachedMethodfor the given method within a cached class. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanAccessLegally(Class<?> callerClass) Checks whether the given caller class can legally access this method.intCompares this cached method with another method-like object for ordering.booleanChecks equality with another method.static CachedMethodFinds aCachedMethodcorresponding to the specified Java method by searching the cached methods of its declaring class.<T extends Annotation>
TgetAnnotation(Class<T> annotationClass) Returns the annotation of the specified type on this method, if present.Returns the underlying Java method, making it accessible if necessary.Returns the cached class that declares this method.Returns the bytecode method descriptor for this method (e.g., "(II)Z").intReturns the modifiers of this method (e.g., public, static, synchronized).getName()Returns the name of this method.intReturns the number of parameters this method accepts.Returns the parameter type information for this method.Class[]getPT()Returns the native Java parameter types of this method.Returns the return type of this method.Returns the complete method signature (name + bytecode descriptor).Returns the transformed version of this method, if one exists.inthashCode()Returns the hash code for this cached method based on the underlying Java method.final ObjectInvokes this method on the given object with the specified arguments.booleanIndicates 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.booleanReturns whether this method is synthetic (generated by the compiler).final MethodMakes this method accessible and returns the underlying Java method.voidsetTransformedMethod(CachedMethod transformedMethod) Sets the transformed version of this method.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, processDoMethodInvokeExceptionMethods inherited from class org.codehaus.groovy.reflection.ParameterTypes
coerceArgumentsToClasses, correctArguments, getNativeParameterTypes, getParameterTypes, isValidExactMethod, isValidExactMethod, isValidMethod, isValidMethod, isVargsMethod, isVargsMethod, setParametersTypesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface groovy.lang.MetaMember
isFinal, isPackagePrivate, isPrivate, isProtected, isPublic, isStatic
-
Field Details
-
EMPTY_ARRAY
An empty array constant representing zero cached methods. -
cachedClass
The cached class that declares this method.
-
-
Constructor Details
-
CachedMethod
Constructs aCachedMethodfor the given method within a cached class.- Parameters:
clazz- the cached class that declares this methodmethod- the Java method to cache
-
CachedMethod
Constructs aCachedMethodfor 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
Finds aCachedMethodcorresponding 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
nullif not found
-
compareTo
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:
compareToin interfaceComparable- Parameters:
other- the object to compare with (typically anotherCachedMethodorMethod)- Returns:
- a negative, zero, or positive value if this method is less than, equal to, or greater than the other
-
equals
Checks equality with another method. Two methods are equal if they represent the same underlying Java method. -
hashCode
public int hashCode()Returns the hash code for this cached method based on the underlying Java method. -
toString
Returns the string representation of the underlying Java method.- Overrides:
toStringin classMetaMethod- Returns:
- a string representation of this cached method
-
canAccessLegally
Checks whether the given caller class can legally access this method.- Parameters:
callerClass- the class attempting to access this method- Returns:
trueif access is allowed;falseotherwise
-
getAnnotation
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
nullif not present
-
getCachedMethod
Returns the underlying Java method, making it accessible if necessary.- Returns:
- the cached method with accessibility ensured
-
getDeclaringClass
Returns the cached class that declares this method.- Specified by:
getDeclaringClassin classMetaMethod- Returns:
- the declaring cached class
-
getDescriptor
Returns the bytecode method descriptor for this method (e.g., "(II)Z"). The descriptor encodes parameter and return types in JVM format.- Overrides:
getDescriptorin classMetaMethod- Returns:
- the method descriptor string
-
getModifiers
public int getModifiers()Returns the modifiers of this method (e.g., public, static, synchronized). SeeModifierfor modifier constants.- Specified by:
getModifiersin interfaceMetaMember- Specified by:
getModifiersin classMetaMethod- Returns:
- the method modifiers
-
getName
Returns the name of this method.- Specified by:
getNamein interfaceMetaMember- Specified by:
getNamein classMetaMethod- Returns:
- the method name
-
getParamsCount
public int getParamsCount()Returns the number of parameters this method accepts.- Returns:
- the parameter count
-
getParamTypes
Returns the parameter type information for this method.Note: This method always returns
nulland is retained for compatibility.- Returns:
null
-
getPT
Returns the native Java parameter types of this method.- Overrides:
getPTin classParameterTypes- Returns:
- an array of parameter classes
-
getReturnType
Returns the return type of this method.- Specified by:
getReturnTypein classMetaMethod- Returns:
- the method's return class
-
getSignature
Returns the complete method signature (name + bytecode descriptor). For example: "toString()Ljava/lang/String;"- Overrides:
getSignaturein classMetaMethod- Returns:
- the method signature
-
isSynthetic
public boolean isSynthetic()Returns whether this method is synthetic (generated by the compiler).- Specified by:
isSyntheticin interfaceMetaMember- Returns:
trueif this method is synthetic;falseotherwise
-
getTransformedMethod
Returns the transformed version of this method, if one exists. Transformed methods are compiler-optimized variants.- Returns:
- the transformed method, or
nullif no transformation exists
-
setTransformedMethod
Sets the transformed version of this method.- Parameters:
transformedMethod- the transformed method, ornullto clear
-
invoke
Invokes this method on the given object with the specified arguments. Handles accessibility, exception wrapping, and method invocation.- Specified by:
invokein classMetaMethod- Parameters:
object- the target object (may benullfor 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 occursRuntimeException- if the method raises an uncaught exception (except MissingMethodException)
-
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 thejdk.internal.reflect.CallerSensitiveannotation 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:
trueif the method is (or must be assumed) caller-sensitive- Since:
- 6.0.0
-
setAccessible
Makes this method accessible and returns the underlying Java method. Synonym forgetCachedMethod().- Returns:
- the cached method with accessibility ensured
-