反射——根据get返回类型set值
2015-09-10 17:25
387 查看
Method[] methods = bbMsg.getClass().getMethods();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get") && !"getDestinationTemplate".equals(methodName)) {
if (method.getReturnType() == String.class && CollectionUtils.isNotEmpty(getNullValueSet())) {
setDefaultNullString(bbMsg, method, methodName);
}
}
}
private void setDefaultNullString(IPBBRefDataBO bbMsg, Method method, String methodName) {
try {
String getValue = (String) method.invoke(bbMsg);
if (getNullValueSet().contains(getValue)) {
setDefaultNullString(bbMsg, methodName);
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOGGER.warn("fail to call method " + methodName + ", message:" + bbMsg.toString());
}
}
private void setDefaultNullString(IPBBRefDataBO bbMsg, String methodName) {
String setmethodName = methodName.replace("get", "set");
try {
bbMsg.getClass().getMethod(setmethodName, String.class).invoke(bbMsg, new Object[] { null });
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
LOGGER.warn("fail to call method " + methodName + ", message:" + bbMsg.toString());
}
}
for (Method method : methods) {
String methodName = method.getName();
if (methodName.startsWith("get") && !"getDestinationTemplate".equals(methodName)) {
if (method.getReturnType() == String.class && CollectionUtils.isNotEmpty(getNullValueSet())) {
setDefaultNullString(bbMsg, method, methodName);
}
}
}
private void setDefaultNullString(IPBBRefDataBO bbMsg, Method method, String methodName) {
try {
String getValue = (String) method.invoke(bbMsg);
if (getNullValueSet().contains(getValue)) {
setDefaultNullString(bbMsg, methodName);
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOGGER.warn("fail to call method " + methodName + ", message:" + bbMsg.toString());
}
}
private void setDefaultNullString(IPBBRefDataBO bbMsg, String methodName) {
String setmethodName = methodName.replace("get", "set");
try {
bbMsg.getClass().getMethod(setmethodName, String.class).invoke(bbMsg, new Object[] { null });
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
LOGGER.warn("fail to call method " + methodName + ", message:" + bbMsg.toString());
}
}
相关文章推荐
- ORACLE时间精确到毫秒
- GUI架构方法
- Android RecyclerView 使用完全解析 体验艺术般的控件
- 小笔记 使android界面打开时EditText不自动获得焦点
- 压缩感知(Compressive Sensing)学习之(二)
- Dos的For命令批量修改文件后缀名
- IOS应用内购买(In App Purchase)总结
- Step4:SQL Server 跨网段(跨机房)复制
- Eclipse下svn的创建分支/合并/切换使用
- 自定义控件 --- 加载旋转图标
- php缓存技术
- 深入理解C++编程中的局部变量和全局变量
- 动态规划之背包问题(一)
- 十张图告诉你昨夜苹果都发布了什么新产品
- Next Permutation
- Android与服务器端数据交互
- ios微信web登录,QQweb登录中遇到的坑
- Java中getResourceAsStream的用法
- 自定义控件属性汇总
- 处理高并发高性能队列-Disruptor