Dude, where’s my class?

I have pretty strong opinions about java annotations usages. Like many others, I feel that this feature is being overly misused for all sort of purposes, without putting too much thought whether it is the right thing to do or not. It sort of reminds me of how XML was 4-5 years ago… soon someone will come up with a scripting language for annotations and we’ll all be very happy…

I decided to do a small experiment. I thought to myself… let’s have a POJO - a simple POJO. I want to persist it, index it, and not to forget, to de/serialize it from/to xml. Then I thought, why not use EJB3/hibernate annotations for persisting and indexing and that other lib I heard of… what’s the name… ah yes… X2J… to do the xml stuff…

So I started of with this:


public class User {

    private Long id;
    private String firstName;
    private String lastName;
    private Address address;

    // constructors...

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

And ended up with:


@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name = "account_type",
    discriminatorType = DiscriminatorType.STRING
)
@Indexed(index="indexes/users")
@Element
public class User {

    private Long id;
    private String firstName;
    private String lastName;
    private Address address;

    // constructors...

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_id_seq")
    @Keyword(id=true)
    @Attribute
    @PrimaryKey
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "first_name", nullable = false, length = 50)
    @Text(name="firstName")
    @Attribute
    public String getFirstName() {
        return firstName;
    }

    @NotNull
    @Length(min = 1, max = 50)
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Basic
    @Column(name = "last_name", nullable = false, length = 50)
    @Text(name="lastName")
    @Attribute
    public String getLastName() {
        return lastName;
    }

    @NotNull
    @Length(min = 1, max = 255)
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Embedded
    @InnerElement
    @ArrayElement(label=false)
    public Address getAddress() {
        return address;
    }

    @NotNull
    @Valid
    public void setAddress(Address address) {
        this.address = address;
    }
}

Holycrap! Dude… where’s my class?!?!?!

10 Responses to “Dude, where’s my class?”

  1. superdude Says:

    brilliant!

  2. Nicola Ken BArozzi Says:

    LOL! :-D

  3. Alef Arendsen Says:

    LOL????? I don’t really find this very funny to be honest ;-).

  4. Shoaib Says:

    You are correct in saying that Annotations are being abused, to the point where your code becomes unreadable. A simple example like yours is proof enough.

    Annotations can pollute the source code with information that doesn’t necessarily belong in the source file. Annotations are being used to specify everything from deployment information, peristence information, etc., etc. It’s convenient in some cases, but just like XML, it’s being abused.

  5. doj Says:

    This confirm better another thing : it’s hard to code “everything” concerning a given class in its only source code.
    All XML solutions adopted by all these framewors playing around our POJOs (Hibernate, Spring, Struts…) have this interest : they can use another source file + properties and method are referenced the quickest possible way (names, names & parameters).

    This could be a requirement for Annotation, the next version : to support to use another source file + to use quickest possible way to reference the POJO in this additional source file.

    The gain against XML : compilation, total inclusion in the java space, in standard.

  6. Boness Says:

    Yes and no…

    The above is only one consequence of annotation misuse… there are quite a few other issue with it. Please refer to:

    annotations-vs-interoperability

    re-annotations-vs-interoperability

    Bob vs. Spring

    There are not that many generic annotations that pass my test… and for the few that do, I do want to see them in my class… and then there are Domain Annotations which can be of much interest… but I guess we still need to figure what they actually mean and how we can use them in practice.

  7. A Couple of Dutch Rants » So when SHOULD you use annotations? Says:

    […] [1] Uri Boness in Dude, where’s my class? [2] Arjen Poutsma in Annotations/Attributes: How do they help us? [3] Rod Johnson in Ecsaping the Technology Cycle [4] Arjen Poutsma in Annotations vs. Interoperability [5] myself in Re: Annotations vs. Interoperability [6] Uri Boness in Verifying configuration in Spring   […]

  8. 虚拟主机 Says:

    I don’t really find this very funny to be honest ^_^

  9. Rodrigo Urubatan Says:

    but do you know that you do not need all this?
    you can make your class persistent just with two annotations (@Entity and @Id) all the rest is used just if you do not want to use the defaults …

  10. Joe McTee Says:

    Another point to make is that instead of annotating your methods, you could annotate your class variables. This makes it much easier to see your code. And to paraphrase Rodrigo. favor convention over configuration to minimize the number of annotations required. See http://www.jeklsoft.com/downloads/hwa_bjug_20060111/index.html for an example that demonstrates this.

Leave a Reply