Introspecting a Bean : Java Beans « Development Class « Java

Introspecting a Bean

Introspecting a Bean
// :
// Introspecting a Bean.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// See copyright notice in CopyRight.txt.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.BeanInfo;
import java.beans.EventSetDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class BeanDumper extends JFrame {
  private JTextField query = new JTextField(20);

  private JTextArea results = new JTextArea();

  public void print(String s) {
    results.append(s + "\n");

  public void dump(Class bean) {
    BeanInfo bi = null;
    try {
      bi = Introspector.getBeanInfo(bean, Object.class);
    } catch (IntrospectionException e) {
      print("Couldn't introspect " + bean.getName());
    PropertyDescriptor[] properties = bi.getPropertyDescriptors();
    for (int i = 0; i < properties.length; i++) {
      Class p = properties[i].getPropertyType();
      if (p == null)
      print("Property type:\n  " + p.getName() + "Property name:\n  "
          + properties[i].getName());
      Method readMethod = properties[i].getReadMethod();
      if (readMethod != null)
        print("Read method:\n  " + readMethod);
      Method writeMethod = properties[i].getWriteMethod();
      if (writeMethod != null)
        print("Write method:\n  " + writeMethod);
    print("Public methods:");
    MethodDescriptor[] methods = bi.getMethodDescriptors();
    for (int i = 0; i < methods.length; i++)
    print("Event support:");
    EventSetDescriptor[] events = bi.getEventSetDescriptors();
    for (int i = 0; i < events.length; i++) {
      print("Listener type:\n  " + events[i].getListenerType().getName());
      Method[] lm = events[i].getListenerMethods();
      for (int j = 0; j < lm.length; j++)
        print("Listener method:\n  " + lm[j].getName());
      MethodDescriptor[] lmd = events[i].getListenerMethodDescriptors();
      for (int j = 0; j < lmd.length; j++)
        print("Method descriptor:\n  " + lmd[j].getMethod());
      Method addListener = events[i].getAddListenerMethod();
      print("Add Listener Method:\n  " + addListener);
      Method removeListener = events[i].getRemoveListenerMethod();
      print("Remove Listener Method:\n  " + removeListener);

  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);

  public BeanDumper() {
    Container cp = getContentPane();
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout());
    p.add(new JLabel("Qualified bean name:"));
    cp.add(BorderLayout.NORTH, p);
    cp.add(new JScrollPane(results));
    Dumper dmpr = new Dumper();
    // Force evaluation
    dmpr.actionPerformed(new ActionEvent(dmpr, 0, ""));

  public static void main(String[] args) {
    run(new BeanDumper(), 600, 500);


  public static void run(JFrame frame, int width, int height) {
    frame.setSize(width, height);

} ///:~


Related examples in the same category

1.JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2.JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3.JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4.JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5.JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6.Bean ContainerBean Container
8.Listening for Changes to the Selected File in a JFileChooser Dialog
9.Get a list of selected files
10.Listening for Changes to the Current Directory in a JFileChooser Dialog
11.Displaying the Current Directory in the Title of a JFileChooser Dialog
12.Setting an Accessory Component in a JFileChooser Dialog
13.Convert a bean to XML persistence
14.Listen for bean's property change event
15.List property names of a Bean
16.Prevent bean's property being serialized to XML
17.Create an instance a Bean
18.Convert an XML persistence to bean
19.Determine bean's property type
20.Listen for a constrained property change
21.Bean has a single property called property.
22.Implementing a Bound Property
23.Implementing a Constrained Property: fires a PropertyChangeEvent whenever its value is about to be changed.
24.Instantiating a Bean
25.Listing the Property Names of a Bean
26.Getting and Setting a Property of a Bean
27.Get and set the value of a property in a bean using Expression and Statement
28.Get and set an Object type property
29.gets and sets a primitive type property
30.gets and sets an array type property
31.Serializing a Bean to XML: XMLEncoder only persists the value of public properties.
32.Deserializing a Bean from XML
33.Preventing a Bean Property from Being Serialized to XML
34.Serializing an Immutable Bean Property to XML
35.Listening for a Property Change Event: A property change event is fired when a bound property is changed.
36.Listening for a Vetoable Property Change Event
37.Read bean's property value
38.Saving and restoring the state of a pretend CAD systemSaving and restoring the state of a pretend CAD system
39.An extension of ArrayList that provides some handy utilities for working with JavaBeans
40.An extension of Vector that provides some handy utilities for working with JavaBeans
41.extends SimpleBeanInfo
42.Get and set properties on a bean
43.Bean Utility
44.Is JavaBean Compliant Setter
45.This program demonstrates the use of an XML encoder and decoder to save and restore a frame.
46.This program demonstrates various persistence delegates.
47.Event Tracer