/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.lang3.reflect; import java.lang.annotation.Annotation; import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils.Interfaces; import org.apache.commons.lang3.Validate; /** * Utility reflection methods focused on {@link Method}s, originally from Commons BeanUtils. * Differences from the BeanUtils version may be noted, especially where similar functionality * already existed within Lang. * *
There is an issue when invoking {@code public} methods contained in a default access superclass on JREs prior to 1.4. * Reflection locates these methods fine and correctly assigns them as {@code public}. * However, an {@link IllegalAccessException} is thrown if the method is invoked.
* *{@link MethodUtils} contains a workaround for this situation. * It will attempt to call {@link java.lang.reflect.AccessibleObject#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 */ public class MethodUtils { private static final ComparatorThis constructor is {@code public} to permit tools that require a JavaBean * instance to operate.
*/ public MethodUtils() { } /** * Invokes a named method without parameters. * *This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.
* *This is a convenient wrapper for * {@link #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}. *
* * @param object invoke method on this object * @param methodName get method with this name * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @since 3.4 */ public static Object invokeMethod(final Object object, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null); } /** * Invokes a named method without parameters. * *This is a convenient wrapper for * {@link #invokeMethod(Object object, boolean forceAccess, String methodName, Object[] args, Class[] parameterTypes)}. *
* * @param object invoke method on this object * @param forceAccess force access to invoke method even if it's not accessible * @param methodName get method with this name * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @since 3.5 */ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeMethod(object, forceAccess, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null); } /** * Invokes a named method whose parameter type matches the object type. * *This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.
* *This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@link Boolean} object * would match a {@code boolean} primitive.
* *This is a convenient wrapper for * {@link #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}. *
* * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection */ public static Object invokeMethod(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); return invokeMethod(object, methodName, args, ClassUtils.toClass(args)); } /** * Invokes 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 {@link Boolean} object * would match a {@code boolean} primitive.
* *This is a convenient wrapper for * {@link #invokeMethod(Object object, boolean forceAccess, String methodName, Object[] args, Class[] parameterTypes)}. *
* * @param object invoke method on this object * @param forceAccess force access to invoke method even if it's not accessible * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @since 3.5 */ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); return invokeMethod(object, forceAccess, methodName, args, ClassUtils.toClass(args)); } /** * Invokes 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 {@link Boolean} object * would match a {@code boolean} primitive.
* * @param object invoke method on this object * @param forceAccess force access to invoke method even if it's not accessible * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @param parameterTypes match these parameters - treat null as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * @since 3.5 */ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object[] args, Class>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Objects.requireNonNull(object, "object"); parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); args = ArrayUtils.nullToEmpty(args); final String messagePrefix; final Method method; final Class extends Object> cls = object.getClass(); if (forceAccess) { messagePrefix = "No such method: "; method = getMatchingMethod(cls, methodName, parameterTypes); if (method != null && !method.isAccessible()) { method.setAccessible(true); } } else { messagePrefix = "No such accessible method: "; method = getMatchingAccessibleMethod(cls, methodName, parameterTypes); } if (method == null) { throw new NoSuchMethodException(messagePrefix + methodName + "() on object: " + cls.getName()); } args = toVarArgs(method, args); return method.invoke(object, args); } /** * Invokes a named method whose parameter type matches the object type. * *This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.
* *This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@link Boolean} object * would match a {@code boolean} primitive.
* * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @param parameterTypes match these parameters - treat null as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection */ public static Object invokeMethod(final Object object, final String methodName, final Object[] args, final Class>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeMethod(object, false, methodName, args, parameterTypes); } /** * Invokes a method whose parameter types match exactly the object * types. * *This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod}(Class, String, Class[])}.
* * @param object invoke method on this object * @param methodName get method with this name * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection * * @since 3.4 */ public static Object invokeExactMethod(final Object object, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeExactMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null); } /** * Invokes a method with no parameters. * *This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod}(Class, String, Class[])}.
* * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeExactMethod(final Object object, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); return invokeExactMethod(object, methodName, args, ClassUtils.toClass(args)); } /** * Invokes a method whose parameter types match exactly the parameter * types given. * *This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.
* * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @param parameterTypes match these parameters - treat {@code null} as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeExactMethod(final Object object, final String methodName, Object[] args, Class>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Objects.requireNonNull(object, "object"); args = ArrayUtils.nullToEmpty(args); parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); final Class> cls = object.getClass(); final Method method = getAccessibleMethod(cls, methodName, parameterTypes); if (method == null) { throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + cls.getName()); } return method.invoke(object, args); } /** * Invokes a {@code static} method whose parameter types match exactly the parameter * types given. * *This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.
* * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @param parameterTypes match these parameters - treat {@code null} as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeExactStaticMethod(final Class> cls, final String methodName, Object[] args, Class>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); final Method method = getAccessibleMethod(cls, methodName, parameterTypes); if (method == null) { throw new NoSuchMethodException("No such accessible method: " + methodName + "() on class: " + cls.getName()); } return method.invoke(null, args); } /** * Invokes a named {@code static} method whose parameter type matches the object type. * *This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.
* *This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@link Boolean} class * would match a {@code boolean} primitive.
* *This is a convenient wrapper for * {@link #invokeStaticMethod(Class, String, Object[], Class[])}. *
* * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeStaticMethod(final Class> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); return invokeStaticMethod(cls, methodName, args, ClassUtils.toClass(args)); } /** * Invokes a named {@code static} method whose parameter type matches the object type. * *This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.
* *This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@link Boolean} class * would match a {@code boolean} primitive.
* * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @param parameterTypes match these parameters - treat {@code null} as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeStaticMethod(final Class> cls, final String methodName, Object[] args, Class>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); final Method method = getMatchingAccessibleMethod(cls, methodName, parameterTypes); if (method == null) { throw new NoSuchMethodException("No such accessible method: " + methodName + "() on class: " + cls.getName()); } args = toVarArgs(method, args); return method.invoke(null, args); } private static Object[] toVarArgs(final Method method, Object[] args) { if (method.isVarArgs()) { final Class>[] methodParameterTypes = method.getParameterTypes(); args = getVarArgs(args, methodParameterTypes); } return args; } /** * Given an arguments array passed to a varargs method, return an array of arguments in the canonical form, * i.e. an array with the declared number of parameters, and whose last parameter is an array of the varargs type. * * @param args the array of arguments passed to the varags method * @param methodParameterTypes the declared array of method parameter types * @return an array of the variadic arguments passed to the method * @since 3.5 */ static Object[] getVarArgs(final Object[] args, final Class>[] methodParameterTypes) { if (args.length == methodParameterTypes.length && (args[args.length - 1] == null || args[args.length - 1].getClass().equals(methodParameterTypes[methodParameterTypes.length - 1]))) { // The args array is already in the canonical form for the method. return args; } // Construct a new array matching the method's declared parameter types. final Object[] newArgs = new Object[methodParameterTypes.length]; // Copy the normal (non-varargs) parameters System.arraycopy(args, 0, newArgs, 0, methodParameterTypes.length - 1); // Construct a new array for the variadic parameters final Class> varArgComponentType = methodParameterTypes[methodParameterTypes.length - 1].getComponentType(); final int varArgLength = args.length - methodParameterTypes.length + 1; Object varArgsArray = Array.newInstance(ClassUtils.primitiveToWrapper(varArgComponentType), varArgLength); // Copy the variadic arguments into the varargs array. System.arraycopy(args, methodParameterTypes.length - 1, varArgsArray, 0, varArgLength); if (varArgComponentType.isPrimitive()) { // unbox from wrapper type to primitive type varArgsArray = ArrayUtils.toPrimitive(varArgsArray); } // Store the varargs array in the last position of the array to return newArgs[methodParameterTypes.length - 1] = varArgsArray; // Return the canonical varargs array. return newArgs; } /** * Invokes a {@code static} method whose parameter types match exactly the object * types. * *This uses reflection to invoke the method obtained from a call to * {@link #getAccessibleMethod(Class, String, Class[])}.
* * @param cls invoke static method on this class * @param methodName get method with this name * @param args use these arguments - treat {@code null} as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the * method invoked * @throws IllegalAccessException if the requested method is not accessible * via reflection */ public static Object invokeExactStaticMethod(final Class> cls, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); return invokeExactStaticMethod(cls, methodName, args, ClassUtils.toClass(args)); } /** * Returns an accessible method (that is, one that can be invoked via * reflection) with given name and parameters. If no such method * can be found, return {@code null}. * This is just a convenience wrapper for * {@link #getAccessibleMethod(Method)}. * * @param cls get method from this class * @param methodName get method with this name * @param parameterTypes with these parameters types * @return The accessible method */ public static Method getAccessibleMethod(final Class> cls, final String methodName, final Class>... parameterTypes) { try { return getAccessibleMethod(cls.getMethod(methodName, parameterTypes)); } catch (final NoSuchMethodException e) { return null; } } /** * Returns an accessible method (that is, one that can be invoked via * reflection) that implements the specified Method. If no such method * can be found, return {@code null}. * * @param method The method that we wish to call * @return The accessible method */ public static Method getAccessibleMethod(Method method) { if (!MemberUtils.isAccessible(method)) { return null; } // If the declaring class is public, we are done final Class> cls = method.getDeclaringClass(); if (ClassUtils.isPublic(cls)) { return method; } final String methodName = method.getName(); final Class>[] parameterTypes = method.getParameterTypes(); // Check the implemented interfaces and subinterfaces method = getAccessibleMethodFromInterfaceNest(cls, methodName, parameterTypes); // Check the superclass chain if (method == null) { method = getAccessibleMethodFromSuperclass(cls, methodName, parameterTypes); } return method; } /** * Returns an accessible method (that is, one that can be invoked via * reflection) by scanning through the superclasses. If no such method * can be found, return {@code null}. * * @param cls Class to be checked * @param methodName Method name of the method we wish to call * @param parameterTypes The parameter type signatures * @return the accessible method or {@code null} if not found */ private static Method getAccessibleMethodFromSuperclass(final Class> cls, final String methodName, final Class>... parameterTypes) { Class> parentClass = cls.getSuperclass(); while (parentClass != null) { if (ClassUtils.isPublic(parentClass)) { try { return parentClass.getMethod(methodName, parameterTypes); } catch (final NoSuchMethodException e) { return null; } } parentClass = parentClass.getSuperclass(); } return null; } /** * Returns 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, return {@code null}. * *There isn't any good reason why this method must be {@code private}. * It is because there doesn't seem any reason why other classes should * call this rather than the higher level methods.
* * @param cls Parent class for the interfaces to be checked * @param methodName Method name of the method we wish to call * @param parameterTypes The parameter type signatures * @return the accessible method or {@code null} if not found */ private static Method getAccessibleMethodFromInterfaceNest(Class> cls, final String methodName, final Class>... parameterTypes) { // Search up the superclass chain for (; cls != null; cls = cls.getSuperclass()) { // Check the implemented interfaces of the parent class final Class>[] interfaces = cls.getInterfaces(); for (final Class> anInterface : interfaces) { // Is this interface public? if (!ClassUtils.isPublic(anInterface)) { continue; } // Does the method exist on this interface? try { return anInterface.getDeclaredMethod(methodName, parameterTypes); } catch (final NoSuchMethodException ignored) { /* * Swallow, if no method is found after the loop then this * method returns null. */ } // Recursively check our parent interfaces final Method method = getAccessibleMethodFromInterfaceNest(anInterface, methodName, parameterTypes); if (method != null) { return method; } } } return null; } /** * Finds 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 * {@link * #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}. *
* *This method can match primitive parameter by passing in wrapper classes. * For example, a {@link Boolean} will match a primitive {@code boolean} * parameter. *
* * @param cls find method in this class * @param methodName find method with this name * @param parameterTypes find method with most compatible parameters * @return The accessible method */ public static Method getMatchingAccessibleMethod(final Class> cls, final String methodName, final Class>... parameterTypes) { try { return MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes)); } catch (final NoSuchMethodException ignored) { // Swallow the exception } // search through all methods final Method[] methods = cls.getMethods(); final ListStops 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.
* @param * the annotation type * @param method * the {@link Method} to query * @param annotationCls * the {@link Annotation} to check if is present on the method * @param searchSupers * determines if a lookup in the entire inheritance hierarchy of the given class is performed * if the annotation was not directly present * @param ignoreAccess * determines if underlying method has to be accessible * @return the first matching annotation, or {@code null} if not found * @throws NullPointerException if either the method or annotation class is {@code null} * @since 3.6 */ public static A getAnnotation(final Method method, final Class annotationCls, final boolean searchSupers, final boolean ignoreAccess) { Objects.requireNonNull(method, "method"); Objects.requireNonNull(annotationCls, "annotationCls"); if (!ignoreAccess && !MemberUtils.isAccessible(method)) { return null; } A annotation = method.getAnnotation(annotationCls); if (annotation == null && searchSupers) { final Class> mcls = method.getDeclaringClass(); final List