[ Pobierz całość w formacie PDF ]
.In fact, one of the primereasons that reflection was added to Java was to support Beans (althoughreflection also supports object serialization and remote method invocation).Soyou might expect that the creator of the application builder tool would have toreflect each Bean and hunt through its methods to find the properties and eventsfor that Bean.[ Add Comment ]This is certainly possible, but the Javadesigners wanted to provide a standard tool, not only to make Beans simpler touse but also to provide a standard gateway to the creation of more complexBeans.This tool is theIntrospector class, andthe most important method in this class is the staticgetBeanInfo( ).Youpass a Class reference to this method and it fully interrogates thatclass and returns a BeanInfo object that you can then dissect to findproperties, methods, and events.[ Add Comment ]Usually you won’t care about any ofthis—you’ll probably get most of your Beans off the shelf fromvendors, and you don’t need to know all the magic that’s going onunderneath.You’ll simply drag your Beans onto your form, then configuretheir properties and write handlers for the events you’re interested in.However, it’s an interesting and educational exercise to use theIntrospector to display information about a Bean, so here’s a toolthat does it://: c13:BeanDumper.java// Introspecting a Bean.// <applet code=BeanDumper width=600 height=500>// </applet>import java.beans.*;import java.lang.reflect.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import com.bruceeckel.swing.*;public class BeanDumper extends JApplet {JTextField query =new JTextField(20);JTextArea results = new JTextArea();public void prt(String s) {results.append(s + "\n");}public void dump(Class bean){results.setText("");BeanInfo bi = null;try {bi = Introspector.getBeanInfo(bean, java.lang.Object.class);} catch(IntrospectionException e) {prt("Couldn't introspect " +bean.getName());return;}PropertyDescriptor[] properties =bi.getPropertyDescriptors();for(int i = 0; i < properties.length; i++) {Class p = properties[i].getPropertyType();prt("Property type:\n " + p.getName() +"Property name:\n " +properties[i].getName());Method readMethod =properties[i].getReadMethod();if(readMethod != null)prt("Read method:\n " + readMethod);Method writeMethod =properties[i].getWriteMethod();if(writeMethod != null)prt("Write method:\n " + writeMethod);prt("====================");}prt("Public methods:");MethodDescriptor[] methods =bi.getMethodDescriptors();for(int i = 0; i < methods.length; i++)prt(methods[i].getMethod().toString());prt("======================");prt("Event support:");EventSetDescriptor[] events =bi.getEventSetDescriptors();for(int i = 0; i < events.length; i++) {prt("Listener type:\n " +events[i].getListenerType().getName());Method[] lm =events[i].getListenerMethods();for(int j = 0; j < lm.length; j++)prt("Listener method:\n " +lm[j].getName());MethodDescriptor[] lmd =events[i].getListenerMethodDescriptors();for(int j = 0; j < lmd.length; j++)prt("Method descriptor:\n " +lmd[j].getMethod());Method addListener =events[i].getAddListenerMethod();prt("Add Listener Method:\n " +addListener);Method removeListener =events[i].getRemoveListenerMethod();prt("Remove Listener Method:\n " +removeListener);prt("====================");}}class Dumper implements ActionListener {public void actionPerformed(ActionEvent e) {String name = query.getText();Class c = null;try {c = Class.forName(name);} catch(ClassNotFoundException ex) {results.setText("Couldn't find " + name);return;}dump(c);}}public void init() {Container cp = getContentPane();JPanel p = new JPanel();p.setLayout(new FlowLayout());p.add(new JLabel("Qualified bean name:"));p.add(query);cp.add(BorderLayout
[ Pobierz całość w formacie PDF ]