Class MethodId
This class is immutable. Compared to the standard reflection API, this class provides constructability, generic type reification, and a more lightweight form.
Usage
A MethodId is not coupled to a declaring class; in fact, it does not verify the existence of a real method
with its details. This means that callers can construct arbitrary instances.
This comes with the drawback that a MethodId is not itself invocable. Instead, the MethodMirror is
tasked with calling methods on it.
To assist the MethodMirror implementation, it can store an opaque object (invisible to callers) to attach
cache information. This cache information is returned by getOpaqueCache(). Importantly, the method mirror
can depend on the preservation of this information, since external callers are forbidden from using method ID
instances except those produced by the mirror itself.
Equality
A method ID is equal to another based on its name, return type, parameters, and default status. If callers want to ignore the return type or default status, they must implement that logic themselves.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA marker interface for cache data appended to aMethodId. -
Constructor Summary
ConstructorsConstructorDescriptionMethodId(@NonNull String name, @NonNull ReifiedType.Annotated returnType, @NonNull ReifiedType @NonNull [] parameters, boolean isDefault) Builds from arguments -
Method Summary
Modifier and TypeMethodDescriptionboolean@Nullable MethodId.OpaqueCacheGets the opaque cache attached to this method ID, if one exists.inthashCode()booleanGets whether the method is implemented by default@NonNull Stringname()The method name@NonNull ReifiedTypeparameterAt(int index) Gets the parameter at a certain indexintThe parameter count@NonNull ReifiedType @NonNull []Gets all the parameters@NonNull ReifiedType.AnnotatedThe return typetoString()@NonNull MethodIdwithOpaqueCache(@Nullable MethodId.OpaqueCache opaqueCache) Creates a new method ID, with the argument set as the opaque cache.
-
Constructor Details
-
MethodId
public MethodId(@NonNull String name, @NonNull ReifiedType.Annotated returnType, @NonNull ReifiedType @NonNull [] parameters, boolean isDefault) Builds from arguments- Parameters:
name- the method namereturnType- its return typeparameters- the method parameters, excluding the receiver typeisDefault- whether the method is implemented by default
-
-
Method Details
-
withOpaqueCache
Creates a new method ID, with the argument set as the opaque cache.Because the opaque cache is not included in equality considerations, the returned object will always be equal to this one (according to
equals) even if not referentially equal.- Parameters:
opaqueCache- the opaque cache, ornullfor none- Returns:
- a new method ID with the cache attached
-
getOpaqueCache
Gets the opaque cache attached to this method ID, if one exists.Most library users will have no need of calling this function. It is intended to help
MethodMirrorimplementations store data inside aMethodId, to speed up calling actual methods.- Returns:
- the opaque cache, or null
-
name
The method name- Returns:
- the method name
-
returnType
The return type- Returns:
- the return type
-
parameterAt
Gets the parameter at a certain index- Parameters:
index- the index- Returns:
- the parameter at it
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
parameterCount
public int parameterCount()The parameter count- Returns:
- the parameter count
-
parameters
Gets all the parameters- Returns:
- a copy of the parameters
-
isDefault
public boolean isDefault()Gets whether the method is implemented by default- Returns:
- true if a default implementation exists
-
equals
-
hashCode
public int hashCode() -
toString
-