Lenik
Lenik

Reputation: 14458

Are there any conventions for how to order Java annotations?

For projects heavily used of Java annotations, is there any suggestion on the annotation order? So as to uniform the code style in the team.

Upvotes: 19

Views: 6872

Answers (3)

corsiKa
corsiKa

Reputation: 82589

I'm going to say there's not. I could be proven wrong on this.

My reasoning is there are hundreds of projects that have annotations, in addition to the ones baked into Java, and the ones baked into our IDEs (specifically Eclipse but I'm sure others have them too.)

So, with all these libraries potentially competitng for "whose on top" I doubt they would agree on a standard.

I would encourage your team to sit down and make decisions about what's best for you guys.

Factors I would consider:

  1. Which annotations are most important? They should be near the top.
  2. Which annotations are most likely to be indicative of potential bugs? They should be near the top.
  3. Which annotations are likely to be on every method in the class? They should be near the bottom.

Upvotes: 12

Steve Chambers
Steve Chambers

Reputation: 39434

Not an exact science but I try to arrange annotations from a "wide scope" at the top to a "narrow scope" at the bottom. Here's an example:

@RadioDomain
@Entity
@Table(name = "receiver")
@ReceiverConstraintCheck
@SuppressWarnings("PMD.ShortVariable")
public class Receiver {
    // ...
}

Upvotes: 4

Edwin Buck
Edwin Buck

Reputation: 70929

If you must impose an order, alphabetical?

Without some sort of knowledge about the annotations you are using, it's hard to suggest a rational ordering. Perhaps a better ordering would suit you and your team better; and if so, if you document what's the agreed order, the only thing left is to verify it follows the team agreed ordering.

After talking to your whole team, you might find you don't need to order them after all.

Upvotes: 4

Related Questions