Class MethodUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.MethodUtils
-
public class MethodUtils extends java.lang.ObjectUtility reflection methods focused onMethods, originally from Commons BeanUtils. Differences from the BeanUtils version may be noted, especially where similar functionality already existed within Lang.Known Limitations
Accessing Public Methods In A Default Access Superclass
There is an issue when invoking
publicmethods contained in a default access superclass on JREs prior to 1.4. Reflection locates these methods fine and correctly assigns them aspublic. However, anIllegalAccessExceptionis thrown if the method is invoked.MethodUtilscontains a workaround for this situation. It will attempt to callAccessibleObject.setAccessible(boolean)on this method. If this call succeeds, then the method can be invoked as normal. This call will only succeed when the application has sufficient security privileges. If this call fails then the method may fail.- Since:
- 2.5
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Comparator<java.lang.reflect.Method>METHOD_BY_SIGNATURE
-
Constructor Summary
Constructors Constructor Description MethodUtils()Deprecated.TODO Make private in 4.0.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static intdistance(java.lang.Class<?>[] fromClassArray, java.lang.Class<?>[] toClassArray)Computes the aggregate number of inheritance hops between assignable argument class types.static java.lang.reflect.MethodgetAccessibleMethod(java.lang.Class<?> cls, java.lang.reflect.Method method)Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.static java.lang.reflect.MethodgetAccessibleMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets an accessible method (that is, one that can be invoked via reflection) with given name and parameters.static java.lang.reflect.MethodgetAccessibleMethod(java.lang.reflect.Method method)Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.private static java.lang.reflect.MethodgetAccessibleMethodFromInterfaceNest(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified method, by scanning through all implemented interfaces and subinterfaces.private static java.lang.reflect.MethodgetAccessibleMethodFromSuperclass(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets an accessible method (that is, one that can be invoked via reflection) by scanning through the superclasses.private static java.util.List<java.lang.Class<?>>getAllSuperclassesAndInterfaces(java.lang.Class<?> cls)Gets a combination ofClassUtils.getAllSuperclasses(Class)andClassUtils.getAllInterfaces(Class), one from superclasses, one from interfaces, and so on in a breadth first way.static <A extends java.lang.annotation.Annotation>
AgetAnnotation(java.lang.reflect.Method method, java.lang.Class<A> annotationCls, boolean searchSupers, boolean ignoreAccess)Gets the annotation object with the given annotation type that is present on the given method or optionally on any equivalent method in super classes and interfaces.private static java.lang.reflect.MethodgetInvokeMethod(boolean forceAccess, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Class<? extends java.lang.Object> cls)static java.lang.reflect.MethodgetMatchingAccessibleMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... requestTypes)Gets an accessible method that matches the given name and has compatible parameters.static java.lang.reflect.MethodgetMatchingMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets a method whether or not it's accessible.static java.lang.reflect.MethodgetMethodObject(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>... parameterTypes)Gets a Method, ornullif a documentedClass.getMethod(String, Class...)exception is thrown.static java.util.List<java.lang.reflect.Method>getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)Gets all class level public methods of the given class that are annotated with the given annotation.static java.util.List<java.lang.reflect.Method>getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)Gets all methods of the given class that are annotated with the given annotation.static java.lang.reflect.Method[]getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)Gets all class level public methods of the given class that are annotated with the given annotation.static java.lang.reflect.Method[]getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)Gets all methods of the given class that are annotated with the given annotation.static java.util.Set<java.lang.reflect.Method>getOverrideHierarchy(java.lang.reflect.Method method, ClassUtils.Interfaces interfacesBehavior)Gets the hierarchy of overridden methods down toresultrespecting generics.static java.lang.ObjectinvokeExactMethod(java.lang.Object object, java.lang.String methodName)Invokes a method whose parameter types match exactly the object type.static java.lang.ObjectinvokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... args)Invokes a method whose parameter types match exactly the object types.static java.lang.ObjectinvokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes)Invokes a method whose parameter types match exactly the parameter types given.static java.lang.ObjectinvokeExactStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object... args)Invokes astaticmethod whose parameter types match exactly the object types.static java.lang.ObjectinvokeExactStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes)Invokes astaticmethod whose parameter types match exactly the parameter types given.static java.lang.ObjectinvokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName)Invokes a named method without parameters.static java.lang.ObjectinvokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName, java.lang.Object... args)Invokes a named method whose parameter type matches the object type.static java.lang.ObjectinvokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes)Invokes a named method whose parameter type matches the object type.static java.lang.ObjectinvokeMethod(java.lang.Object object, java.lang.String methodName)Invokes a named method without parameters.static java.lang.ObjectinvokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... args)Invokes a named method whose parameter type matches the object type.static java.lang.ObjectinvokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes)Invokes a named method whose parameter type matches the object type.static java.lang.ObjectinvokeStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object... args)Invokes a namedstaticmethod whose parameter type matches the object type.static java.lang.ObjectinvokeStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes)Invokes a namedstaticmethod whose parameter type matches the object type.private static java.lang.reflect.MethodrequireNonNull(java.lang.reflect.Method method, java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>[] parameterTypes)private static java.lang.Object[]toVarArgs(java.lang.Object[] args, java.lang.Class<?>[] methodParameterTypes)Gets an array of arguments in the canonical form, given an arguments array passed to a varargs method, for example an array with the declared number of parameters, and whose last parameter is an array of the varargs type.(package private) static java.lang.Object[]toVarArgs(java.lang.reflect.Executable executable, java.lang.Object[] args)
-
-
-
Constructor Detail
-
MethodUtils
@Deprecated public MethodUtils()
Deprecated.TODO Make private in 4.0.MethodUtilsinstances should NOT be constructed in standard programming. Instead, the class should be used asMethodUtils.getAccessibleMethod(method).This constructor is
publicto permit tools that require a JavaBean instance to operate.
-
-
Method Detail
-
distance
private static int distance(java.lang.Class<?>[] fromClassArray, java.lang.Class<?>[] toClassArray)Computes the aggregate number of inheritance hops between assignable argument class types. Returns -1 if the arguments aren't assignable. Fills a specific purpose for getMatchingMethod and is not generalized.- Parameters:
fromClassArray- the Class array to calculate the distance from.toClassArray- the Class array to calculate the distance to.- Returns:
- the aggregate number of inheritance hops between assignable argument class types.
-
getAccessibleMethod
public static java.lang.reflect.Method getAccessibleMethod(java.lang.Class<?> cls, java.lang.reflect.Method method)Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. If no such method can be found, returnnull.- Parameters:
cls- The implementing class, may be null.method- The method that we wish to call, may be null.- Returns:
- The accessible method or null.
- Since:
- 3.19.0
-
getAccessibleMethod
public static java.lang.reflect.Method getAccessibleMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets an accessible method (that is, one that can be invoked via reflection) with given name and parameters. If no such method can be found, returnnull. This is just a convenience wrapper forgetAccessibleMethod(Method).- Parameters:
cls- get method from this class.methodName- get method with this name.parameterTypes- with these parameters types.- Returns:
- The accessible method.
-
getAccessibleMethod
public static java.lang.reflect.Method getAccessibleMethod(java.lang.reflect.Method method)
Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. If no such method can be found, returnnull.- Parameters:
method- The method that we wish to call, may be null.- Returns:
- The accessible method
-
getAccessibleMethodFromInterfaceNest
private static java.lang.reflect.Method getAccessibleMethodFromInterfaceNest(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified method, by scanning through all implemented interfaces and subinterfaces. If no such method can be found, returnnull.There isn't any good reason why this method must be
private. It is because there doesn't seem any reason why other classes should call this rather than the higher level methods.- Parameters:
cls- Parent class for the interfaces to be checkedmethodName- Method name of the method we wish to callparameterTypes- The parameter type signatures- Returns:
- the accessible method or
nullif not found
-
getAccessibleMethodFromSuperclass
private static java.lang.reflect.Method getAccessibleMethodFromSuperclass(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets an accessible method (that is, one that can be invoked via reflection) by scanning through the superclasses. If no such method can be found, returnnull.- Parameters:
cls- Class to be checked.methodName- Method name of the method we wish to call.parameterTypes- The parameter type signatures.- Returns:
- the accessible method or
nullif not found.
-
getAllSuperclassesAndInterfaces
private static java.util.List<java.lang.Class<?>> getAllSuperclassesAndInterfaces(java.lang.Class<?> cls)
Gets a combination ofClassUtils.getAllSuperclasses(Class)andClassUtils.getAllInterfaces(Class), one from superclasses, one from interfaces, and so on in a breadth first way.- Parameters:
cls- the class to look up, may benull- Returns:
- the combined
Listof superclasses and interfaces in order going up from this onenullif null input
-
getAnnotation
public static <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.reflect.Method method, java.lang.Class<A> annotationCls, boolean searchSupers, boolean ignoreAccess)Gets the annotation object with the given annotation type that is present on the given method or optionally on any equivalent method in super classes and interfaces. Returns null if the annotation type was not present.Stops searching for an annotation once the first annotation of the specified type has been found. Additional annotations of the specified type will be silently ignored.
- Type Parameters:
A- the annotation type- Parameters:
method- theMethodto query, may be null.annotationCls- theAnnotationto check if is present on the methodsearchSupers- determines if a lookup in the entire inheritance hierarchy of the given class is performed if the annotation was not directly presentignoreAccess- determines if underlying method has to be accessible- Returns:
- the first matching annotation, or
nullif not found - Throws:
java.lang.NullPointerException- if either the method or annotation class isnulljava.lang.SecurityException- if an underlying accessible object's method denies the request.- Since:
- 3.6
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
getInvokeMethod
private static java.lang.reflect.Method getInvokeMethod(boolean forceAccess, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Class<? extends java.lang.Object> cls)
-
getMatchingAccessibleMethod
public static java.lang.reflect.Method getMatchingAccessibleMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... requestTypes)Gets an accessible method that matches the given name and has compatible parameters. Compatible parameters mean that every method parameter is assignable from the given parameters. In other words, it finds a method with the given name that will take the parameters given.This method is used by
invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes).This method can match primitive parameter by passing in wrapper classes. For example, a
Booleanwill match a primitivebooleanparameter.- Parameters:
cls- find method in this class.methodName- find method with this name.requestTypes- find method with most compatible parameters.- Returns:
- The accessible method or null.
- Throws:
java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
getMatchingMethod
public static java.lang.reflect.Method getMatchingMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Gets a method whether or not it's accessible. If no such method can be found, returnnull.- Parameters:
cls- The class that will be subjected to the method searchmethodName- The method that we wish to callparameterTypes- Argument class types- Returns:
- The method
- Throws:
java.lang.IllegalStateException- if there is no unique resultjava.lang.NullPointerException- if the class isnull- Since:
- 3.5
-
getMethodObject
public static java.lang.reflect.Method getMethodObject(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>... parameterTypes)Gets a Method, ornullif a documentedClass.getMethod(String, Class...)exception is thrown.- Parameters:
cls- Receiver forClass.getMethod(String, Class...).name- the name of the method.parameterTypes- the list of parameters.- Returns:
- a Method or
null. - Since:
- 3.15.0
- See Also:
SecurityManager.checkPermission(java.security.Permission),Class.getMethod(String, Class...)
-
getMethodsListWithAnnotation
public static java.util.List<java.lang.reflect.Method> getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)Gets all class level public methods of the given class that are annotated with the given annotation.- Parameters:
cls- theClassto queryannotationCls- theAnnotationthat must be present on a method to be matched- Returns:
- a list of Methods (possibly empty).
- Throws:
java.lang.NullPointerException- if the class or annotation arenull- Since:
- 3.4
-
getMethodsListWithAnnotation
public static java.util.List<java.lang.reflect.Method> getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)Gets all methods of the given class that are annotated with the given annotation.- Parameters:
cls- theClassto queryannotationCls- theAnnotationthat must be present on a method to be matchedsearchSupers- determines if a lookup in the entire inheritance hierarchy of the given class should be performedignoreAccess- determines if non-public methods should be considered- Returns:
- a list of Methods (possibly empty).
- Throws:
java.lang.NullPointerException- if either the class or annotation class isnull- Since:
- 3.6
-
getMethodsWithAnnotation
public static java.lang.reflect.Method[] getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)Gets all class level public methods of the given class that are annotated with the given annotation.- Parameters:
cls- theClassto queryannotationCls- theAnnotationthat must be present on a method to be matched- Returns:
- an array of Methods (possibly empty).
- Throws:
java.lang.NullPointerException- if the class or annotation arenull- Since:
- 3.4
-
getMethodsWithAnnotation
public static java.lang.reflect.Method[] getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)Gets all methods of the given class that are annotated with the given annotation.- Parameters:
cls- theClassto queryannotationCls- theAnnotationthat must be present on a method to be matchedsearchSupers- determines if a lookup in the entire inheritance hierarchy of the given class should be performedignoreAccess- determines if non-public methods should be considered- Returns:
- an array of Methods (possibly empty).
- Throws:
java.lang.NullPointerException- if the class or annotation arenull- Since:
- 3.6
-
getOverrideHierarchy
public static java.util.Set<java.lang.reflect.Method> getOverrideHierarchy(java.lang.reflect.Method method, ClassUtils.Interfaces interfacesBehavior)Gets the hierarchy of overridden methods down toresultrespecting generics.- Parameters:
method- lowest to considerinterfacesBehavior- whether to search interfaces,nullimpliesfalse- Returns:
- a
Set<Method>in ascending order from subclass to superclass - Throws:
java.lang.NullPointerException- if the specified method isnulljava.lang.SecurityException- if an underlying accessible object's method denies the request.- Since:
- 3.2
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeExactMethod
public static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a method whose parameter types match exactly the object type.This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[]).- Parameters:
object- invoke method on this object.methodName- get method with this name.- Returns:
- The value returned by the invoked method.
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- Since:
- 3.4
-
invokeExactMethod
public static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a method whose parameter types match exactly the object types.This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[]).- Parameters:
object- invoke method on this object.methodName- get method with this name.args- use these arguments - treat null as empty array.- Returns:
- The value returned by the invoked method.
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.
-
invokeExactMethod
public static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a method whose parameter types match exactly the parameter types given.This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[]).- Parameters:
object- Invokes a method on this object.methodName- Gets a method with this name.args- Method arguments - treat null as empty array.parameterTypes- Match these parameters - treatnullas empty array.- Returns:
- The value returned by the invoked method.
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.
-
invokeExactStaticMethod
public static java.lang.Object invokeExactStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes astaticmethod whose parameter types match exactly the object types.This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[]).- Parameters:
cls- invoke static method on this classmethodName- get method with this nameargs- use these arguments - treatnullas empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.
-
invokeExactStaticMethod
public static java.lang.Object invokeExactStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes astaticmethod whose parameter types match exactly the parameter types given.This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[]).- Parameters:
cls- invoke static method on this classmethodName- get method with this nameargs- use these arguments - treatnullas empty arrayparameterTypes- match these parameters - treatnullas empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a named method without parameters.This is a convenient wrapper for
invokeMethod(Object object, boolean forceAccess, String methodName, Object[] args, Class[] parameterTypes).- Parameters:
object- invoke method on this objectforceAccess- force access to invoke method even if it's not accessiblemethodName- get method with this name- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- Since:
- 3.5
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a named method whose parameter type matches the object type.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanobject would match abooleanprimitive.This is a convenient wrapper for
invokeMethod(Object object, boolean forceAccess, String methodName, Object[] args, Class[] parameterTypes).- Parameters:
object- invoke method on this objectforceAccess- force access to invoke method even if it's not accessiblemethodName- get method with this nameargs- use these arguments - treat null as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- Since:
- 3.5
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a named method whose parameter type matches the object type.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanobject would match abooleanprimitive.- Parameters:
object- invoke method on this objectforceAccess- force access to invoke method even if it's not accessiblemethodName- get method with this nameargs- use these arguments - treat null as empty arrayparameterTypes- match these parameters - treat null as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- Since:
- 3.5
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a named method without parameters.This method delegates the method search to
getMatchingAccessibleMethod(Class, String, Class[]).This is a convenient wrapper for
invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes).- Parameters:
object- invoke method on this objectmethodName- get method with this name- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- if there is no such accessible methodjava.lang.reflect.InvocationTargetException- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException- if the requested method is not accessible via reflectionjava.lang.SecurityException- if an underlying accessible object's method denies the request.- Since:
- 3.4
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a named method whose parameter type matches the object type.This method delegates the method search to
getMatchingAccessibleMethod(Class, String, Class[]).This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanobject would match abooleanprimitive.This is a convenient wrapper for
invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes).- Parameters:
object- invoke method on this objectmethodName- get method with this nameargs- use these arguments - treat null as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- if there is no such accessible methodjava.lang.reflect.InvocationTargetException- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException- if the requested method is not accessible via reflectionjava.lang.NullPointerException- if the object or method name arenulljava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a named method whose parameter type matches the object type.This method delegates the method search to
getMatchingAccessibleMethod(Class, String, Class[]).This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanobject would match abooleanprimitive.- Parameters:
object- invoke method on this objectmethodName- get method with this nameargs- use these arguments - treat null as empty arrayparameterTypes- match these parameters - treat null as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.NullPointerException- Thrown if the specifiedobjectis null.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeStaticMethod
public static java.lang.Object invokeStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a namedstaticmethod whose parameter type matches the object type.This method delegates the method search to
getMatchingAccessibleMethod(Class, String, Class[]).This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanclass would match abooleanprimitive.This is a convenient wrapper for
invokeStaticMethod(Class, String, Object[], Class[]).- Parameters:
cls- invoke static method on this classmethodName- get method with this nameargs- use these arguments - treatnullas empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
invokeStaticMethod
public static java.lang.Object invokeStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionInvokes a namedstaticmethod whose parameter type matches the object type.This method delegates the method search to
getMatchingAccessibleMethod(Class, String, Class[]).This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanclass would match abooleanprimitive.- Parameters:
cls- invoke static method on this classmethodName- get method with this nameargs- use these arguments - treatnullas empty arrayparameterTypes- match these parameters - treatnullas empty array- Returns:
- The value returned by the invoked method
- Throws:
java.lang.NoSuchMethodException- Thrown if there is no such accessible method.java.lang.IllegalAccessException- Thrown if this foundMethodis enforcing Java language access control and the underlying method is inaccessible.java.lang.IllegalArgumentException- Thrown if:- the found
Methodis an instance method and the specifiedobjectargument is not an instance of the class or interface declaring the underlying method (or of a subclass or interface implementor); - the number of actual and formal parameters differ;
- an unwrapping conversion for primitive arguments fails; or
- after possible unwrapping, a parameter value can't be converted to the corresponding formal parameter type by a method invocation conversion.
- the found
java.lang.reflect.InvocationTargetException- Thrown if the underlying method throws an exception.java.lang.ExceptionInInitializerError- Thrown if the initialization provoked by this method fails.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
requireNonNull
private static java.lang.reflect.Method requireNonNull(java.lang.reflect.Method method, java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException- Throws:
java.lang.NoSuchMethodException
-
toVarArgs
static java.lang.Object[] toVarArgs(java.lang.reflect.Executable executable, java.lang.Object[] args) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodException- Throws:
java.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.NoSuchMethodException
-
toVarArgs
private static java.lang.Object[] toVarArgs(java.lang.Object[] args, java.lang.Class<?>[] methodParameterTypes) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodExceptionGets an array of arguments in the canonical form, given an arguments array passed to a varargs method, for example an array with the declared number of parameters, and whose last parameter is an array of the varargs type.We follow the JLS 5.1.2. Widening Primitive Conversion rules.
- Parameters:
args- the array of arguments passed to the varags method.methodParameterTypes- the declared array of method parameter types.- Returns:
- an array of the variadic arguments passed to the method.
- Throws:
java.lang.NoSuchMethodException- Thrown if the constructor could not be found.java.lang.IllegalAccessException- Thrown if thisConstructorobject is enforcing Java language access control and the underlying constructor is inaccessible.java.lang.IllegalArgumentException- Thrown 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; if this constructor pertains to an enum type.java.lang.InstantiationException- Thrown if a class that declares the underlying constructor represents an abstract class.java.lang.reflect.InvocationTargetException- Thrown if an underlying constructor throws an exception.java.lang.ExceptionInInitializerError- Thrown if an initialization provoked by this method fails.- See Also:
- JLS 5.1.2. Widening Primitive Conversion
-
-