Class MethodDefinition

  • All Implemented Interfaces:
    java.io.Serializable

    @Immutable
    public final class MethodDefinition
    extends ExecutableDefinition<java.lang.reflect.Method>
    The definition class of Java Method, providing a structured way to define and resolve methods by their name, parameter types, and declaring class. This class serves as a convenient wrapper for method metadata and resolution logic.

    Example Usage

    
     // Define a method without deprecation information
     MethodDefinition methodDef = new MethodDefinition("1.0.0", "com.example.MyClass", "myMethod", "java.lang.String", "int");
    
     // Define a deprecated method
     Deprecation deprecation = new Deprecation("2.0.0", "Use newMethod instead.");
     MethodDefinition deprecatedMethodDef = new MethodDefinition("1.0.0", deprecation, "com.example.MyClass", "oldMethod", "java.util.List");
    
     // Resolve and invoke the method
     Method method = methodDef.getMethod(); // May return null if not found
     String result = methodDef.invoke(instance, "example", 42); // Invokes myMethod with String and int parameters
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Method, Version, Serialized Form
    • Constructor Detail

      • MethodDefinition

        public MethodDefinition​(java.lang.String since,
                                java.lang.String declaredClassName,
                                java.lang.String methodName,
                                java.lang.String... parameterClassNames)
        Parameters:
        since - the 'since' version
        declaredClassName - The declared class name of the method
        methodName - the method name
        parameterClassNames - the parameter types
      • MethodDefinition

        public MethodDefinition​(java.lang.String since,
                                Deprecation deprecation,
                                java.lang.String declaredClassName,
                                java.lang.String methodName,
                                java.lang.String... parameterClassNames)
        Parameters:
        since - the 'since' version
        deprecation - the deprecation
        declaredClassName - The declared class name of the method
        methodName - the method name
        parameterClassNames - the parameter class names
      • MethodDefinition

        public MethodDefinition​(Version since,
                                java.lang.String declaredClassName,
                                java.lang.String methodName,
                                java.lang.String... parameterClassNames)
        Parameters:
        since - the 'since' version
        declaredClassName - The declared class name of the method
        methodName - the method name
        parameterClassNames - the parameter types
      • MethodDefinition

        public MethodDefinition​(Version since,
                                Deprecation deprecation,
                                java.lang.String declaredClassName,
                                java.lang.String methodName,
                                java.lang.String... parameterClassNames)
        Parameters:
        since - the 'since' version
        deprecation - the deprecation
        declaredClassName - The declared class name of the method
        methodName - the method name
        parameterClassNames - the parameter class names
    • Method Detail

      • getMethodName

        @Nonnull
        public java.lang.String getMethodName()
        The method name
        Returns:
        non-null
      • getMethod

        @Nullable
        public java.lang.reflect.Method getMethod()
        The resolved method
        Returns:
        null if not resolved
      • resolveMember

        protected java.lang.reflect.Method resolveMember()
        Description copied from class: MemberDefinition
        Resolve the member instance
        Specified by:
        resolveMember in class MemberDefinition<java.lang.reflect.Method>
        Returns:
        null if can't be resolved
      • invoke

        public <R> R invoke​(java.lang.Object instance,
                            java.lang.Object... args)
                     throws java.lang.IllegalStateException,
                            java.lang.IllegalArgumentException,
                            java.lang.RuntimeException
        Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.
        Type Parameters:
        R - the type of return value
        Parameters:
        instance - the instance for method invocation
        args - the arguments for method invocation
        Returns:
        the return value
        Throws:
        java.lang.IllegalStateException - if this Method object is enforcing Java language access control and the underlying method is inaccessible.
        java.lang.IllegalArgumentException - if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.
        java.lang.RuntimeException - if the underlying method throws an exception.