This section will start off with an overview of the converter scheme for the JBeans package and then
move on to events.
Conversion (or from the definition section, auto-conversion) occurs when properties are being changed
and the type of the new value is not the correct type for the set method. For example, if we are
trying to change the value of a property called age, which is of type int, and we pass it the string
42 as a String object. Obviously the value of the string is a proper integer, but you can't just
cast a string to an int (at least in Java :). So, what you do is ask the BeanProperty (or which
ever class you are using) to auto-convert the value for you. Here's an example using the BeanProperty
class:
/* We'll use this JavaBean for our example */
public class User {
private int age;
public int getAge() { return age; }
public void setAge(int value) { age = value; }
}
// Auto-conversion
User userInstance = new User();
try {
BeanProperty property = new BeanProperty("age", User.class);
property.setPropertyValue(userInstance, "42", true);
System.out.println("The user's age: " + user.getAge());
} catch (BeanException be) {
be.printStackTrace();
}
/*
Output:
The user's age: 42
*/
A few things to notice about this example. First, when we called the setPropertyValue method we used
the version that takes a third parameter which tells the method whether or not to convert the
parameter (if necessary). Second, when conversion fails, a TypeConversionException is thrown. This
exception is a sub-class of BeanException and therefore is caught by out catch block.
But there it is. Very simple indeed. Okay, you're dying to know how it works right? Well, here's the
skinny. The setPropertyValue method determines what the type of the property that is being set is.
If the new value is not of the same type or a sub-class of the type, auto-conversion happens. If it
is of the same type or sub-class, no conversion occurs. The way that the actual conversion occurs
is that the TypeConverterManager is consult to determine if a converter exists for the type of
the property (in our case Integer.TYPE). If there is one, it is retrieved and the convert method is
called on it to do the actual conversion.
Although it is important to know how to implement new converters, it is a fairly simple process and
we will only go over it very briefly here. You must implement the org.jbeans.TypeConverter interface.
You can extend the org.jbeans.BaseTypeConverter class, which is an excellent starting point. After
you have completed writing the class, you application must register it with the TypeConverterManager
using the registerTypeConverter method. Most of the rest of it is explained thoroughly in the Javadocs
for the TypeConverter, BaseTypeConverter and TypeConverterManager.
One last note before moving on. The TypeConverter interface is very powerful and handles all types
of conversions including conversions to and from arrays. JBeans supports all of these conversion.
That is to say, if you have a property which is an array type, you can convert a String into an
instance of the array type. For example, you can convert the String "1,2,3,4" to an
array of Integer objects. This is done using a delimiter character, tokenizing the string and
converting each token into the correct type.
|