Skip to content

io microsphere invoke MethodHandleUtils

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

MethodHandleUtils

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

Source: microsphere-java-core/src/main/java/io/microsphere/invoke/MethodHandleUtils.java

Overview

Utility class for working with MethodHandle.

This class provides helper methods to simplify the usage of the MethodHandles API, including creating lookups, finding methods, and handling invocation failures.

Example Usage

`// Example 1: Find a virtual method handle
MethodHandle mh = MethodHandleUtils.findVirtual(String.class, "length", int.class);
int length = (int) mh.invokeExact("Hello");
System.out.println(length); // Output: 5

// Example 2: Find a static method handle
MethodHandle mh = MethodHandleUtils.findStatic(Math.class, "abs", double.class);
double abs = (double) mh.invokeExact(-42.0);
System.out.println(abs); // Output: 42.0

// Example 3: Handle invokeExact failure
try {
    MethodHandle mh = MethodHandleUtils.findVirtual(Object.class, "nonExistentMethod", void.class);
    mh.invokeExact();
` catch (Throwable e) {
    MethodHandleUtils.handleInvokeExactFailure(e, mh);
}
}

Declaration

public abstract class MethodHandleUtils 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

// Example 1: Find a virtual method handle
MethodHandle mh = MethodHandleUtils.findVirtual(String.class, "length", int.class);
int length = (int) mh.invokeExact("Hello");
System.out.println(length); // Output: 5

// Example 2: Find a static method handle
MethodHandle mh = MethodHandleUtils.findStatic(Math.class, "abs", double.class);
double abs = (double) mh.invokeExact(-42.0);
System.out.println(abs); // Output: 42.0

// Example 3: Handle invokeExact failure
try {
    MethodHandle mh = MethodHandleUtils.findVirtual(Object.class, "nonExistentMethod", void.class);
    mh.invokeExact();
} catch (Throwable e) {
    MethodHandleUtils.handleInvokeExactFailure(e, mh);
}

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.invoke.MethodHandleUtils;

API Reference

Public Methods

Method Description
lookup A single-bit mask representing module access,
lookup Create an instance of Lookup by the specified lookup class
findVirtual The convenient method to find Lookup#findVirtual(Class, String, MethodType)
findStatic The convenient method to find Lookup#findStatic(Class, String, MethodType)
handleInvokeExactFailure handle the failure of MethodHandle#invokeExact(Object...)

Method Details

lookup

public static Lookup lookup(Class<?> requestedClass)

A single-bit mask representing module access, which may contribute to the result of Lookup#lookupModes lookupModes. The value is 0x10, which does not correspond meaningfully to any particular Modifier modifier bit. In conjunction with the PUBLIC modifier bit, a Lookup with this lookup mode can access all public types in the module of the lookup class and public types in packages exported by other modules to the module of the class to be looked up.

If this lookup mode is set, the Lookup#previousrequestedClass() previous lookup class is always null.

Since: 9

lookup

public static Lookup lookup(Class<?> requestedClass, LookupMode... lookupModes)

Create an instance of Lookup by the specified lookup class with #ALL_MODES all access (public, private, protected and package)

findVirtual

public static MethodHandle findVirtual(Class<?> requestedClass, String methodName, Class... parameterTypes)

The convenient method to find Lookup#findVirtual(Class, String, MethodType)

findStatic

public static MethodHandle findStatic(Class<?> requestedClass, String methodName, Class... parameterTypes)

The convenient method to find Lookup#findStatic(Class, String, MethodType)

handleInvokeExactFailure

public static void handleInvokeExactFailure(Throwable e, MethodHandle methodHandle, Object... args)

handle the failure of MethodHandle#invokeExact(Object...)

See Also

  • MethodHandles
  • MethodHandle

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