您的位置:首页 > 其它

反射——根据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());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: