Java 注释的排序有什么约定吗?

发布于 2024-11-01 00:07:59 字数 49 浏览 1 评论 0原文

对于大量使用Java注解的项目,对于注解顺序有什么建议吗?从而统一团队内的代码风格。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-11-08 00:07:59

我要说没有。我可能在这一点上被证明是错误的。

我的理由是,除了 Java 中的项目和 IDE 中的项目(特别是 Eclipse,但我确信其他项目也有它们)之外,还有数百个项目具有注释。

因此,所有这些库都具有潜在的竞争性对于“谁在上面”,我怀疑他们会同意一个标准。

我鼓励您的团队坐下来做出对你们最有利的决定。

我会考虑的因素:

  1. 哪些注释最重要?他们应该接近顶部。
  2. 哪些注释最有可能表明潜在的错误?他们应该接近顶部。
  3. 类中的每个方法可能有哪些注释?它们应该接近底部。

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.
望喜 2024-11-08 00:07:59

如果你必须强加一个顺序,按字母顺序?

如果不了解您正在使用的注释,就很难提出合理的排序建议。也许更好的排序更适合您和您的团队;如果是这样,如果您记录了商定的顺序,那么剩下的唯一事情就是验证它是否遵循团队商定的顺序。

与您的整个团队交谈后,您可能会发现您根本不需要订购它们。

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.

£烟消云散 2024-11-08 00:07:59

这不是一门精确的科学,但我尝试将注释从顶部的“宽范围”排列到底部的“窄范围”。这是一个例子:

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

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 {
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文