Class ExecutableUtils
- java.lang.Object
-
- io.microsphere.reflect.ExecutableUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E extends java.lang.reflect.Executable & java.lang.reflect.Member>
voidexecute(E object, ThrowableConsumer<E> callback)
Executes the givenExecutable
object using the provided callback.static <E extends java.lang.reflect.Executable & java.lang.reflect.Member,R>
Rexecute(E executableMember, ThrowableFunction<E,R> callback)
Executes the providedExecutable
using the given callback function.static <E extends java.lang.reflect.Executable & java.lang.reflect.Member,R>
Rexecute(E executable, ThrowableSupplier<R> supplier)
Executes the givenExecutable
object using aThrowableSupplier
.
-
-
-
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 givenExecutable
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
, orIllegalArgumentException
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 ofExecutable
.- Parameters:
object
- The executable member to be executed, such as aMethod
,Constructor
, orField
.callback
- The callback that defines the operation to perform on the executable.- Throws:
java.lang.NullPointerException
- IfexecutableMember
isnull
java.lang.IllegalStateException
- if thisexecutableMember
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 givenExecutable
object using aThrowableSupplier
.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 ofExecutable
.R
- The type of the result expected from the supplier.- Parameters:
executable
- The executable member to be executed, such as aMethod
,Constructor
, orField
.supplier
- The supplier defining the operation to perform on the executable.- Returns:
- The result of the supplier execution.
- Throws:
java.lang.NullPointerException
- IfexecutableMember
isnull
java.lang.IllegalStateException
- if thisexecutableMember
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 providedExecutable
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 ofExecutable
.R
- The type of the result of the callback function.- Parameters:
executableMember
- The executable member to be executed, such as aMethod
,Constructor
, orField
.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
- IfexecutableMember
isnull
java.lang.IllegalStateException
- if thisexecutableMember
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.
-
-