Java SE API
MethodHandles.Lookup.findVirtual()
Resolve a virtual method handle with access checking. See official API for exceptions and overloads; Java 21-compatible example.
Formal Syntax & Structure
java syntaxjava
MethodHandle findVirtual(Class<?> refc, String name, MethodType type)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| refc | Class<?> | Reference class |
| name | String | Method name |
| type | MethodType | Declared method type, without receiver |
Return Value
MethodHandle
Resolve a virtual method handle with access checking.
Working Example
Example Snippetjava
import java.lang.annotation.*;import java.lang.reflect.*;import java.lang.invoke.*;import java.util.*;import java.nio.file.*;import java.nio.charset.StandardCharsets;import java.net.URLClassLoader;import java.net.URL;public class Main { public static class Parent { public String name() { return "report"; } } public static class Report extends Parent { public int pages = 3; private int secret = 7; public String format(int count) { return "pages=" + count; } public void reject() { throw new IllegalStateException("invalid"); } public List<String> tags = List.of("daily"); } public static void main(String[] args) throws Throwable { MethodHandle handle = MethodHandles.publicLookup().findVirtual(Report.class, "format", MethodType.methodType(String.class, int.class)); String text = (String) handle.invokeExact(new Report(), 4); System.out.println(text); }}