1818package org .apache .commons .beanutils2 ;
1919
2020import java .lang .reflect .Method ;
21- import java .lang .reflect .Modifier ;
2221import java .util .Arrays ;
2322import java .util .Map ;
2423import java .util .Objects ;
@@ -170,41 +169,11 @@ private static Method computeIfAbsent(final MethodKey key, final Function<Method
170169 * @param method The method that we wish to call.
171170 * @return The accessible method.
172171 * @since 1.8.0
172+ * @deprecated Use {@link org.apache.commons.lang3.reflect.MethodUtils#getAccessibleMethod(Class, Method)}.
173173 */
174+ @ Deprecated
174175 public static Method getAccessibleMethod (Class <?> clazz , Method method ) {
175- // Make sure we have a method to check
176- if (method == null ) {
177- return null ;
178- }
179- // If the requested method is not public we cannot call it
180- if (!Modifier .isPublic (method .getModifiers ())) {
181- return null ;
182- }
183- boolean sameClass = true ;
184- if (clazz == null ) {
185- clazz = method .getDeclaringClass ();
186- } else {
187- if (!method .getDeclaringClass ().isAssignableFrom (clazz )) {
188- throw new IllegalArgumentException (clazz .getName () + " is not assignable from " + method .getDeclaringClass ().getName ());
189- }
190- sameClass = clazz .equals (method .getDeclaringClass ());
191- }
192- // If the class is public, we are done
193- if (Modifier .isPublic (clazz .getModifiers ())) {
194- if (!sameClass && !Modifier .isPublic (method .getDeclaringClass ().getModifiers ())) {
195- setMethodAccessible (method ); // Default access superclass workaround
196- }
197- return method ;
198- }
199- final String methodName = method .getName ();
200- final Class <?>[] parameterTypes = method .getParameterTypes ();
201- // Check the implemented interfaces and subinterfaces
202- method = getAccessibleMethodFromInterfaceNest (clazz , methodName , parameterTypes );
203- // Check the superclass chain
204- if (method == null ) {
205- method = getAccessibleMethodFromSuperclass (clazz , methodName , parameterTypes );
206- }
207- return method ;
176+ return org .apache .commons .lang3 .reflect .MethodUtils .getAccessibleMethod (clazz , method );
208177 }
209178
210179 /**
@@ -219,7 +188,7 @@ public static Method getAccessibleMethod(Class<?> clazz, Method method) {
219188 public static Method getAccessibleMethod (final Class <?> clazz , final String methodName , final Class <?>... parameterTypes ) {
220189 return computeIfAbsent (new MethodKey (clazz , methodName , parameterTypes , true ), k -> {
221190 try {
222- return getAccessibleMethod (clazz , clazz .getMethod (methodName , parameterTypes ));
191+ return org . apache . commons . lang3 . reflect . MethodUtils . getAccessibleMethod (clazz , clazz .getMethod (methodName , parameterTypes ));
223192 } catch (final NoSuchMethodException e ) {
224193 return null ;
225194 }
@@ -232,78 +201,11 @@ public static Method getAccessibleMethod(final Class<?> clazz, final String meth
232201 *
233202 * @param method The method that we wish to call.
234203 * @return The accessible method.
204+ * @deprecated Use {@link org.apache.commons.lang3.reflect.MethodUtils#getAccessibleMethod(Method)}.
235205 */
206+ @ Deprecated
236207 public static Method getAccessibleMethod (final Method method ) {
237- return method != null ? getAccessibleMethod (method .getDeclaringClass (), method ) : null ;
238- }
239-
240- /**
241- * Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified method, by scanning through all implemented
242- * interfaces and subinterfaces. If no such method can be found, return {@code null}.
243- *
244- * <p>
245- * 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
246- * the higher level methods.
247- * </p>
248- *
249- * @param clazz Parent class for the interfaces to be checked.
250- * @param methodName Method name of the method we wish to call.
251- * @param parameterTypes The parameter type signatures.
252- */
253- private static Method getAccessibleMethodFromInterfaceNest (Class <?> clazz , final String methodName , final Class <?>[] parameterTypes ) {
254- Method method = null ;
255- // Search up the superclass chain
256- for (; clazz != null ; clazz = clazz .getSuperclass ()) {
257- // Check the implemented interfaces of the parent class
258- final Class <?>[] interfaces = clazz .getInterfaces ();
259- for (final Class <?> anInterface : interfaces ) {
260- // Is this interface public?
261- if (!Modifier .isPublic (anInterface .getModifiers ())) {
262- continue ;
263- }
264- // Does the method exist on this interface?
265- try {
266- method = anInterface .getDeclaredMethod (methodName , parameterTypes );
267- } catch (final NoSuchMethodException e ) {
268- /*
269- * Swallow, if no method is found after the loop then this method returns null.
270- */
271- }
272- if (method != null ) {
273- return method ;
274- }
275- // Recursively check our parent interfaces
276- method = getAccessibleMethodFromInterfaceNest (anInterface , methodName , parameterTypes );
277- if (method != null ) {
278- return method ;
279- }
280- }
281- }
282- // We did not find anything
283- return null ;
284- }
285-
286- /**
287- * 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, return
288- * {@code null}.
289- *
290- * @param clazz Class to be checked.
291- * @param methodName Method name of the method we wish to call.
292- * @param parameterTypes The parameter type signatures.
293- */
294- private static Method getAccessibleMethodFromSuperclass (final Class <?> clazz , final String methodName , final Class <?>[] parameterTypes ) {
295- Class <?> parentClazz = clazz .getSuperclass ();
296- while (parentClazz != null ) {
297- if (Modifier .isPublic (parentClazz .getModifiers ())) {
298- try {
299- return parentClazz .getMethod (methodName , parameterTypes );
300- } catch (final NoSuchMethodException e ) {
301- return null ;
302- }
303- }
304- parentClazz = parentClazz .getSuperclass ();
305- }
306- return null ;
208+ return org .apache .commons .lang3 .reflect .MethodUtils .getAccessibleMethod (method );
307209 }
308210
309211 /**
0 commit comments