Skip to content

Commit 8d3f59e

Browse files
committed
Remove unused
org.apache.commons.beanutils2.MethodUtils.invokeMethod(Object, String, Object) in favor of Apache Commons Lang's MethodUtils
1 parent a8c8e47 commit 8d3f59e

File tree

3 files changed

+7
-119
lines changed

3 files changed

+7
-119
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<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 MethodUtils.</action>
4444
<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 MethodUtils.</action>
4545
<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 MethodUtils.</action>
46+
<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 MethodUtils.</action>
4647
</release>
4748
<release version="2.0.0-M2" date="2025-05-25" description="This is a major release and requires Java 8.">
4849
<!-- FIX -->

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ private static float getTotalTransformationCost(final Class<?>[] srcArgs, final
632632
* @throws InvocationTargetException wraps an exception thrown by the method invoked
633633
* @throws IllegalAccessException if the requested method is not accessible via reflection
634634
*/
635-
public static Object invokeExactMethod(final Object object, final String methodName, Object[] args)
635+
public static Object invokeExactMethod(final Object object, final String methodName, Object... args)
636636
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
637637
if (args == null) {
638638
args = BeanUtils.EMPTY_OBJECT_ARRAY;
@@ -718,39 +718,6 @@ public static Object invokeExactStaticMethod(final Class<?> objectClass, final S
718718
return method.invoke(null, args);
719719
}
720720

721-
/**
722-
* <p>
723-
* Invoke a named method whose parameter type matches the object type.
724-
* </p>
725-
*
726-
* <p>
727-
* The behavior of this method is less deterministic than {@code invokeExactMethod()}. It loops through all methods with names that match and then executes
728-
* the first it finds with compatible parameters.
729-
* </p>
730-
*
731-
* <p>
732-
* This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a {@code Boolean} class would match a
733-
* {@code boolean} primitive.
734-
* </p>
735-
*
736-
* <p>
737-
* This is a convenient wrapper for {@link #invokeMethod(Object object,String methodName,Object [] args)}.
738-
* </p>
739-
*
740-
* @param object invoke method on this object
741-
* @param methodName get method with this name
742-
* @param arg use this argument. May be null (this will result in calling the parameterless method with name {@code methodName}).
743-
* @return The value returned by the invoked method
744-
* @throws NoSuchMethodException if there is no such accessible method
745-
* @throws InvocationTargetException wraps an exception thrown by the method invoked
746-
* @throws IllegalAccessException if the requested method is not accessible via reflection
747-
*/
748-
public static Object invokeMethod(final Object object, final String methodName, final Object arg)
749-
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
750-
final Object[] args = toArray(arg);
751-
return invokeMethod(object, methodName, args);
752-
}
753-
754721
/**
755722
* <p>
756723
* Invoke a named method whose parameter type matches the object type.

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

Lines changed: 5 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2121
import static org.junit.jupiter.api.Assertions.assertNotNull;
22-
import static org.junit.jupiter.api.Assertions.assertThrows;
2322
import static org.junit.jupiter.api.Assertions.assertTrue;
2423

2524
import java.lang.reflect.Method;
@@ -65,7 +64,7 @@ public void tearDown() {
6564
void testClearCache() throws Exception {
6665
MethodUtils.clearCache(); // make sure it starts empty
6766
final PublicSubBean bean = new PublicSubBean();
68-
MethodUtils.invokeMethod(bean, "setFoo", "alpha");
67+
MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
6968
assertEquals(1, MethodUtils.clearCache());
7069
assertEquals(0, MethodUtils.clearCache());
7170
}
@@ -113,18 +112,6 @@ void testInvokeExactMethodNullArrayNullArray() throws Exception {
113112
assertEquals("parent", result);
114113
}
115114

116-
/**
117-
* <p>
118-
* Test {@code invokeMethod}.
119-
*/
120-
@Test
121-
void testInvokeMethod() throws Exception {
122-
final AbstractParent parent = new AlphaBean("parent");
123-
final BetaBean childOne = new BetaBean("ChildOne");
124-
125-
assertEquals("ChildOne", MethodUtils.invokeMethod(parent, "testAddChild", childOne), "Cannot invoke through abstract class (1)");
126-
}
127-
128115
@Test
129116
void testInvokeMethodArray() throws Exception {
130117
final AbstractParent parent = new AlphaBean("parent");
@@ -137,13 +124,6 @@ void testInvokeMethodArray() throws Exception {
137124
assertEquals("ChildTwo", MethodUtils.invokeMethod(parent, "testAddChild2", params), "Cannot invoke through abstract class");
138125
}
139126

140-
@Test
141-
void testInvokeMethodNull() throws Exception {
142-
final Object object = new Object();
143-
final Object result = MethodUtils.invokeMethod(object, "toString", (Object) null);
144-
assertEquals(object.toString(), result);
145-
}
146-
147127
@Test
148128
void testInvokeMethodNullArray() throws Exception {
149129
final Object result = MethodUtils.invokeMethod(new AlphaBean("parent"), "getName", null);
@@ -158,79 +138,19 @@ void testInvokeMethodNullArrayNullArray() throws Exception {
158138
assertEquals("parent", result);
159139
}
160140

161-
@Test
162-
void testInvokeMethodObject() throws Exception {
163-
final AbstractParent parent = new AlphaBean("parent");
164-
final Child childTwo = new AlphaBean("ChildTwo");
165-
166-
assertEquals("ChildTwo", MethodUtils.invokeMethod(parent, "testAddChild", childTwo), "Cannot invoke through interface (1)");
167-
}
168-
169-
@Test
170-
void testInvokeMethodPrimitiveBoolean() throws Exception {
171-
final PrimitiveBean bean = new PrimitiveBean();
172-
MethodUtils.invokeMethod(bean, "setBoolean", Boolean.FALSE);
173-
assertEquals(false, bean.getBoolean(), "Call boolean property using invokeMethod");
174-
}
175-
176-
@Test
177-
void testInvokeMethodPrimitiveDouble() throws Exception {
178-
final PrimitiveBean bean = new PrimitiveBean();
179-
MethodUtils.invokeMethod(bean, "setDouble", Double.valueOf(25.5d));
180-
assertEquals(25.5d, bean.getDouble(), 0.01d, "Set double property using invokeMethod");
181-
}
182-
183-
@Test
184-
void testInvokeMethodPrimitiveFloat() throws Exception {
185-
final PrimitiveBean bean = new PrimitiveBean();
186-
MethodUtils.invokeMethod(bean, "setFloat", Float.valueOf(20.0f));
187-
assertEquals(20.0f, bean.getFloat(), 0.01f, "Call float property using invokeMethod");
188-
}
189-
190-
@Test
191-
void testInvokeMethodPrimitiveInt() throws Exception {
192-
final PrimitiveBean bean = new PrimitiveBean();
193-
MethodUtils.invokeMethod(bean, "setInt", Integer.valueOf(12));
194-
assertEquals(12, bean.getInt(), "Set int property using invokeMethod");
195-
}
196-
197-
@Test
198-
void testInvokeMethodPrimitiveLong() throws Exception {
199-
final PrimitiveBean bean = new PrimitiveBean();
200-
MethodUtils.invokeMethod(bean, "setLong", Long.valueOf(10));
201-
assertEquals(10, bean.getLong(), "Call long property using invokeMethod");
202-
}
203-
204-
@Test
205-
void testInvokeMethodUnknown() throws Exception {
206-
// test that exception is correctly thrown when a method cannot be found with matching params
207-
final AbstractParent parent = new AlphaBean("parent");
208-
final BetaBean childOne = new BetaBean("ChildOne");
209-
assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeMethod(parent, "bogus", childOne));
210-
}
211-
212141
@Test
213142
void testNoCaching() throws Exception {
214143
// no caching
215144
MethodUtils.setCacheMethods(false);
216145

217146
final PublicSubBean bean = new PublicSubBean();
218-
MethodUtils.invokeMethod(bean, "setFoo", "alpha");
147+
MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
219148
assertEquals(0, MethodUtils.clearCache());
220149

221150
// reset default
222151
MethodUtils.setCacheMethods(true);
223152
}
224153

225-
@Test
226-
void testParentMethod() throws Exception {
227-
final String a = "A";
228-
final String actual1 = (String) MethodUtils.invokeMethod(a, "toLowerCase", null);
229-
assertEquals("a", actual1);
230-
final char actual2 = (char) MethodUtils.invokeMethod(a, "charAt", 0);
231-
assertEquals('A', actual2);
232-
}
233-
234154
@Test
235155
void testPublicSub() throws Exception {
236156
// make sure that bean does what it should
@@ -244,9 +164,9 @@ void testPublicSub() throws Exception {
244164

245165
// see if we can access public methods in a default access superclass
246166
// from a public access subclass instance
247-
MethodUtils.invokeMethod(bean, "setFoo", "alpha");
167+
MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
248168
assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)");
249-
MethodUtils.invokeMethod(bean, "setBar", "beta");
169+
MethodUtils.invokeExactMethod(bean, "setBar", "beta");
250170
assertEquals(bean.getBar(), "beta", "Set value (bar:2)");
251171

252172
Method method = MethodUtils.getAccessibleMethod(PublicSubBean.class, "setFoo", String.class);
@@ -270,7 +190,7 @@ void testSetCacheMethods() throws Exception {
270190
MethodUtils.clearCache(); // make sure it starts empty
271191

272192
final PublicSubBean bean = new PublicSubBean();
273-
MethodUtils.invokeMethod(bean, "setFoo", "alpha");
193+
MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
274194
assertEquals(1, MethodUtils.clearCache());
275195
assertEquals(0, MethodUtils.clearCache());
276196
}

0 commit comments

Comments
 (0)