Class ExecutableUtils

  • All Implemented Interfaces:
    Utils

    public abstract class ExecutableUtils
    extends java.lang.Object
    implements Utils
    The utility class for Java Reflection Executable
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Executable
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E extends java.lang.reflect.Executable & java.lang.reflect.Member>
      void
      execute​(E object, ThrowableConsumer<E> callback)
      Executes the given Executable object using the provided callback.
      static <E extends java.lang.reflect.Executable & java.lang.reflect.Member,​R>
      R
      execute​(E executableMember, ThrowableFunction<E,​R> callback)
      Executes the provided Executable using the given callback function.
      static <E extends java.lang.reflect.Executable & java.lang.reflect.Member,​R>
      R
      execute​(E executable, ThrowableSupplier<R> supplier)
      Executes the given Executable object using a ThrowableSupplier.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • execute

        public static <E extends java.lang.reflect.Executable & java.lang.reflect.Member> void execute​(E object,
                                                                                                       ThrowableConsumer<E> callback)
                                                                                                throws java.lang.NullPointerException,
                                                                                                       java.lang.IllegalStateException,
                                                                                                       java.lang.IllegalArgumentException,
                                                                                                       java.lang.RuntimeException
        Executes the given Executable object using the provided callback.

        This method is typically used when no return value is expected from the execution, and any exception thrown during execution will be wrapped and rethrown as a RuntimeException, IllegalStateException, or IllegalArgumentException depending on the cause.

        Example Usage

        
         Method method = MyClass.class.getMethod("myMethod");
         ExecutableUtils.execute(method, executable -> {
             // Perform some operation with the executable
             executable.invoke(instance);
         });
         

        If an exception occurs during execution:

        
         try {
             ExecutableUtils.execute(method, executable -> {
                 executable.invoke(instance);
             });
         } catch (RuntimeException e) {
             System.err.println("Execution failed: " + e.getMessage());
         }
         
        Type Parameters:
        E - The type or subtype of Executable.
        Parameters:
        object - The executable member to be executed, such as a Method, Constructor, or Field.
        callback - The callback that defines the operation to perform on the executable.
        Throws:
        java.lang.NullPointerException - If executableMember is null
        java.lang.IllegalStateException - if this executableMember object is enforcing Java language access control and the underlying executable member is inaccessible.
        java.lang.IllegalArgumentException - if the executable member is an instance executable member and the specified object argument is not an instance of the class or interface declaring the underlying executable member (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 executable member invocation conversion.
        java.lang.RuntimeException - if the underlying executable member throws an exception.
      • execute

        public static <E extends java.lang.reflect.Executable & java.lang.reflect.Member,​R> R execute​(E executable,
                                                                                                            ThrowableSupplier<R> supplier)
                                                                                                     throws java.lang.NullPointerException,
                                                                                                            java.lang.IllegalStateException,
                                                                                                            java.lang.IllegalArgumentException,
                                                                                                            java.lang.RuntimeException
        Executes the given Executable object using a ThrowableSupplier.

        This method is useful when you want to execute an operation that may throw a checked exception, and you need to handle it in a clean and concise way. The supplier's execution result will be returned if successful, or an appropriate runtime exception will be thrown if an error occurs.

        Example Usage

        
         Method method = MyClass.class.getMethod("myMethod");
         String result = ExecutableUtils.execute(method, () -> {
             return (String) method.invoke(instance);
         });
         

        If an exception occurs during execution:

        
         try {
             String result = ExecutableUtils.execute(method, () -> {
                 return (String) method.invoke(instance);
             });
         } catch (RuntimeException e) {
             System.err.println("Execution failed: " + e.getMessage());
         }
         
        Type Parameters:
        E - The type or subtype of Executable.
        R - The type of the result expected from the supplier.
        Parameters:
        executable - The executable member to be executed, such as a Method, Constructor, or Field.
        supplier - The supplier defining the operation to perform on the executable.
        Returns:
        The result of the supplier execution.
        Throws:
        java.lang.NullPointerException - If executableMember is null
        java.lang.IllegalStateException - if this executableMember object is enforcing Java language access control and the underlying executable member is inaccessible.
        java.lang.IllegalArgumentException - if the executable member is an instance executable member and the specified object argument is not an instance of the class or interface declaring the underlying executable member (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 executable member invocation conversion.
        java.lang.RuntimeException - if the underlying executable member throws an exception.
      • execute

        public static <E extends java.lang.reflect.Executable & java.lang.reflect.Member,​R> R execute​(E executableMember,
                                                                                                            ThrowableFunction<E,​R> callback)
                                                                                                     throws java.lang.NullPointerException,
                                                                                                            java.lang.IllegalStateException,
                                                                                                            java.lang.IllegalArgumentException,
                                                                                                            java.lang.RuntimeException
        Executes the provided Executable using the given callback function.

        This method handles common reflection-related exceptions and wraps them into appropriate runtime exceptions. It is suitable for executing operations that return a result, allowing custom handling of the executable member.

        Example Usage

        
         Method method = MyClass.class.getMethod("myMethod", String.class);
         String result = ExecutableUtils.execute(method, m -> {
             return (String) m.invoke(instance, "Hello");
         });
         

        If an exception occurs during execution:

        
         try {
             Method method = MyClass.class.getMethod("myMethod", String.class);
             String result = ExecutableUtils.execute(method, m -> {
                 return (String) m.invoke(instance, "Hello");
             });
         } catch (IllegalStateException e) {
             System.err.println("Member is inaccessible: " + e.getMessage());
         } catch (IllegalArgumentException e) {
             System.err.println("Argument mismatch: " + e.getMessage());
         } catch (RuntimeException e) {
             System.err.println("Execution failed: " + e.getMessage());
         }
         
        Type Parameters:
        E - The type or subtype of Executable.
        R - The type of the result of the callback function.
        Parameters:
        executableMember - The executable member to be executed, such as a Method, Constructor, or Field.
        callback - The function defining the operation to perform on the executable member, returning a result.
        Returns:
        The result of the callback execution.
        Throws:
        java.lang.NullPointerException - If executableMember is null
        java.lang.IllegalStateException - if this executableMember object is enforcing Java language access control and the underlying executable member is inaccessible.
        java.lang.IllegalArgumentException - if the executable member is an instance executable member and the specified object argument is not an instance of the class or interface declaring the underlying executable member (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 executable member invocation conversion.
        java.lang.RuntimeException - if the underlying executable member throws an exception.