Skip to content

io microsphere reflect ExecutableUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

ExecutableUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.reflect | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/reflect/ExecutableUtils.java

Overview

The utility class for Java Reflection Executable

Declaration

public abstract class ExecutableUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

execute

Method method = MyClass.class.getMethod("myMethod");
ExecutableUtils.execute(method, executable -> {
    // Perform some operation with the executable
    executable.invoke(instance);
});
try {
    ExecutableUtils.execute(method, executable -> {
        executable.invoke(instance);
    });
} catch (RuntimeException e) {
    System.err.println("Execution failed: " + e.getMessage());
}

execute

Method method = MyClass.class.getMethod("myMethod");
String result = ExecutableUtils.execute(method, () -> {
    return (String) method.invoke(instance);
});
try {
    String result = ExecutableUtils.execute(method, () -> {
        return (String) method.invoke(instance);
    });
} catch (RuntimeException e) {
    System.err.println("Execution failed: " + e.getMessage());
}

execute

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

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.reflect.ExecutableUtils;

API Reference

Public Methods

Method Description
execute Executes the given Executable object using the provided callback.
execute Executes the given Executable object using a ThrowableSupplier.
execute Executes the provided Executable using the given callback function.

Method Details

execute

public static <E extends Executable & Member> void execute(E object, ThrowableConsumer<E> callback)

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());
}
}

execute

public static <E extends Executable & Member, R> R execute(E executable, ThrowableSupplier<R> supplier)

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());
}
}

execute

public static <E extends Executable & Member, R> R execute(E executableMember, ThrowableFunction<E, R> callback)

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());
}
}

See Also

  • Executable

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally