Class MethodUtils
- java.lang.Object
-
- io.microsphere.util.BaseUtils
-
- io.microsphere.reflect.MethodUtils
-
-
Field Summary
Fields Modifier and Type Field Description 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>
PULIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isPublic(Member)
-
Constructor Summary
Constructors Constructor Description MethodUtils()
-
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)
Create an instance ofPredicate
forMethod
to exclude the specified declared classstatic java.util.List<java.lang.reflect.Method>
filterMethods(java.lang.Class<?> targetClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Filter allmethods
of the target class by the specifiedPredicate
static java.lang.reflect.Method
findDeclaredMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Find the declaredMethod
by the specified type (including inherited types) and method name and parameter typesstatic 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) and method name and parameter types with cachestatic java.lang.reflect.Method
findNearestOverriddenMethod(java.lang.reflect.Method overrider)
Find the nearest overriddenmethod
from the inherited classstatic java.lang.reflect.Method
findOverriddenMethod(java.lang.reflect.Method overrider, java.lang.Class<?> targetClass)
Find the overriddenmethod
from the target classstatic java.util.List<java.lang.reflect.Method>
getAllDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Get all declaredmethods
of the target class, including the inherited methods.static java.util.List<java.lang.reflect.Method>
getAllMethods(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.util.List<java.lang.reflect.Method>
getDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Get all declaredmethods
of the target class, excluding the inherited methodsstatic java.util.List<java.lang.reflect.Method>
getMethods(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.String
getSignature(java.lang.reflect.Method method)
Get the signature ofthe specified method
static <R> R
invokeMethod(java.lang.Object instance, java.lang.Class<?> type, java.lang.String methodName, java.lang.Object... 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)
Invoke the target objects' methodstatic <R> R
invokeStaticMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... arguments)
Invoke the target classes' static methodstatic <R> R
invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... arguments)
Invoke the target classes' static methodstatic boolean
isCallerSensitiveMethod(java.lang.reflect.Method method)
Test whether the specifiedmethod
annotates jdk.internal.reflect.CallerSensitive or notstatic boolean
isObjectMethod(java.lang.reflect.Method method)
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)
-
PULIC_METHOD_PREDICATE
public static final java.util.function.Predicate<? super java.lang.reflect.Method> PULIC_METHOD_PREDICATE
ThePredicate
reference toMemberUtils.isPublic(Member)
-
-
Method Detail
-
excludedDeclaredClass
public static java.util.function.Predicate<? super java.lang.reflect.Method> excludedDeclaredClass(java.lang.Class<?> declaredClass)
Create an instance ofPredicate
forMethod
to exclude the specified declared class- Parameters:
declaredClass
- the target class to exclude- Returns:
- non-null
-
filterMethods
public static java.util.List<java.lang.reflect.Method> filterMethods(java.lang.Class<?> targetClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Filter allmethods
of the target class by the specifiedPredicate
- Parameters:
targetClass
- the target classincludeInheritedTypes
- include the inherited types, e,g. super classes or interfacespublicOnly
- only public methodmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
-
getDeclaredMethods
public static java.util.List<java.lang.reflect.Method> getDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Get all declaredmethods
of the target class, excluding the inherited methods- Parameters:
targetClass
- the target classmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
- See Also:
filterMethods(Class, boolean, boolean, Predicate[])
-
getMethods
public static java.util.List<java.lang.reflect.Method> getMethods(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.- Parameters:
targetClass
- the target classmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
- See Also:
filterMethods(Class, boolean, boolean, Predicate[])
-
getAllDeclaredMethods
public static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
Get all declaredmethods
of the target class, including the inherited methods.- Parameters:
targetClass
- the target classmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
- See Also:
filterMethods(Class, boolean, boolean, Predicate[])
-
getAllMethods
public static java.util.List<java.lang.reflect.Method> getAllMethods(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.- Parameters:
targetClass
- the target classmethodsToFilter
- (optional) the methods to be filtered- Returns:
- non-null read-only
List
- See Also:
filterMethods(Class, boolean, boolean, Predicate[])
-
findMethod
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.- Parameters:
targetClass
- the target typemethodName
- the specified method name- Returns:
- if not found, return
null
-
findMethod
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) and method name and parameter types with cache- Parameters:
targetClass
- the target typemethodName
- the method nameparameterTypes
- the parameter types- Returns:
- if not found, return
null
-
findDeclaredMethod
public static java.lang.reflect.Method findDeclaredMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Find the declaredMethod
by the specified type (including inherited types) and method name and parameter types- Parameters:
targetClass
- the target typemethodName
- the method nameparameterTypes
- the parameter types- Returns:
-
invokeMethod
public static <R> R invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... arguments)
Invoke the target objects' method- Type Parameters:
R
- the return type- Parameters:
object
- the target objectmethodName
- the method namearguments
- the method arguments- Returns:
- the target method's execution result
-
invokeStaticMethod
public static <R> R invokeStaticMethod(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... arguments)
Invoke the target classes' static method- Type Parameters:
R
- the return type- Parameters:
targetClass
- the target classmethodName
- the method namearguments
- the method arguments- Returns:
- the target method's execution result
-
invokeStaticMethod
public static <R> R invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... arguments)
Invoke the target classes' static method- Type Parameters:
R
- the return type- Parameters:
method
- the methodarguments
- the method arguments- Returns:
- the target method's execution result
-
invokeMethod
public static <R> R invokeMethod(java.lang.Object instance, java.lang.Class<?> type, java.lang.String methodName, java.lang.Object... arguments)
-
invokeMethod
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.
- 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.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.- Parameters:
overrider
- the first method, possible overrideroverridden
- the second method, possibly being overridden- Returns:
true
if and only if the first method overrides the second- See Also:
Elements.overrides(ExecutableElement, ExecutableElement, TypeElement)
-
findNearestOverriddenMethod
public static java.lang.reflect.Method findNearestOverriddenMethod(java.lang.reflect.Method overrider)
Find the nearest overriddenmethod
from the inherited class- Parameters:
overrider
- the overridermethod
- Returns:
- if found, the overrider
method
, ornull
-
findOverriddenMethod
public static java.lang.reflect.Method findOverriddenMethod(java.lang.reflect.Method overrider, java.lang.Class<?> targetClass)
Find the overriddenmethod
from the target class- Parameters:
overrider
- the overridermethod
targetClass
- the class that is declaring the overriddenmethod
- Returns:
- if found, the overrider
method
, ornull
-
getSignature
public static java.lang.String getSignature(java.lang.reflect.Method method)
Get the signature ofthe specified method
- Parameters:
method
-the specified method
- Returns:
- non-null
-
isObjectMethod
public static boolean isObjectMethod(java.lang.reflect.Method method)
-
isCallerSensitiveMethod
public static boolean isCallerSensitiveMethod(java.lang.reflect.Method method)
Test whether the specifiedmethod
annotates jdk.internal.reflect.CallerSensitive or not- Parameters:
method
-Method
- Returns:
true
if the specifiedmethod
annotates jdk.internal.reflect.CallerSensitive- See Also:
jdk.internal.reflect.CallerSensitive
-
-