Skip to content

Commit 7790d95

Browse files
committed
Remove unused org.apache.commons.beanutils2.MethodUtils.invoke*(*)
methods in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils
1 parent abb7745 commit 7790d95

File tree

4 files changed

+10
-165
lines changed

4 files changed

+10
-165
lines changed

src/changes/changes.xml

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,12 @@
3737
<!-- UPDATE -->
3838
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-lang3 from 3.17.0 to 3.18.0 #357.</action>
3939
<!-- REMOVE -->
40-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused ConstructorUtils in favor of Apache Commons Lang's org.apache.commons.lang3.ConstructorUtils. ConstructorUtils is unused in this component.</action>
41-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeExactMethod(Object, String, Object) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
42-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeExactMethod(Object, String, Object[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
43-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeStaticMethod(Class, String, Object) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
44-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeExactStaticMethod(Class, String, Object) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
45-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeExactStaticMethod(Class, String, Object) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
46-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeExactStaticMethod(Class, String, Object[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
47-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeMethod(Object, String, Object) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
48-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeMethod(Object, String, Object[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
49-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeMethod(Object, String, Object[], Class[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
50-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeStaticMethod(Class, String, Object[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
51-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeStaticMethod(Class, String, Object[], Class[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
52-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeExactStaticMethod(Class, String, Object[], Class[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
53-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.toNonPrimitiveClass(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class).</action>
54-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.getPrimitiveType(Class) in favor of org.apache.commons.lang3.ClassUtils.wrapperToPrimitive(Class).</action>
55-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove org.apache.commons.beanutils2.MethodUtils.getPrimitiveWrapper(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class).</action>
56-
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove org.apache.commons.beanutils2.MethodUtils.isAssignmentCompatible(Class toClass, Class cls) in favor of org.apache.commons.lang3.ClassUtils.isAssignable(Class cls, Class toClass).</action>
40+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused ConstructorUtils in favor of Apache Commons Lang's org.apache.commons.lang3.ConstructorUtils. ConstructorUtils is unused in this component (org.apache.commons:commons-lang3).</action>
41+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invoke*(*) methods in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils (org.apache.commons:commons-lang3).</action>
42+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.toNonPrimitiveClass(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class) (org.apache.commons:commons-lang3).</action>
43+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.getPrimitiveType(Class) in favor of org.apache.commons.lang3.ClassUtils.wrapperToPrimitive(Class) (org.apache.commons:commons-lang3).</action>
44+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove org.apache.commons.beanutils2.MethodUtils.getPrimitiveWrapper(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class) (org.apache.commons:commons-lang3).</action>
45+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove org.apache.commons.beanutils2.MethodUtils.isAssignmentCompatible(Class toClass, Class cls) in favor of org.apache.commons.lang3.ClassUtils.isAssignable(Class cls, Class toClass) (org.apache.commons:commons-lang3).</action>
5746
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove org.apache.commons.beanutils2.MethodUtils.getAccessibleMethod(Class, String, Class) in favor vararg variant.</action>
5847
</release>
5948
<release version="2.0.0-M2" date="2025-05-25" description="This is a major release and requires Java 8.">

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.lang.ref.Reference;
2121
import java.lang.ref.WeakReference;
22-
import java.lang.reflect.InvocationTargetException;
2322
import java.lang.reflect.Method;
2423
import java.lang.reflect.Modifier;
2524
import java.util.Arrays;
@@ -514,40 +513,6 @@ private static float getTotalTransformationCost(final Class<?>[] srcArgs, final
514513
return totalCost;
515514
}
516515

517-
/**
518-
* Invoke a method whose parameter types match exactly the parameter types given.
519-
*
520-
* <p>
521-
* This uses reflection to invoke the method obtained from a call to {@code getAccessibleMethod()}.
522-
* </p>
523-
*
524-
* @param object invoke method on this object.
525-
* @param methodName get method with this name.
526-
* @param args use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name
527-
* {@code methodName}).
528-
* @param parameterTypes match these parameters - treat null as empty array.
529-
* @return The value returned by the invoked method.
530-
* @throws NoSuchMethodException if there is no such accessible method.
531-
* @throws InvocationTargetException wraps an exception thrown by the method invoked.
532-
* @throws IllegalAccessException if the requested method is not accessible via reflection.
533-
*/
534-
public static Object invokeExactMethod(final Object object, final String methodName, Object[] args, Class<?>[] parameterTypes)
535-
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
536-
if (args == null) {
537-
args = BeanUtils.EMPTY_OBJECT_ARRAY;
538-
}
539-
540-
if (parameterTypes == null) {
541-
parameterTypes = BeanUtils.EMPTY_CLASS_ARRAY;
542-
}
543-
544-
final Method method = getAccessibleMethod(object.getClass(), methodName, parameterTypes);
545-
if (method == null) {
546-
throw new NoSuchMethodException("No such accessible method: " + methodName + "() on object: " + object.getClass().getName());
547-
}
548-
return method.invoke(object, args);
549-
}
550-
551516
/**
552517
* Sets whether methods should be cached for greater performance or not, default is {@code true}.
553518
*

src/test/java/org/apache/commons/beanutils2/MethodUtilsTest.java

Lines changed: 3 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void tearDown() {
6464
void testClearCache() throws Exception {
6565
MethodUtils.clearCache(); // make sure it starts empty
6666
final PublicSubBean bean = new PublicSubBean();
67-
MethodUtils.invokeExactMethod(bean, "setFoo", new String[] { "alpha" }, new Class[] { String.class });
67+
assertNotNull(MethodUtils.getAccessibleMethod(bean.getClass(), "setFoo", new Class[] { String.class }));
6868
assertEquals(1, MethodUtils.clearCache());
6969
assertEquals(0, MethodUtils.clearCache());
7070
}
@@ -99,56 +99,17 @@ void testGetAccessibleMethodIndirectInterface() {
9999
assertMethod(method, "methodBaz");
100100
}
101101

102-
@Test
103-
void testInvokeExactMethodNullArrayNullArray() throws Exception {
104-
final Object result = MethodUtils.invokeExactMethod(new AlphaBean("parent"), "getName", null, null);
105-
106-
assertEquals("parent", result);
107-
}
108-
109102
@Test
110103
void testNoCaching() throws Exception {
111104
// no caching
112105
MethodUtils.setCacheMethods(false);
113-
114106
final PublicSubBean bean = new PublicSubBean();
115-
MethodUtils.invokeExactMethod(bean, "setFoo", new String[] { "alpha" }, new Class[] { String.class });
107+
MethodUtils.getAccessibleMethod(bean.getClass(), "setFoo", new Class[] { String.class });
116108
assertEquals(0, MethodUtils.clearCache());
117-
118109
// reset default
119110
MethodUtils.setCacheMethods(true);
120111
}
121112

122-
@Test
123-
void testPublicSub() throws Exception {
124-
// make sure that bean does what it should
125-
final PublicSubBean bean = new PublicSubBean();
126-
assertEquals(bean.getFoo(), "This is foo", "Start value (foo)");
127-
assertEquals(bean.getBar(), "This is bar", "Start value (bar)");
128-
bean.setFoo("new foo");
129-
bean.setBar("new bar");
130-
assertEquals(bean.getFoo(), "new foo", "Set value (foo)");
131-
assertEquals(bean.getBar(), "new bar", "Set value (bar)");
132-
133-
// see if we can access public methods in a default access superclass
134-
// from a public access subclass instance
135-
MethodUtils.invokeExactMethod(bean, "setFoo", new String[] { "alpha" }, new Class[] { String.class });
136-
assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)");
137-
MethodUtils.invokeExactMethod(bean, "setBar", new String[] { "beta" }, new Class[] { String.class });
138-
assertEquals(bean.getBar(), "beta", "Set value (bar:2)");
139-
140-
Method method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setFoo", String.class);
141-
assertNotNull(method, "getAccessibleMethod() setFoo is Null");
142-
method.invoke(bean, "1111");
143-
assertEquals("1111", bean.getFoo(), "Set value (foo:3)");
144-
145-
method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setBar", String.class);
146-
assertNotNull(method, "getAccessibleMethod() setBar is Null");
147-
method.invoke(bean, "2222");
148-
assertEquals("2222", bean.getBar(), "Set value (bar:3)");
149-
150-
}
151-
152113
/**
153114
* Test {@link MethodUtils#setCacheMethods(boolean)}.
154115
*/
@@ -157,81 +118,11 @@ void testSetCacheMethods() throws Exception {
157118
MethodUtils.setCacheMethods(true);
158119
MethodUtils.clearCache(); // make sure it starts empty
159120
final PublicSubBean bean = new PublicSubBean();
160-
MethodUtils.invokeExactMethod(bean, "setFoo", new String[] { "alpha" }, new Class[] { String.class });
121+
MethodUtils.getAccessibleMethod(bean.getClass(), "setFoo", new Class[] { String.class });
161122
assertEquals(1, MethodUtils.clearCache());
162123
assertEquals(0, MethodUtils.clearCache());
163124
}
164125

165-
/**
166-
* Simple tests for accessing static methods via invokeMethod().
167-
*/
168-
@Test
169-
void testSimpleStatic1() throws Exception {
170-
final TestBean bean = new TestBean();
171-
Object value = null;
172-
int current = TestBean.currentCounter();
173-
// Return initial value of the counter
174-
value = MethodUtils.invokeExactMethod(bean, "currentCounter", new Object[0], new Class[0]);
175-
assertNotNull(value, "currentCounter exists");
176-
assertInstanceOf(Integer.class, value, "currentCounter type");
177-
assertEquals(current, ((Integer) value).intValue(), "currentCounter value");
178-
179-
// Increment via no-arguments version
180-
MethodUtils.invokeExactMethod(bean, "incrementCounter", new Object[0], new Class[0]);
181-
182-
// Validate updated value
183-
current++;
184-
value = MethodUtils.invokeExactMethod(bean, "currentCounter", new Object[0], new Class[0]);
185-
assertNotNull(value, "currentCounter exists");
186-
assertInstanceOf(Integer.class, value, "currentCounter type");
187-
assertEquals(current, ((Integer) value).intValue(), "currentCounter value");
188-
189-
// Increment via specified-argument version
190-
MethodUtils.invokeExactMethod(bean, "incrementCounter", new Object[] { Integer.valueOf(5) }, new Class[] { Integer.TYPE });
191-
192-
// Validate updated value
193-
current += 5;
194-
value = MethodUtils.invokeExactMethod(bean, "currentCounter", new Object[0], new Class[0]);
195-
assertNotNull(value, "currentCounter exists");
196-
assertInstanceOf(Integer.class, value, "currentCounter type");
197-
assertEquals(current, ((Integer) value).intValue(), "currentCounter value");
198-
}
199-
200-
/**
201-
* Simple tests for accessing static methods via invokeExactMethod().
202-
*/
203-
@Test
204-
void testSimpleStatic2() throws Exception {
205-
final TestBean bean = new TestBean();
206-
Object value = null;
207-
int current = TestBean.currentCounter();
208-
// Return initial value of the counter
209-
value = MethodUtils.invokeExactMethod(bean, "currentCounter", new Object[0], new Class[0]);
210-
assertNotNull(value, "currentCounter exists");
211-
assertInstanceOf(Integer.class, value, "currentCounter type");
212-
assertEquals(current, ((Integer) value).intValue(), "currentCounter value");
213-
214-
// Increment via no-arguments version
215-
MethodUtils.invokeExactMethod(bean, "incrementCounter", new Object[0], new Class[0]);
216-
217-
// Validate updated value
218-
current++;
219-
value = MethodUtils.invokeExactMethod(bean, "currentCounter", new Object[0], new Class[0]);
220-
assertNotNull(value, "currentCounter exists");
221-
assertInstanceOf(Integer.class, value, "currentCounter type");
222-
assertEquals(current, ((Integer) value).intValue(), "currentCounter value");
223-
224-
// Increment via specified-argument version
225-
MethodUtils.invokeExactMethod(bean, "incrementCounter", new Object[] { Integer.valueOf(5) }, new Class[] { Integer.TYPE });
226-
227-
// Validate updated value
228-
current += 5;
229-
value = MethodUtils.invokeExactMethod(bean, "currentCounter", new Object[0], new Class[0]);
230-
assertNotNull(value, "currentCounter exists");
231-
assertInstanceOf(Integer.class, value, "currentCounter type");
232-
assertEquals(current, ((Integer) value).intValue(), "currentCounter value");
233-
}
234-
235126
/**
236127
* Simple tests for accessing static methods via getAccessibleMethod()
237128
*/

src/test/java/org/apache/commons/beanutils2/memoryleaktests/MemoryLeakTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ void testMethodUtils_cache_memoryLeak() throws Exception {
382382

383383
// if you comment the following line, the test will work, and the ClassLoader will be released.
384384
// That proves that nothing is wrong with the test, and MethodUtils is holding a reference
385-
assertEquals("initialValue", MethodUtils.invokeExactMethod(bean, "getName", new Object[0], new Class[0]));
385+
assertNotNull(MethodUtils.getAccessibleMethod(bean.getClass(), "getName", new Class[0]));
386386

387387
// this should make the reference go away.
388388
loader = null;

0 commit comments

Comments
 (0)