Say No to Properties Boilerplate!
Here is a simple code example from the
href="http://docs.jboss.org/ejb3/app-server/tutorial/">JBoss EJB3
tutorial.
@Entity
public class LineItem implements java.io.Serializable
{
private int id;
private double subtotal;
private int quantity;
private String product;
private Order order;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public double getSubtotal()
{
return subtotal;
}
public void setSubtotal(double subtotal)
{
this.subtotal = subtotal;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public String getProduct()
{
return product;
}
public void setProduct(String product)
{
this.product = product;
}
@ManyToOne
@JoinColumn(name = "order_id")
public Order getOrder()
{
return order;
}
public void setOrder(Order order)
{
this.order = order;
}
}
64 lines.
Here is what you want to read:
@Entity
public class LineItem implements java.io.Serializable
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
<b>@Property</b> private int id;
<b>@Property</b> private double subtotal;
<b>@Property</b> private int quantity;
<b>@Property</b> private String product;
@ManyToOne
@JoinColumn(name = "order_id")
<b>@Property</b> private Order order;@
}
12 lines.
That's only 20% of the original. The remaining 80% is boilerplate.

And that's not even counting the Javadoc!
There has to be a better way. My graduate student Alexandre Alves has
provided just that in his
href="http://www.geocities.com/adcalves/javaproperty/">M.S. thesis. He
implemented
- an extension to the Mustang javac compiler
- a Javadoc doclet
- an optional annotation processor
- Ant tasks for all these
His tools support the @Property annotation of the preceding
example. Simply call his propjavac, and it synthesizes the
getters and setters and adds a Properties section to the Javadoc.
But wait, there's more. We give you syntax for accessing the
properties, almost like in JavaScript or Visual Basic:
LineItem item = new LineItem();
<b>item.@subtotal</b> = 100;
// calls item.setSubtotal(100)
em.persist(item);
int id = <b>item.@id</b>;
// calls item.getId()
We chose the .@ operator rather than the simple dot in JS/VB
so that there is no confusion with the field access item.id.
Alexandre worked out the pesky details with visibility, read-only
properties, BeanInfo, inheritance, and so on.
But there is still work to be done.
- If this is to be a feature of Dolphin, we can't use annotations to
generate the getters and setters. Annotations aren't supposed to modify
the file in which they are contained. It doesn't seem likely that we can
introduce a property keyword either. I suppose we could href="http://blogs.sun.com/roller/page/gbracha?entry=developing_modules_for_development">reuse
super . . . NOT. But what about
That's just a token, which doesn't break backwardsprivate int @id;
private Order @order;
compatibility because you couldn't have an annotation at that spot. Too
subtle, or just right? - Alexandre's property annotation has attributes for finetuning the
code generation, such as making read-only properties, package visible
getters and setters, etc. That's harder to do with just syntax. Does one
need these features, or should one go by the 80/20 rule? After all, one
can always write plain Java for the non-standard cases. - The
href="http://java.sun.com/products/javabeans/docs/spec.html">JavaBeans
specification has lots of seldom-used advanced features, such as
BeanInfo, indexed properties, property change listeners, and so on. Some
of them can be supported easily, others are trickier. I'll gladly
elaborate if anyone is interested. Does anyone use these regularly, and
if so, do you need boilerplate relief? For example, the href="http://today.java.net/pub/a/today/2006/03/07/unified-jsp-jsf-expression-language.html">JSP/JSF
EL doesn't even deal with indexed properties.
I really want native properties to happen in Dolphin. I am sick of the
boilerplate. Two years ago, I thought it would be too late, but I have
learned from EJB3 that Java can do the right thing...after we've tried
everything else.

- Login or register to post comments
- Printer-friendly version
- cayhorstmann's blog
- 2633 reads






Comments
Link is broken
by dboucquey - 2010-01-08 14:40
Interesting !Is the thesis material available somewhere (the link is broken).
There is a high risk of collison however with frameworks that use annotations on the getter/setter (e.g. Hibernate)
On the other hand the doclet concept is very intersting to build API public documentation: I always thought it a pain to have to comment or read comments at getter/setter level (after all, formally speaking the setter just sets and the getter just gets the value) while only the documentation about the property itself is useful, but this one is usually private...
Thanks !