diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java index 7b9e36ced..e626df680 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java @@ -1056,9 +1056,11 @@ public class ReflectUtil { actualArgs[i] = null; } else if (false == parameterTypes[i].isAssignableFrom(args[i].getClass())) { //对于类型不同的字段,尝试转换,转换失败则使用原对象类型 - final Object targetValue = Convert.convertQuietly(parameterTypes[i], args[i], args[i]); + final Object targetValue = Convert.convertWithCheck(parameterTypes[i], args[i], null, true); if (null != targetValue) { actualArgs[i] = targetValue; + } else { + actualArgs[i] = args[i]; } } else { actualArgs[i] = args[i]; diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java index 6a5a88adc..28b0b88f4 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java @@ -99,6 +99,30 @@ public class ReflectUtilTest { Assert.assertEquals(10, testClass.getA()); } + @Test + public void invokeMethodTest() { + final TestClass testClass = new TestClass(); + final Method method = ReflectUtil.getMethod(TestClass.class, "setA", int.class); + ReflectUtil.invoke(testClass, method, 10); + Assert.assertEquals(10, testClass.getA()); + } + + @Test + public void invokeMethodWithParamConvertTest() { + final TestClass testClass = new TestClass(); + final Method method = ReflectUtil.getMethod(TestClass.class, "setA", int.class); + ReflectUtil.invoke(testClass, method, "10"); + Assert.assertEquals(10, testClass.getA()); + } + + @Test + public void invokeMethodWithParamConvertFailedTest() { + final TestClass testClass = new TestClass(); + final Method method = ReflectUtil.getMethod(TestClass.class, "setA", int.class); + Assert.assertThrows(IllegalArgumentException.class, + () -> ReflectUtil.invoke(testClass, method, "NaN")); + } + @Test public void noneStaticInnerClassTest() { final NoneStaticClass testAClass = ReflectUtil.newInstanceIfPossible(NoneStaticClass.class);