Home | | Web Programming | Introspection - Java Beans

Chapter: Java The Complete Reference : Applying Java : Java Beans

Introspection - Java Beans

At the core of Java Beans is introspection. This is the process of analyzing a Bean to determine its capabilities.

Introspection

 

At the core of Java Beans is introspection. This is the process of analyzing a Bean to determine its capabilities. This is an essential feature of the Java Beans API because it allows another application, such as a design tool, to obtain information about a component. Without introspection, the Java Beans technology could not operate.

There are two ways in which the developer of a Bean can indicate which of its properties, events, and methods should be exposed. With the first method, simple naming conventions are used. These allow the introspection mechanisms to infer information about a Bean. In the second way, an additional class that extends the BeanInfo interface is provided that explicitly supplies this information. Both approaches are examined here.

 

Design Patterns for Properties

 

A property is a subset of a Bean’s state. The values assigned to the properties determine the behavior and appearance of that component. A property is set through a setter method. A property is obtained by a getter method. There are two types of properties: simple and indexed.

 

Simple Properties

 

A simple property has a single value. It can be identified by the following design patterns, where N is the name of the property and T is its type:

 

public T getN( ) public void setN(T arg)

 

A read/write property has both of these methods to access its values. A read-only property has only a get method. A write-only property has only a set method.

Here are three read/write simple properties along with their getter and setter methods:

 

private double depth, height, width;

 

public double getDepth( ) { return depth;

 

}

 

public void setDepth(double d) { depth = d;

 

}

 

public double getHeight( ) { return height;

 

}

 

public void setHeight(double h) { height = h;

 

}

 

public double getWidth( ) { return width;

 

}

 

public void setWidth(double w) { width = w;

 

}

Indexed Properties

 

An indexed property consists of multiple values. It can be identified by the following design patterns, where N is the name of the property and T is its type:

 

public T getN(int index);

 

public void setN(int index, T value); public T[ ] getN( );

public void setN(T values[ ]);

 

Here is an indexed property called data along with its getter and setter methods:

 

private double data[ ];

 

public double getData(int index) { return data[index];

 

}

 

public void setData(int index, double value) { data[index] = value;

 

}

 

public double[ ] getData( ) { return data;

 

}

 

public void setData(double[ ] values) { data = new double[values.length];

 

System.arraycopy(values, 0, data, 0, values.length);

 

}

Design Patterns for Events

 

Beans use the delegation event model that was discussed earlier in this book. Beans can generate events and send them to other objects. These can be identified by the following design patterns, where T is the type of the event:

 

public void addTListener(TListener eventListener) public void addTListener(TListener eventListener)

throws java.util.TooManyListenersException public void removeTListener(TListener eventListener)

 

These methods are used to add or remove a listener for the specified event. The version of addTListener( ) that does not throw an exception can be used to multicast an event, which means that more than one listener can register for the event notification. The version that throws TooManyListenersException unicasts the event, which means that the number of listeners can be restricted to one. In either case, removeTListener( ) is used to remove the listener. For example, assuming an event interface type called TemperatureListener, a Bean that monitors temperature might supply the following methods:

 

public void addTemperatureListener(TemperatureListener tl) {

 

...

 

}

 

public void removeTemperatureListener(TemperatureListener tl) {

 

...

 

}

 

Methods and Design Patterns

 

Design patterns are not used for naming nonproperty methods. The introspection mechanism finds all of the public methods of a Bean. Protected and private methods are not presented.

 

 

Using the BeanInfo Interface

 

As the preceding discussion shows, design patterns implicitly determine what information is available to the user of a Bean. The BeanInfo interface enables you to explicitly control what information is available. The BeanInfo interface defines several methods, including these:

 

PropertyDescriptor[ ] getPropertyDescriptors( ) EventSetDescriptor[ ] getEventSetDescriptors( ) MethodDescriptor[ ] getMethodDescriptors( )

 

They return arrays of objects that provide information about the properties, events, and methods of a Bean. The classes PropertyDescriptor, EventSetDescriptor, and MethodDescriptor are defined within the java.beans package, and they describe the indicated elements. By implementing these methods, a developer can designate exactly what is presented to a user, bypassing introspection based on design patterns.

 

When creating a class that implements BeanInfo, you must call that class bnameBeanInfo, where bname is the name of the Bean. For example, if the Bean is called MyBean, then the information class must be called MyBeanBeanInfo.

To simplify the use of BeanInfo, JavaBeans supplies the SimpleBeanInfo class. It provides default implementations of the BeanInfo interface, including the three methods just shown. You can extend this class and override one or more of the methods to explicitly control what aspects of a Bean are exposed. If you don’t override a method, then design-pattern introspection will be used. For example, if you don’t override getPropertyDescriptors( ), then design patterns are used to discover a Bean’s properties. You will see SimpleBeanInfo in action later in this chapter.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Applying Java : Java Beans : Introspection - Java Beans |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.