At work, I often have to deal with 3rd party libraries, which make heavy use of JavaBeans. Because working with JavaBeans (especially creation) is very tedious and violates dry (lots of replication), I often write a helper class to be able to create JavaBeans in more “oo-like” (i.e., pass all the needed information into the constructor), something like this
// left out the implementation of the method, just to give an idea
public class MyBean {
public MyBean();
public void setFoo(String foo);
public void setBar(String bar);
}
public MyBeans {
public static MyBean newMyBean(String foo, String bar);
}
Now, with using static imports, I can:
MyBean bean = newMyBean(foo, bar);
Instead of:
MyBean bean = new MyBean();
bean.setFoo(foo);
bean.setBar(bar);
Is this some sort of idiom, are there any downsides (I know, the static import is kinda controverse), and are there any Maven plugins which can generate these helper classes for me?
I know of no Maven plugin that will create these factories for you, but it’s a simple code generation library. It needs to be configurable to create various factories given a class, which properties you’d like to set, but must take class specifics into account.
Because of the configuration needs (as I see it, anyway) I’ve never bothered wrapping my implementations into a Maven plugin. I generate them manually (along with fluid interface options), and never think about them again.
IMO they usually require human intervention, so it’s not worth Mavenizing the process. Just generate them into the normal
src/main/javatree and pretend you wrote them yourself.