Reflecting, Introspecting, and Customizing JavaBeans
By Laura Bennett2003-10-09
Reflection
Before you start developing ways for users to customize your beans, you should understand the reflection technology introduced in JDK 1.1. Reflection is the process of looking at a JavaBean during run time in order to understand its features or properties. A visual builder can read the internals of a JavaBean from its binary class file using the Java Reflection APIs. It can interpret the bean's properties using a convention known as design patterns. You should use design patterns, or templates, when you name setters and getters for properties.
Setters are a type of method that assign a value to a property. Getters are a type of method that allow a program to retrieve the current value of a property. The method name is based on the property contained in the bean.
As an example, a clock bean needs to have its style, analog or digital, set. Its setter method would begin with the word set and its getter method would begin with get . The method should have the name of the property contained within the method name itself. For example, the style property methods would be named setStyle() and getStyle(). If you wanted to allow users or tools to manipulate the background color of the clock, the bean would have the accessor methods setBackgroundColor() and getBackgroundColor().
You can choose to not make public the setter and getter methods for a bean's property. In that case, the property is known as a read-only or write-only property where users don't have the authority to customize it.
Reflection also allows a user or tool to manipulate a bean. The reflection process can occur by looking at the bean code itself programmatically or by introspecting BeanInfo. (See Introspection.) Reflection is an example of a low-level interface.
Take a look at an example demonstrating reflection. In the example, text is a property of the TickerTape bean. You will see a method to both retrieve and set the value of the text property. The last statement in the setText() method fires an event to make the visual builder aware that a change with the bean has occurred.
Tutorial Pages:
» Reflecting, introspecting, and customizing JavaBeans
» Reflection
» Customization using property sheets and editors
» Customization using customizers
» Summary
» Resources
First published by IBM DeveloperWorks
