Java Reference
In-Depth Information
The reason for having the setter return the bean instance is so that you can chain
calls to the setters:
myBean.setProperty(x)
.setSomeProperty(y);
For two properties, it is not all that useful, but if you have a bean in which you
have more, it can save you some typing.
Bean navigation
When using tools that are bean aware, properties are often referred to using
something called dot notation , which means that instead of calling getters and set-
ters, you refer to the properties defined by them. The property name is deter-
mined by using the rules discussed earlier. Table 4.2 contains some examples.
Table 4.2
Bean navigation examples
Java code
Dot notation
anOrder.getAccount().getUsername()
anOrder.account.username
anOrder.getOrderItem().get(0).getProductId()
anOrder.orderItem[0].productId
anObject.getID()
anObject.ID
anObject.getxCoordinate()
anObject.xCoordinate
If you have a bean and want to see the names of the properties of that bean, the fol-
lowing sample method will use the built-in Java Introspector class to output them:
public void listPropertyNames(Class c)
throws IntrospectionException {
PropertyDescriptor[] pd;
pd = Introspector.getBeanInfo(c).getPropertyDescriptors();
for(int i=0; i< pd.length; i++) {
System.out.println(pd[i].getName()
+ " (" + pd[i].getPropertyType().getName() + ")");
}
}
This example uses the Introspector class to get an array of PropertyDescriptor
objects for a bean, and then walks through that array to display the name and type
of the properties that the bean exposes, which can be a useful troubleshooting aid.
Search WWH ::




Custom Search