If I have a typical "value object" (transport object) e g a Java class with only instance variables and getters and setters, then I said that I want to declare that outside of Java, like a Javabean in XML and let the container generate the class. What is the advantage? the other guy asked. I can think of several advantages but I want to know what you think? If we declare it in XML it is more like separating the data from the code, and I think that all those "value objects" are the same, so they should not be their own classes when there is no methods besides getters and setters i e a typical "javabean".

  • JavaBeans can have behavior, i.e. methods and functions. – Tulains Córdova Aug 3 '16 at 17:51

No, you shouldn't declare it in XML, you should use an annotation processor.

it is more like separating the data from the code

No it's not. What you are proposing is generating code from metadata. If you generate code from data, you are making that data part of your code. It's the exact opposite of separation.

I think that all those "value objects" are the same

Yes. Such classes tend to be very repetitive and annoying to write and maintain.

They should not be their own classes when there is no methods besides getters and setters

What happens when you suddenly want to add an extra method? It's going to be a bunch of extra annoying work to convert it into a class and then add the method. On top of that, now you have to manually maintain the class.

So what should you do instead?

One option is FreeBuilder, https://github.com/google/FreeBuilder

You write code like this:

public abstract class Person {
   public abstract String getFirstName();
   public abstract String getLastName();
   public abstract Date getBirthDate();

   public String getFullName() {
      return getFirstName() + " " + getLastName();

   public static class Builder extends Person_Builder {}


FreeBuilder will automatically generate code for hashCode, equals, and a builder so you can do:

Person person = new Person.Builder()

Adding new fields is probably easier than modifying XML. IDEs natively support annotation processors so they'll naturally work with them. I can easily add methods that aren't fields without losing the ability of the processor to automatically generate the missing function.

On my current project, I wrote my own annotation processor that lets me write code like:

class Person extends PersonImpl {
    static class Fields {
        String first;
        String last;
        Date birthDate; 

AutoBean creates a PersonImpl which provides getters, setters, and builder for all my fields. Whenever I wish all my value objects had a new feature, I can add it to the AutoBean implementation, and they all have it.

Basically, the boilerplate involved in creating nice value objects in Java should be avoided, but XML is a poor way to do it. Use an annotation processor, either one that provides what you need, or roll your own.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.