CKTIS / Java
java.lang.reflect.Method — inspecting and invoking a method
A Method object represents a method accessed through reflection. It lets you inspect its name and annotations and invoke it at runtime. Suppressing access checks with setAccessible remains subject to module boundaries.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Method m = Type.class.getDeclaredMethod("methodName", ParameterType.class);
String name = m.getName();
boolean annotated = m.isAnnotationPresent(Marker.class);
m.setAccessible(true);
Object result = m.invoke(receiver, argument);java.lang.reflect.Method represents one method of a class or interface. Class.getDeclaredMethod finds a method declared by that type, including private methods but excluding inherited ones; Class.getMethod finds public methods, including inherited ones. Parameter types identify the overload: int.class and Integer.class are not interchangeable during lookup.
Method.getName() returns only the name, not the signature. The inherited Method.isAnnotationPresent(Marker.class) checks for an annotation of that type; the annotation must have RetentionPolicy.RUNTIME retention. Method annotations are not inherited by overriding methods, even when @Inherited is used.
Method.setAccessible(true) suppresses Java language access checks for this reflection object when permitted; it does not change the declaration to public. It does not bypass module boundaries: accessing a private method in another module requires its package to be open to the caller's module. Denial can cause InaccessibleObjectException or SecurityException. trySetAccessible() lets you receive false instead of the former exception.
Method.invoke(receiver, arguments) invokes the method; for a static method, the receiver is ignored and may be null. The result is an Object, primitive values are boxed, and void yields null. Invalid arguments cause IllegalArgumentException, denied access causes IllegalAccessException, and an exception from the target method is wrapped in InvocationTargetException.
The example uses a separate top-level class Greeter to demonstrate access to a private method from Main. Scope: Java SE 21; repository runtime, production runtime not attested.
Examples
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
class Greeter {
@Retention(RetentionPolicy.RUNTIME)
@interface Marker {}
@Marker
private String greet(String name) {
return "Hello, " + name;
}
}
public class Main {
public static void main(String[] args) throws ReflectiveOperationException {
Method m = Greeter.class.getDeclaredMethod("greet", String.class);
System.out.println(m.getName());
System.out.println(m.isAnnotationPresent(Greeter.Marker.class));
m.setAccessible(true);
System.out.println(m.invoke(new Greeter(), "Ada"));
}
}Result
greet true Hello, Ada
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.