Duplicate :
|
/* In the example below we expect the java beans Introspector to find a single read/write property called "foo". Currently it produces a read-only PropertyDescriptor instead, because setFoo() is overloaded. This scenario comes up in both the AWT classes, e.g. the Scrollbar.visible property, and in Swing, e.g. the AbstractButton.mnemonic property. */ import java.beans.*; import java.lang.reflect.*; class FooProperty1 { public String getFoo() { return ""; } public void setFoo(String x) { } public void setFoo(Object x) { setFoo(x.toString()); } } public class IntrospectorBug { /** * Print the PropertyDescriptors computed by the java beans Introspector * for the FooProperty class. We expect one read/write "foo" property. */ public static void main(String[] args) throws Exception { BeanInfo info = java.beans.Introspector.getBeanInfo(FooProperty.class, Object.class); PropertyDescriptor[] pds = info.getPropertyDescriptors(); for(int i = 0; i < pds.length; i++) { System.out.println(propertyDescriptorString(pds[i])); } } /** * PropertyDescriptor.toString() doesn't do anything useful. So we build * a nice string here. */ static String propertyDescriptorString(PropertyDescriptor pd) { String classString = pd.getClass().getName(); String nameString = " \"" + pd.getName() + "\""; Class type = pd.getPropertyType(); String typeString = (type != null) ? " type " + type.getName() : " <null type>"; Method reader = pd.getReadMethod(); Method writer = pd.getWriteMethod(); String accessString = ""; if ((reader == null) && (writer == null)) { accessString = " <no access>"; } else if (reader == null) { accessString = " write-only"; } else if (writer == null) { accessString = " read-only"; } return "#<" + classString + nameString + typeString + accessString + ">"; } }