java:introspection
Introspection
動的にJavaBeanのGetter、Setterを呼び出すには、IntrospectorとBeanInfoを使ってアクセサメソッドオブジェクトを取得します。
SomeBean someBean = new SomeBean(); PrintStream ps = System.out; try { BeanInfo beanInfo = Introspector.getBeanInfo(someBean.getClass()); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd: pds) { String propertyName = pd.getName(); Method method = pd.getReadMethod(); if (method == null) { continue; } Object propertyValue = method.invoke(someBean, new Object[0]); ps.print(propertyName); ps.print("="); ps.println(propertyValue); } } catch (IntrospectionException e) { e.printStackTrace(ps); } catch (IllegalArgumentException e) { e.printStackTrace(ps); } catch (IllegalAccessException e) { e.printStackTrace(ps); } catch (InvocationTargetException e) { e.printStackTrace(ps); }
java/introspection.txt · 最終更新: 2010/12/13 07:11 by 127.0.0.1