Method Chaining and Struts

Method chaining is very cool. Basically, it allows you to turn this code:

Car hondaFit = new Car();
hondaFit.setColor(Car.Colors.BLACK);
hondaFit.setStyle(Car.Styles.SPORT);
etc.


To:

Car hondaFit = new Car().
setColor(Car.Colors.BLACK).
setStyle(Car.Styles.SPORT);

Pretty cool. It makes Java/C# style languages function similar to Ruby and it also saves some space when setting a lot of attributes at once.

Implementation is pretty simple. All you have to do is set your domain objects to return a reference pointer in their getters, setters, constructors, and whatever other methods you want to add. For example:

public class Car() {
public Car setColor(Car.Colors colorIn) { this.color = colorIn; return this; } }

There is a downside to this when using Struts, however: Struts doesn’t support method chaining for domain objects. I submitted a bug report but it doesn’t look like the Struts community is interested in changing that in order to keep their POJO implementation as clean as possible. I get that, but it is a bummer that we had to revert back to a method-chainingless implementation of our domain objects.

For reference, the error you will receive when trying to bolt Struts onto a method chained domain object backend is:


ERROR ParametersInterceptor.setParameters()204 - ParametersInterceptor - [setParameters]: Unexpected Exception caught setting '[domainProperty]' on 'class [our action]': Error setting expression '[ourExpression]' with value '[Ljava.lang.String;@81b051'

The items in [ ] I have replaced with generic descriptions of the specific values, but you get the idea.

Advertisements

About johnnywey

Welcome to A Regular Expression. This blog is designed to reflect my thoughts on life, music, software design, Apple, faith, philosophy, and whatever else I can think of.

Posted on June 29, 2008, in Java, Programming, Struts 2. Bookmark the permalink. 2 Comments.

  1. what is mean by regular expressions?
    i want to know about that. please answer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: