bean-checker library

Bean checker is a Java library which helps to check that all properties of a Java bean have been set (no matter if they were set to not-null or null values).

For instance, imagine this bean:

public class SimpleBean {
    private Date date;
    private String author;
    private String body;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

}

 

And the calling code:

SimpleBean messageBean = new SimpleBean();

messageBean.setAuthor("vlad");
messageBean.setDate(new Date());

You see above that the developer forgot to call the setBody() setter.

You can avoid situations like these by using this library, like this:

BeanChecker<SimpleBean> beanChecker = BeanCheckerFactory.createBeanChecker(SimpleBean.class);
SimpleBean messageBean = beanChecker.getBeanProxy();

messageBean.setAuthor("vlad");
messageBean.setDate(new Date());

beanChecker.mandatorySettersCalled(); // this will throw an exception if not all setters were called

Maven

Maven dependency:  (stored on maven central, no need for special repository entries)

<dependency>
    <groupId>com.github.vlad2</groupId>
    <artifactId>bean-checker</artifactId>
    <version>1.0.5</version>
</dependency>

github page: https://github.com/vlad2/bean-checker

I know there are some other solutions, like a constructor with all the fields of the bean, grouping setters together, etc; but all these solutions have their pros and cons. This library is very easy to integrate into existing code, especially for debugging purposes. It probably can be even integrated in unit-tests, if there’s a way to construct/inject the bean from the unit-test. Give it a try 🙂

Advertisements

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