import java.lang.reflect.*; import java.awt.*; import java.awt.event.*; import java.util.*; import utilities.beans.*; public class FindBeanProperties extends Frame implements ItemListener, WindowListener { protected Object bean; // the bean we analyze protected Class target; // the Class of the bean's class protected Vector properties; // the list of bean properties protected Hashtable rProperties = new Hashtable(); protected Hashtable wProperties = new Hashtable(); // AWT Components for the GUI protected List propertiesList; protected TextField propertyValueTF; //------------------------------------------------------------------- // main() entry point //------------------------------------------------------------------- public static void main (String[] args) { String beanClassName; Object sampleBeanObj = null; if ( args.length != 1 ) { System.out.println("Usage: FindBeanProperties "); System.exit(10); } beanClassName = args[0]; // obtain bean instance sampleBeanObj = BeansKit.newBean(beanClassName); if ( sampleBeanObj == null ) { System.out.println("Could not instantiate bean!"); System.exit(10); } // finally, pop open our window and let user play with props. new FindBeanProperties(sampleBeanObj); } //------------------------------------------------------------------- // FindBeanProperties Constructor //------------------------------------------------------------------- public FindBeanProperties(Object beanObj) { super("Properties Lister"); this.bean = beanObj; properties = findProperties(beanObj); propertiesList = buildList(properties); propertiesList.setMultipleMode(false); // single selections only propertyValueTF = new TextField(); propertyValueTF.setEditable(false); // textfield is for output only add(propertiesList, "Center"); add(propertyValueTF, "South"); propertiesList.addItemListener(this); addWindowListener(this); // listen to our own window close setSize(new Dimension(130,300)); setVisible(true); } //------------------------------------------------------------------- // Construct a List component from the elements contained in a Vector //------------------------------------------------------------------- protected List buildList(Vector listItems) { List aList = new List(); String item; Enumeration vecIterator = listItems.elements(); while ( vecIterator.hasMoreElements() ) { item = (String) vecIterator.nextElement(); aList.add(item); } return aList; } //------------------------------------------------------------------- // Analyze a bean's public methods and record all methods starting // with getXXX and setXXX. //------------------------------------------------------------------- protected Vector findProperties(Object anObject) { Vector properties = new Vector(); Method[] publicMethods; String methodName; String propertyName = null; // get Class for bean target = anObject.getClass(); // get list of accessible public methods publicMethods = target.getMethods(); // forall public methods for (int i=0; i < publicMethods.length; i++) { methodName = publicMethods[i].getName(); if ( methodName.startsWith("set")) { // extract property name from method name propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4); // add property to set of WRITE properties wProperties.put(propertyName, methodName); // check if we've already encountered a matching getter // if so, don't add this property to list again ! if ( rProperties.get(propertyName) != null ) { continue; } } else if ( methodName.startsWith("get")) { propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4); // add property to set of READ properties rProperties.put(propertyName, methodName); // check if we've already encountered a matching setter // if so, don't add this property to list again ! if ( wProperties.get(propertyName) != null ) { continue; } } else { continue; } // add property to list of properties properties.addElement(propertyName); } // end forall public methods return properties; } //------------------------------------------------------------------- // ItemListener method //------------------------------------------------------------------- public void itemStateChanged(ItemEvent itemEvent) { String property, propertyValueText; String methodName; Method getter = null; Object value = "! ERROR !"; // in case we trip over an exception Class[] argsList = {}; // empty argument list for getters property = ((List)(itemEvent.getSource() )).getSelectedItem(); if ( rProperties.get(property) == null ) { propertyValueText = "not a read property"; } else { // recall getter method name for this property methodName = (String) ( rProperties.get(property) ); try { getter = target.getMethod(methodName, argsList); } catch (NoSuchMethodException ouch) { System.out.println(""); System.out.println("We don't support indexed getters" + ouch); } // invoke getter to obtain property value System.out.println("Invoking '" + methodName + "()' to obtain property value"); try { value = getter.invoke(bean, argsList); } catch (IllegalArgumentException badArgs) { System.out.println(badArgs); System.exit(10); } catch (IllegalAccessException noAccess) { System.out.println(noAccess); System.exit(10); } catch (InvocationTargetException wrongTarget) { System.out.println(wrongTarget); System.exit(10); } // convert value to readable representation if ( value == null ) { value = "null"; } propertyValueText = value.toString(); } // display property value propertyValueTF.setText(propertyValueText); } //------------------------------------------------------------------- // WindowListener methods, mostly no-ops, except for close request //------------------------------------------------------------------- public void windowOpened (WindowEvent windowEvent) {} public void windowClosing (WindowEvent windowEvent) { System.exit(0); } public void windowClosed (WindowEvent windowEvent) {} public void windowIconified (WindowEvent windowEvent) {} public void windowDeiconified (WindowEvent windowEvent) {} public void windowActivated (WindowEvent windowEvent) {} public void windowDeactivated (WindowEvent windowEvent) {} } // End of Class FindBeanProperties