Is this a suitable way to access the properties of a bean without knowing / not caring about its exact type? (Or is there a built-in method that does this already?) Is there a suitable throw exception when the property does not exist or is not available?
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? }
source share