Are there any conventions for how to order Java annotations?
For p开发者_如何学Crojects heavily used of Java annotations, is there any suggestion on the annotation order? So as to uniform the code style in the team.
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:
- Which annotations are most important? They should be near the top.
- Which annotations are most likely to be indicative of potential bugs? They should be near the top.
- Which annotations are likely to be on every method in the class? They should be near the bottom.
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.
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 {
// ...
}
精彩评论