Merge branch 'v5-dev' of github.com:dromara/hutool into v5-dev

This commit is contained in:
Looly 2023-02-21 15:57:12 +08:00
commit 029801deed
2 changed files with 27 additions and 1 deletions

View File

@ -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];

View File

@ -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);