这是在不知道/关心其确切类型的情况下访问对象的 bean 属性的适当方法吗?(或者是否已经有内置方法可以做到这一点?)当属性不存在或不可用时是否有适当的异常抛出?
static private Object getBeanPropertyValue(Object bean, String propertyName) {
// access a no-arg method through reflection
// following bean naming conventions
try {
Method m = bean.getClass().getMethod(
"get"
+propertyName.substring(0,1).toUpperCase()
+propertyName.substring(1)
, null);
return m.invoke(bean);
}
catch (SecurityException e) {
// (gulp) -- swallow exception and move on
}
catch (NoSuchMethodException e) {
// (gulp) -- swallow exception and move on
}
catch (IllegalArgumentException e) {
// (gulp) -- swallow exception and move on
}
catch (IllegalAccessException e) {
// (gulp) -- swallow exception and move on
}
catch (InvocationTargetException e) {
// (gulp) -- swallow exception and move on
}
return null; // it would be better to throw an exception, wouldn't it?
}