Class MethodId

java.lang.Object
space.arim.dazzleconf.reflect.MethodId

public final class MethodId extends Object
Identifiers for a method.

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.

  • 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 name
      returnType - its return type
      parameters - the method parameters, excluding the receiver type
      isDefault - whether the method is implemented by default
  • Method Details

    • withOpaqueCache

      public @NonNull MethodId withOpaqueCache(@Nullable MethodId.OpaqueCache opaqueCache)
      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, or null for none
      Returns:
      a new method ID with the cache attached
    • getOpaqueCache

      @Pure public @Nullable MethodId.OpaqueCache 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 MethodMirror implementations store data inside a MethodId, to speed up calling actual methods.

      Returns:
      the opaque cache, or null
    • name

      public @NonNull String name()
      The method name
      Returns:
      the method name
    • returnType

      public @NonNull ReifiedType.Annotated returnType()
      The return type
      Returns:
      the return type
    • parameterAt

      public @NonNull ReifiedType parameterAt(int index)
      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

      public @NonNull ReifiedType @NonNull [] 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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object