Skip to content

Commit 9d1d6a5

Browse files
committed
Reuse Lang MethodUtils
1 parent 1ea9cca commit 9d1d6a5

File tree

2 files changed

+12
-110
lines changed

2 files changed

+12
-110
lines changed

src/main/java/org/apache/commons/beanutils2/MethodUtils.java

Lines changed: 7 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.commons.beanutils2;
1919

2020
import java.lang.reflect.Method;
21-
import java.lang.reflect.Modifier;
2221
import java.util.Arrays;
2322
import java.util.Map;
2423
import 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
/**

src/main/java/org/apache/commons/beanutils2/PropertyUtilsBean.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private BeanIntrospectionData fetchIntrospectionData(final Class<?> beanClass) {
296296
}
297297

298298
private Method getAccessibleMethod(final Object bean, final Method writeMethod) {
299-
return MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod);
299+
return org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod);
300300
}
301301

302302
/**
@@ -888,7 +888,7 @@ public Class<?> getPropertyType(Object bean, String name) throws IllegalAccessEx
888888
* @since 2.0.0
889889
*/
890890
public Method getReadMethod(final Class<?> clazz, final PropertyDescriptor descriptor) {
891-
return MethodUtils.getAccessibleMethod(clazz, descriptor.getReadMethod());
891+
return org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(clazz, descriptor.getReadMethod());
892892
}
893893

894894
/**
@@ -904,7 +904,7 @@ public Method getReadMethod(final Class<?> clazz, final PropertyDescriptor descr
904904
* @return The read method
905905
*/
906906
public Method getReadMethod(final PropertyDescriptor descriptor) {
907-
return MethodUtils.getAccessibleMethod(descriptor.getReadMethod());
907+
return org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(descriptor.getReadMethod());
908908
}
909909

910910
/**
@@ -993,7 +993,7 @@ public Object getSimpleProperty(final Object bean, final String name) throws Ill
993993
*/
994994
public Method getWriteMethod(final Class<?> clazz, final PropertyDescriptor descriptor) {
995995
final BeanIntrospectionData data = getIntrospectionData(clazz);
996-
return MethodUtils.getAccessibleMethod(clazz, data.getWriteMethod(clazz, descriptor));
996+
return org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(clazz, data.getWriteMethod(clazz, descriptor));
997997
}
998998

999999
/**
@@ -1013,7 +1013,7 @@ public Method getWriteMethod(final Class<?> clazz, final PropertyDescriptor desc
10131013
* @return The write method
10141014
*/
10151015
public Method getWriteMethod(final PropertyDescriptor descriptor) {
1016-
return MethodUtils.getAccessibleMethod(descriptor.getWriteMethod());
1016+
return org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(descriptor.getWriteMethod());
10171017
}
10181018

10191019
/**

0 commit comments

Comments
 (0)