mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
Merge branch 'v5-dev' of github.com:dromara/hutool into v5-dev
This commit is contained in:
commit
029801deed
@ -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];
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user