import java.beans.*; import java.lang.reflect.*; import utilities.*; public class ShowBeanInterface { //------------------------------------------------------------------- // main() entry point //------------------------------------------------------------------- public static void main (String[] args) { String beanName; Class bean = null; BeanInfo beanInformation = null; boolean all=false, beanOnly=true; // check whether user gave us the right number of arguments if ( args.length < 1 || args.length > 2) { System.out.println( "Usage: ShowBeanInfo [ALL | BEANONLY]"); System.exit(10); } // if optional argument present, check which one if ( args.length == 2 ) { all = args[1].equalsIgnoreCase("ALL"); beanOnly = args[1].equalsIgnoreCase("BEANONLY"); if ( (all == false) && (beanOnly == false) ) { System.out.println( "Optional argument has to be either ALL or BEANONLY."); System.out.println("You entered '"+ args[1] + "'."); System.exit(10); } } // try and locate or load user-specified bean beanName = args[0]; try { bean = Class.forName(beanName); } catch (ClassNotFoundException wheresThatClass) { System.out.println("ERROR: Class for bean "+ beanName + " not found."); System.exit(10); } // use Introspector to find (or synthesize) BeanInfo for user's bean // if user specified BEANONLY option, then don't bother getting parent // beans info. try { if ( beanOnly ) { Class stopClass = bean.getSuperclass(); beanInformation = Introspector.getBeanInfo(bean, stopClass); } else { // "all" beanInformation = Introspector.getBeanInfo(bean); } } catch (IntrospectionException obscureBean) { System.out.println("ERROR: Introspector failed to X-ray your bean."); System.out.println("Can't show BeanInfo for bean '" + beanName +"'."); System.exit(10); } // Print a summary of the specified bean's interface System.out.println("Interface for bean " + beanName + ":"); System.out.println(""); dumpBeanInfo(beanInformation); } //------------------------------------------------------------------- // Given a BeanInfo class, print a summary of the associated bean's // interface (properties, event sets, methods). //------------------------------------------------------------------- protected static void dumpBeanInfo(BeanInfo beanInfo) { dumpBeanProperties(beanInfo); System.out.println(""); dumpBeanFiredEvents(beanInfo); System.out.println(""); dumpBeanPublicMethods(beanInfo); System.out.println(""); } //------------------------------------------------------------------- // List a bean's properties, fired event sets or public methods. //------------------------------------------------------------------- protected static void dumpBeanProperties(BeanInfo beanInfo) { PropertyDescriptor[] props; PropertyDescriptor propertyDescriptor; String propertyName, propertyType, propertyInfo, readWrite; String columnPadding; Method getter, setter; props = beanInfo.getPropertyDescriptors(); System.out.print("Bean Properties: "); if (props == null || props.length == 0) { System.out.println("(none)"); return; } System.out.println(""); System.out.println("----------------"); // for every bean property for (int i=0; i < props.length; i++) { propertyDescriptor = props[i]; // see if this prop is read (R/-), write (-/W) or read/write (R/W) getter = propertyDescriptor.getReadMethod(); setter = propertyDescriptor.getWriteMethod(); readWrite = (getter == null) ? "-" : "R"; readWrite += "/"; readWrite += (setter == null) ? "-" : "W"; propertyName = propertyDescriptor.getName(); propertyType = propertyDescriptor.getPropertyType().getName(); columnPadding = StringKit.columnPadding(12, propertyName); propertyInfo = propertyName + columnPadding + "["+ readWrite +"] " + "("+ propertyType +")"; System.out.println(propertyInfo); } } //------------------------------------------------------------------- // List a bean's fired event sets. //------------------------------------------------------------------- protected static void dumpBeanFiredEvents(BeanInfo beanInfo) { EventSetDescriptor[] events; EventSetDescriptor eventSetDescriptor; events = beanInfo.getEventSetDescriptors(); System.out.print("Bean fired event sets: "); if (events == null || events.length == 0) { System.out.println("(none)"); return; } System.out.println(""); System.out.println("----------------------"); // for every bean event set for (int i=0; i < events.length; i++) { eventSetDescriptor = events[i]; Class listenerType = eventSetDescriptor.getListenerType(); String eventSetInfo = listenerType.getName(); System.out.println("All events for interface: " + eventSetInfo); } } //------------------------------------------------------------------- // List a bean's public methods. //------------------------------------------------------------------- protected static void dumpBeanPublicMethods(BeanInfo beanInfo) { MethodDescriptor[] methods; MethodDescriptor methodDescriptor; Method aMethod; Class[] parameterTypes; String returnTypeName, methodName, parameterList, methodInfo; String column1Padding, column2Padding; methods = beanInfo.getMethodDescriptors(); System.out.print("Bean public methods: "); if (methods == null || methods.length == 0) { System.out.println("(none)"); return; } System.out.println(""); System.out.println("--------------------"); // for every bean public method for (int i=0; i < methods.length; i++) { methodDescriptor = methods[i]; methodName = methodDescriptor.getName(); aMethod = methodDescriptor.getMethod(); parameterTypes = aMethod.getParameterTypes(); returnTypeName = aMethod.getReturnType().getName(); parameterList = genParameterList(parameterTypes); column1Padding = StringKit.columnPadding(19, returnTypeName); column2Padding = StringKit.columnPadding(16, methodName); methodInfo = returnTypeName + column1Padding + methodName + column2Padding + "("+ parameterList +")" + ";"; System.out.println(methodInfo); } } //------------------------------------------------------------------- // Given an array of types (Class objs), produce formal param. list String // sample output: "int, int, java.awt.Point" //------------------------------------------------------------------- protected static String genParameterList(Class[] types) { StringBuffer sb = new StringBuffer(); if ( types != null ) { for (int i=0; i < types.length; i++) { String typeName = types[i].getName(); sb.append(typeName + ", "); } StringKit.shrinkStringBufferBy(2, sb); // strip superfluous ", " } return sb.toString(); } } // End of Class ShowBeanInterface