Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Java Reflection


Dr. Dobb's Journal January 1998: Reflecting on Beans

Reflecting on Beans

Dr. Dobb's Journal January 1998

By Chris Howard


JavaBeans, the platform-neutral component architecture for Java, was added in JDK 1.1. In the Windows world, a Java Bean is similar to an OCX or VBX control. A Bean is reusable and can be visually manipulated in a software-development tool such as Borland's JBuilder, IBM's VisualAge for Java, Sun's Java Workshop, Sybase's PowerJ, Symantec's Visual Cafe, and, to some extent, Microsoft's Visual J++. But how do those tools know how to manipulate a Bean? They do it through a process known as "reflection."

In the past, a software component might publish an API in a separate definition file or use some other technique to inform an application of its interface: A Bean-aware application uses the Java Core Reflection API instead. This lets an application dynamically read the class and method definitions directly from the binary class file. With reflection, you can acquire interfaces and instances for any other object. Without reflection, JavaBeans wouldn't be possible.

The Java Core Reflection API is a low-level interface, and while it can be used directly, there are some pitfalls. Determining a valid method for a Bean can be subject to some interpretation by third-party applications. An easier (and better) way to inspect Bean properties is the Introspector class, which uses the Reflection API internally to do its work.

The Introspector class understands Beans completely, so it won't make any mistakes interpreting Bean methods and properties. If all applications use the Introspector class to get a Bean's interface, they will all expose a consistent representation of the Bean's methods. The main function of the Introspector class is to return a BeanInfo class for any Bean passed as an argument to one of two getBeanInfo(bean) methods. One method merges all BeanInfo descriptions of every parent of the specified Bean; the other method lets you specify a class beyond which introspection should be ignored. Remember that every Java class is a legal Bean, so the inheritance chain could be quite long if you don't stop it. Eventually, it will stop at the Object class.

If a Bean does not specify a BeanInfo class, then one will be constructed automatically by the Introspector class and returned. Most Beans designed for third-party use in a Java IDE will implement a BeanInfo object. The information in a BeanInfo includes property, event, set, and method descriptors, as well as default values, and even a Bean icon that can be used by the IDE to represent the Bean.

You don't have to be writing an IDE to make use of the Introspector class. Any application can be a container for JavaBeans. This means you can structure your program in such a way that your application examines Beans, and can determine their use on the fly. A paint program, for instance, could implement drawing objects and graphics file formats as Beans. For your Java applications, it's something to reflect on.

-- C.H.


Copyright © 1998, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.