Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于ReflectionUtil折腾反馈 #559

Open
zhugw opened this issue Apr 11, 2017 · 2 comments
Open

关于ReflectionUtil折腾反馈 #559

zhugw opened this issue Apr 11, 2017 · 2 comments

Comments

@zhugw
Copy link

zhugw commented Apr 11, 2017

ReflectionUtil 使用反馈

单元测试测试一个私有方法 方法签名如下所示:

private List<Bar> getBarList(List<Foo> fooList, Long Id) 

刚开始直接调用

ReflectionUtil.invokeMethod(obj, "getBarList", fooList, 1L);

报错: java.lang.IllegalArgumentException: Could not find method [getBarList] on target [XXX]
断点调试发现实际参数类型用的是ArrayList

于是显式指定方法参数类型

ReflectionUtil.invokeMethod(obj, "getBarList", new Object[]{ fooList, 1L}, new Class[]{List.class,Long.class});

还是不行
断点调试发现下面的代码将Long.class --> long

// 处理原子类型与对象类型的兼容
ClassUtil.wrapClassses(theParameterTypes);

最后只好使用下面的写法解决了该问题

ReflectionUtil.invokeMethodByName(obj, "getBarList", new Object[]{ fooList, 1L});

反馈

我觉得如果调用者显式指定了参数类型 如下所示 直接使用好了 不用多做额外的处理了吧 如包装类型 --> 基本类型

ReflectionUtil.invokeMethod(obj, "getBarList", new Object[]{ fooList, 1L}, new Class[]{List.class,Long.class});
@calvin1978
Copy link
Contributor

谢谢反馈,我看看

@calvin1978
Copy link
Contributor

已修复,多用Apache Commmon Lang里的类

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants