Class MethodUtils
- java.lang.Object
-
- io.microsphere.reflect.MethodUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.function.Predicate<? super java.lang.reflect.Method>
FINAL_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isFinal(Member)
static java.util.function.Predicate<? super java.lang.reflect.Method>
NON_PRIVATE_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isNonPrivate(Member)
static java.util.function.Predicate<? super java.lang.reflect.Method>
NON_STATIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isNonStatic(Member)
static java.util.List<java.lang.reflect.Method>
OBJECT_DECLARED_METHODS
The declared methods ofObject
static java.util.function.Predicate<? super java.lang.reflect.Method>
OBJECT_METHOD_PREDICATE
ThePredicate
reference toisObjectMethod(Method)
static java.util.List<java.lang.reflect.Method>
OBJECT_PUBLIC_METHODS
The public methods ofObject
static java.util.function.Predicate<? super java.lang.reflect.Method>
PUBLIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isPublic(Member)
static java.util.function.Predicate<? super java.lang.reflect.Method>
STATIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isStatic(Member)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.function.Predicate<? super java.lang.reflect.Method>
excludedDeclaredClass(java.lang.Class<?> declaredClass)
Creates aPredicate
that excludes methods declared by the specified class.static java.util.List<java.lang.reflect.Method>
findAllDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Retrieves all declared methods directly defined in the specified class, excluding inherited methods.static java.util.List<java.lang.reflect.Method>
findAllMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Get all publicmethods
of the target class, including the inherited methods.static java.lang.reflect.Method
findDeclaredMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Finds a declared method in the specified class, including its superclasses and interfaces.static java.util.List<java.lang.reflect.Method>
findDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Find all declaredmethods
of the target class, excluding the inherited methods.static java.lang.reflect.Method
findMethod(java.lang.Class targetClass, java.lang.String methodName)
Find theMethod
by the specified type (including inherited types) and method name without the parameter type.static java.lang.reflect.Method
findMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Find theMethod
by the specified type (including inherited types), method name, and parameter types.static java.util.List<java.lang.reflect.Method>
findMethods(java.lang.Class<?> targetClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Find allmethods
of the target class by the specified criteria.static java.util.List<java.lang.reflect.Method>
findMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Find all public methods directly declared in the specified class, without including inherited methods.static java.lang.reflect.Method
findNearestOverriddenMethod(java.lang.reflect.Method overrider)
Finds the nearest overridden method in the class hierarchy for the given overriding method.static java.lang.reflect.Method
findOverriddenMethod(java.lang.reflect.Method overrider, java.lang.Class<?> targetClass)
Finds the method in the specified target class that is overridden by the given overriding method.static java.util.List<java.lang.reflect.Method>
getAllDeclaredMethods(java.lang.Class<?> targetClass)
Get all declaredmethods
of the target class, including the inherited methods.static java.util.List<java.lang.reflect.Method>
getAllMethods(java.lang.Class<?> targetClass)
Get all publicmethods
of the target class, including the inherited methods.static java.util.List<java.lang.reflect.Method>
getDeclaredMethods(java.lang.Class<?> targetClass)
Get all declaredmethods
of the target class, excluding the inherited methods.static java.util.List<java.lang.reflect.Method>
getMethods(java.lang.Class<?> targetClass)
Get all publicmethods
of the target class, excluding the inherited methods.static java.lang.String
getSignature(java.lang.reflect.Method method)
Generates a string representation of the method signature.static <R> R
invokeMethod(java.lang.Object instance, java.lang.Class<?> type, java.lang.String methodName, java.lang.Object... arguments)
Invokes a method with the specified name on the given class type, using the provided arguments.static <R> R
invokeMethod(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... arguments)
Invokes the underlying method represented by thisMethod
object, on the specified object with the specified parameters.static <R> R
invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... arguments)
Invokes a method with the specified name on the given object, using the provided arguments.static <R> R
invokeStaticMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... arguments)
Invokes a static method of the specified target class with the given method name and arguments.static <R> R
invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... arguments)
Invokes the specified static method represented by the givenMethod
object.static boolean
isCallerSensitiveMethod(java.lang.reflect.Method method)
Checks if the specified method is annotated withjdk.internal.reflect.CallerSensitive
.static boolean
isObjectMethod(java.lang.reflect.Method method)
Checks whether the given method is declared by theObject
class.static boolean
overrides(java.lang.reflect.Method overrider, java.lang.reflect.Method overridden)
Tests whether one method, as a member of a given type, overrides another method.
-
-
-
Field Detail
-
OBJECT_PUBLIC_METHODS
public static final java.util.List<java.lang.reflect.Method> OBJECT_PUBLIC_METHODS
The public methods ofObject
-
OBJECT_DECLARED_METHODS
public static final java.util.List<java.lang.reflect.Method> OBJECT_DECLARED_METHODS
The declared methods ofObject
-
OBJECT_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> OBJECT_METHOD_PREDICATE
ThePredicate
reference toisObjectMethod(Method)
-
PUBLIC_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> PUBLIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isPublic(Member)
-
STATIC_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> STATIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isStatic(Member)
-
NON_STATIC_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> NON_STATIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isNonStatic(Member)
-
FINAL_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> FINAL_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isFinal(Member)
-
NON_PRIVATE_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> NON_PRIVATE_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isNonPrivate(Member)
-
-
Method Detail
-
excludedDeclaredClass
@Nonnull public static java.util.function.Predicate<? super java.lang.reflect.Method> excludedDeclaredClass(java.lang.Class<?> declaredClass)
Creates aPredicate
that excludes methods declared by the specified class.This method is useful when filtering methods to exclude those that are declared by a specific class, for example, when searching for overridden methods in subclasses.
Example Usage
// Find all methods in MyClass excluding those declared by MySuperClass List<Method> filteredMethods = MethodUtils.findMethods(MyClass.class, MethodUtils.excludedDeclaredClass(MySuperClass.class));
- Parameters:
declaredClass
- the class whose declared methods should be excluded- Returns:
- a non-null
Predicate
that evaluates totrue
for methods not declared by the given class
-
getDeclaredMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> getDeclaredMethods(java.lang.Class<?> targetClass)
Get all declaredmethods
of the target class, excluding the inherited methods.This method retrieves only the methods that are directly declared in the specified class, without including any methods from its superclasses or interfaces.
Example Usage
// Get all declared methods in MyClass List<Method> declaredMethods = MethodUtils.getDeclaredMethods(MyClass.class);
- Parameters:
targetClass
- the target class- Returns:
- non-null read-only
List
of declared methods - See Also:
findDeclaredMethods(Class, Predicate...)
-
getMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> getMethods(java.lang.Class<?> targetClass)
Get all publicmethods
of the target class, excluding the inherited methods.This method retrieves only the public methods that are directly declared in the specified class, without including any methods from its superclasses or interfaces.
Example Usage
// Get all public methods declared in MyClass List<Method> publicMethods = MethodUtils.getMethods(MyClass.class);
- Parameters:
targetClass
- the target class- Returns:
- non-null read-only
List
- See Also:
findMethods(Class, Predicate...)
-
getAllDeclaredMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods(java.lang.Class<?> targetClass)
Get all declaredmethods
of the target class, including the inherited methods.This method retrieves all methods that are declared in the specified class and its superclasses, including those from interfaces implemented by the class and its ancestors.
Example Usage
// Get all declared methods in MyClass, including inherited ones List<Method> allDeclaredMethods = MethodUtils.getAllDeclaredMethods(MyClass.class);
- Parameters:
targetClass
- the target class- Returns:
- non-null read-only
List
- See Also:
findAllDeclaredMethods(Class, Predicate...)
-
getAllMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> getAllMethods(java.lang.Class<?> targetClass)
Get all publicmethods
of the target class, including the inherited methods.This method retrieves all public methods that are declared in the specified class and its superclasses, including those from interfaces implemented by the class and its ancestors.
Example Usage
// Get all public methods of MyClass, including inherited ones List<Method> allPublicMethods = MethodUtils.getAllMethods(MyClass.class);
Note: If you need only the methods declared directly in the class (excluding inherited ones), consider using
getMethods(Class)
instead.- Parameters:
targetClass
- the target class- Returns:
- non-null read-only
List
- See Also:
findAllMethods(Class, Predicate...)
-
findDeclaredMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> findDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Find all declaredmethods
of the target class, excluding the inherited methods.This method retrieves only the methods that are directly declared in the specified class, without including any methods from its superclasses or interfaces.
Example Usage
// Get all declared methods in MyClass List<Method> declaredMethods = MethodUtils.findDeclaredMethods(MyClass.class);
Filtering Example
// Get all non-private declared methods in MyClass List<Method> nonPrivateMethods = MethodUtils.findDeclaredMethods(MyClass.class, MethodUtils::isNonPrivate);
- Parameters:
targetClass
- the target classmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
- See Also:
findMethods(Class, boolean, boolean, Predicate[])
-
findMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Find all public methods directly declared in the specified class, without including inherited methods.This method retrieves only the public methods that are explicitly declared in the given class, excluding any methods from its superclasses or interfaces.
Example Usage
// Get all public methods declared in MyClass List<Method> publicMethods = MethodUtils.findMethods(MyClass.class);
Filtering Example
// Get all non-static public methods declared in MyClass List<Method> nonStaticPublicMethods = MethodUtils.findMethods(MyClass.class, method -> !MemberUtils.isStatic(method));
- Parameters:
targetClass
- the target class to inspectmethodsToFilter
- optional predicates used to filter the methods further- Returns:
- a non-null read-only list of public methods declared in the specified class
-
findAllDeclaredMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> findAllDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Retrieves all declared methods directly defined in the specified class, excluding inherited methods.This method returns only the methods that are explicitly declared in the given class, and does not include any methods from superclasses or interfaces.
Example Usage
// Get all declared methods in MyClass List<Method> declaredMethods = MethodUtils.findAllDeclaredMethods(MyClass.class);
Filtering Example
// Get all non-private declared methods in MyClass List<Method> nonPrivateMethods = MethodUtils.findAllDeclaredMethods(MyClass.class, method -> !MemberUtils.isPrivate(method));
- Parameters:
targetClass
- the class to retrieve declared methods frommethodsToFilter
- optional predicates to filter the methods- Returns:
- a non-null read-only list of declared methods in the specified class
-
findAllMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> findAllMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Get all publicmethods
of the target class, including the inherited methods.This method retrieves all public methods that are declared in the specified class and its superclasses, including those from interfaces implemented by the class and its ancestors.
Example Usage
// Get all public methods of MyClass, including inherited ones List<Method> allPublicMethods = MethodUtils.findAllMethods(MyClass.class);
Filtering Example
// Get all non-static public methods of MyClass, including inherited ones List<Method> nonStaticPublicMethods = MethodUtils.findAllMethods(MyClass.class, method -> !MemberUtils.isStatic(method));
- Parameters:
targetClass
- the target classmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
-
findMethods
@Nonnull public static java.util.List<java.lang.reflect.Method> findMethods(java.lang.Class<?> targetClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Find allmethods
of the target class by the specified criteria.This method provides a flexible way to retrieve methods from a class based on whether inherited methods should be included, whether only public methods should be considered, and optional filtering predicates.
Example Usage
Basic Usage
// Get all public methods of MyClass including inherited ones List<Method> methods = MethodUtils.findMethods(MyClass.class, true, true);
Filtering Example
// Get all non-static public methods of MyClass including inherited ones List<Method> nonStaticPublicMethods = MethodUtils.findMethods(MyClass.class, true, true, method -> !MemberUtils.isStatic(method));
Advanced Filtering Example
// Get all non-private, non-static methods of MyClass including inherited ones List<Method> filteredMethods = MethodUtils.findMethods(MyClass.class, true, false, MethodUtils::isNonPrivate, MemberUtils::isNonStatic);
- Parameters:
targetClass
- the target classincludeInheritedTypes
- if set to true, includes methods from superclasses and interfacespublicOnly
- if set to true, only public methods are returnedmethodsToFilter
- (optional) one or more predicates to further filter the methods- Returns:
- a non-null read-only list of methods matching the criteria
-
findMethod
@Nullable public static java.lang.reflect.Method findMethod(java.lang.Class targetClass, java.lang.String methodName)
Find theMethod
by the specified type (including inherited types) and method name without the parameter type.This method searches for a method with the given name in the specified class and its superclasses, returning the first match found. If no method is found, this method returns
null
.Example Usage
// Find a method named "toString" in the MyClass class Method method = MethodUtils.findMethod(MyClass.class, "toString"); if (method != null) { System.out.println("Method found: " + method); } else { System.out.println("Method not found."); }
- Parameters:
targetClass
- the target type to search for the methodmethodName
- the name of the method to find- Returns:
- the found method, or
null
if no matching method is found
-
findMethod
@Nullable public static java.lang.reflect.Method findMethod(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Find theMethod
by the specified type (including inherited types), method name, and parameter types.This method searches for a method with the given name and parameter types in the specified class and its superclasses, returning the first match found. The search is cached to improve performance on repeated calls. If no matching method is found, this method returns
null
.Example Usage
// Find a method named "toString" with no parameters in MyClass Method method = MethodUtils.findMethod(MyClass.class, "toString"); if (method != null) { System.out.println("Method found: " + method); } else { System.out.println("Method not found."); }
// Find a method named "setValue" that takes a String parameter Method method = MethodUtils.findMethod(MyClass.class, "setValue", String.class); if (method != null) { System.out.println("Method found: " + method); }
- Parameters:
targetClass
- the target class to search for the methodmethodName
- the name of the method to findparameterTypes
- the parameter types of the method (optional, defaults to empty array)- Returns:
- the found method, or
null
if no matching method is found
-
findDeclaredMethod
@Nullable public static java.lang.reflect.Method findDeclaredMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Finds a declared method in the specified class, including its superclasses and interfaces.This method searches for a method with the given name and parameter types in the specified class, its superclasses (if the class is not an interface), and its implemented interfaces. It returns the first matching method found.
Example Usage
// Find a method named "exampleMethod" with no parameters Method method1 = MethodUtils.findDeclaredMethod(MyClass.class, "exampleMethod");
// Find a method named "exampleMethod" that takes a String and an int Method method2 = MethodUtils.findDeclaredMethod(MyClass.class, "exampleMethod", String.class, int.class);
- Parameters:
targetClass
- the class to search for the declared methodmethodName
- the name of the method to findparameterTypes
- the parameter types of the method (optional, defaults to empty array)- Returns:
- the found method, or
null
if no matching method is found
-
invokeMethod
@Nullable public static <R> R invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... arguments)
Invokes a method with the specified name on the given object, using the provided arguments.This method dynamically retrieves the class of the target object and searches for the appropriate method to invoke based on the method name and argument types. It supports both instance and static methods.
Example Usage
// Example class with an instance method public class MyClass { public String greet(String name) { return "Hello, " + name; } } // Create an instance of MyClass MyClass myInstance = new MyClass(); // Call the 'greet' method using invokeMethod String result = MethodUtils.invokeMethod(myInstance, "greet", "World"); System.out.println(result); // Output: Hello, World
Note: This method internally uses reflection to find and invoke the matching method, which may throw exceptions if the method cannot be found or invoked properly.
- Type Parameters:
R
- The expected return type of the method.- Parameters:
object
- The object on which the method will be invoked. Must not be null.methodName
- The name of the method to invoke. Must not be null or empty.arguments
- The arguments to pass to the method. Can be null or empty if the method requires no parameters.- Returns:
- The result of invoking the method, wrapped in the appropriate type.
- Throws:
java.lang.NullPointerException
- If the provided object is null.java.lang.IllegalStateException
- If the method cannot be found or accessed.java.lang.IllegalArgumentException
- If the arguments do not match the method's parameter types.java.lang.RuntimeException
- If the underlying method throws an exception during invocation.
-
invokeStaticMethod
@Nullable public static <R> R invokeStaticMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... arguments)
Invokes a static method of the specified target class with the given method name and arguments.This utility method simplifies the process of invoking a static method using reflection by internally calling
invokeMethod(Object, Class, String, Object...)
with a null instance to indicate that the method is static.Example Usage
public class ExampleClass { public static int add(int a, int b) { return a + b; } } // Invoke the static method "add" Integer result = MethodUtils.invokeStaticMethod(ExampleClass.class, "add", 2, 3); System.out.println(result); // Output: 5
- Type Parameters:
R
- the expected return type of the method- Parameters:
targetClass
- the class containing the static methodmethodName
- the name of the static method to invokearguments
- the arguments to pass to the method (can be null or empty)- Returns:
- the result of the method invocation, wrapped in the appropriate type
- Throws:
java.lang.NullPointerException
- if the provided target class or method name is nulljava.lang.IllegalStateException
- if the method cannot be found or accessedjava.lang.IllegalArgumentException
- if the arguments do not match the method's parameter typesjava.lang.RuntimeException
- if the underlying method throws an exception during invocation
-
invokeStaticMethod
@Nullable public static <R> R invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... arguments)
Invokes the specified static method represented by the givenMethod
object.This method is specifically designed to invoke static methods. If the provided method is not static, it may result in an exception during invocation.
Example Usage
public class ExampleClass { public static int multiply(int a, int b) { return a * b; } } // Retrieve the method using reflection Method method = ExampleClass.class.getMethod("multiply", int.class, int.class); // Invoke the static method Integer result = MethodUtils.invokeStaticMethod(method, 5, 3); System.out.println(result); // Output: 15
- Type Parameters:
R
- the expected return type of the method- Parameters:
method
- theMethod
object representing the static method to be invokedarguments
- the arguments to pass to the method (can be null or empty)- Returns:
- the result of the method invocation, wrapped in the appropriate type
- Throws:
java.lang.NullPointerException
- if the provided method is nulljava.lang.IllegalStateException
- if the method cannot be accessed or throws an exception during invocationjava.lang.IllegalArgumentException
- if the arguments do not match the method's parameter types
-
invokeMethod
@Nullable public static <R> R invokeMethod(java.lang.Object instance, java.lang.Class<?> type, java.lang.String methodName, java.lang.Object... arguments)
Invokes a method with the specified name on the given class type, using the provided arguments.This method dynamically searches for a method in the specified class that matches the method name and argument types, and then invokes it. It supports both instance and static methods.
Example Usage
public class ExampleClass { public String greet(String name) { return "Hello, " + name; } } // Create an instance of ExampleClass ExampleClass exampleInstance = new ExampleClass(); // Call the 'greet' method using invokeMethod String result = MethodUtils.invokeMethod(exampleInstance, ExampleClass.class, "greet", "World"); System.out.println(result); // Output: Hello, World
Note: This method internally uses reflection to find and invoke the matching method, which may throw exceptions if the method cannot be found or invoked properly.
- Type Parameters:
R
- The expected return type of the method.- Parameters:
instance
- The object on which the method will be invoked. Can be null for static methods.type
- The class type to search for the method. Must not be null.methodName
- The name of the method to invoke. Must not be null or empty.arguments
- The arguments to pass to the method. Can be null or empty if the method requires no parameters.- Returns:
- The result of invoking the method, wrapped in the appropriate type.
- Throws:
java.lang.NullPointerException
- If the provided type or method name is null.java.lang.IllegalStateException
- If the method cannot be found or accessed.java.lang.IllegalArgumentException
- If the arguments do not match the method's parameter types.java.lang.RuntimeException
- If the underlying method throws an exception during invocation.
-
invokeMethod
@Nullable public static <R> R invokeMethod(@Nullable java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... arguments)
Invokes the underlying method represented by thisMethod
object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.If the underlying method is static, then the specified
instance
argument is ignored. It may be null.If the number of formal parameters required by the underlying method is 0, the supplied
args
array may be of length 0 or null.If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, section ; in particular, overriding based on the runtime type of the target object may occur.
If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.
If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. However, if the value has the type of an array of a primitive type, the elements of the array are not wrapped in objects; in other words, an array of primitive type is returned. If the underlying method return type is void, the invocation returns null.
Example Usage
public class ExampleClass { public String greet(String name) { return "Hello, " + name; } public static int add(int a, int b) { return a + b; } } // Instance method example ExampleClass instance = new ExampleClass(); String result = MethodUtils.invokeMethod(instance, ExampleClass.class.getMethod("greet", String.class), "World"); System.out.println(result); // Output: Hello, World // Static method example Integer sum = MethodUtils.invokeMethod(null, ExampleClass.class.getMethod("add", int.class, int.class), 2, 3); System.out.println(sum); // Output: 5
- Type Parameters:
R
-- Parameters:
instance
- the object the underlying method is invoked frommethod
- the underlying methodarguments
- the arguments used for the method call- Returns:
- the result of dispatching the method represented by
this object on
instance
with parametersarguments
- Throws:
java.lang.NullPointerException
- if thisMethod
object isnull
java.lang.IllegalStateException
- if thisMethod
object is enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException
- if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (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 method invocation conversion.java.lang.RuntimeException
- if the underlying method throws an exception.
-
overrides
public static boolean overrides(java.lang.reflect.Method overrider, java.lang.reflect.Method overridden)
Tests whether one method, as a member of a given type, overrides another method.This method checks if the first method (
overrider
) overrides the second method (overridden
). For a method to override another method, it must meet several conditions based on the Java Language Specification, such as being declared in a subclass of the declaring class of the overridden method, having the same name and signature, not being private or static, and having a return type that is a subtype of the overridden method's return type.Example Usage
{@code class Parent { public void sayHello() { System.out.println("Hello from Parent"); } } class Child extends Parent {
- Parameters:
overrider
- the method that may override the other methodoverridden
- the method that may be overridden- Returns:
true
if and only if the first method overrides the second
-
findNearestOverriddenMethod
@Nullable public static java.lang.reflect.Method findNearestOverriddenMethod(java.lang.reflect.Method overrider)
Finds the nearest overridden method in the class hierarchy for the given overriding method.This method searches through the inheritance chain of the class that declares the provided overriding method to locate the first method it overrides. The search includes both superclasses and interfaces.
Example Usage
{@code public class Parent { public void exampleMethod() {} } public class Child extends Parent {
- Parameters:
overrider
- the method that potentially overrides another method- Returns:
- the overridden method if found; otherwise,
null
-
findOverriddenMethod
@Nullable public static java.lang.reflect.Method findOverriddenMethod(java.lang.reflect.Method overrider, java.lang.Class<?> targetClass)
Finds the method in the specified target class that is overridden by the given overriding method.This method searches for a method in the provided
targetClass
with the same name and signature as theoverrider
method, and checks whether it is actually overridden by the provided method. If a matching overridden method is found, it is returned; otherwise, this method returnsnull
.Example Usage
{@code public class Parent { public void exampleMethod(String arg) { System.out.println("Parent method"); } } public class Child extends Parent {
- Parameters:
overrider
- the method that potentially overrides another methodtargetClass
- the class where the overridden method might be declared- Returns:
- the overridden method declared in the target class, or
null
if none is found
-
getSignature
@Nonnull public static java.lang.String getSignature(java.lang.reflect.Method method)
Generates a string representation of the method signature.The signature includes the fully qualified name of the declaring class, the method name, and the parameter types in parentheses.
Example Usage
Method method = String.class.getMethod("substring", int.class, int.class); String signature = MethodUtils.getSignature(method); System.out.println(signature); // Output: java.lang.String#substring(int,int)
- Parameters:
method
- The method for which to generate the signature.- Returns:
- A non-null string representing the method signature.
-
isObjectMethod
public static boolean isObjectMethod(java.lang.reflect.Method method)
Checks whether the given method is declared by theObject
class.This utility method helps determine if a method belongs directly to the root class
Object
, which is useful when filtering out methods that are common to all Java objects and not specific to a subclass.Example Usage
Method toStringMethod = String.class.getMethod("toString"); boolean isObjectMethod = MethodUtils.isObjectMethod(toStringMethod); System.out.println(isObjectMethod); // Output: true
Method customMethod = MyClass.class.getMethod("customMethod"); boolean isObjectMethod = MethodUtils.isObjectMethod(customMethod); System.out.println(isObjectMethod); // Output: false (assuming customMethod is defined in MyClass)
- Parameters:
method
- the method to check, may be null- Returns:
- true if the method is declared by the
Object
class; false otherwise or if the method is null
-
isCallerSensitiveMethod
public static boolean isCallerSensitiveMethod(java.lang.reflect.Method method)
Checks if the specified method is annotated withjdk.internal.reflect.CallerSensitive
.The
CallerSensitive
annotation indicates that the method's behavior may be influenced by the caller's context. This is typically used in internal Java APIs to restrict or alter behavior based on the calling class.Example Usage
Method defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); boolean isCallerSensitive = MethodUtils.isCallerSensitiveMethod(defineClassMethod); System.out.println("Is defineClass method caller-sensitive? " + isCallerSensitive); // Likely output: true
- Parameters:
method
- the method to check, may be null- Returns:
true
if the method is non-null and annotated withjdk.internal.reflect.CallerSensitive
; false otherwise- See Also:
jdk.internal.reflect.CallerSensitive
-
-