我应该为我的注释创建一个常量类吗?
哪个更好?
@SuppressWarnings("unchecked")
@SuppressWarnings(AnnotationConstants.UNCHECKED)
其中 AnnotationConstants
是一个典型的常量类...
public final class AnnotationConstants {
private AnnotationConstants() { }
public static final String UNCHECKED = "unchecked";
...
}
我知道有很多支持和反对常量类的一般论点——而这正是我不感兴趣的。我想知道专门用于注释的常量类是否是一个好主意或坏主意。
Which is better?
@SuppressWarnings("unchecked")
@SuppressWarnings(AnnotationConstants.UNCHECKED)
Where AnnotationConstants
is a typical constants class...
public final class AnnotationConstants {
private AnnotationConstants() { }
public static final String UNCHECKED = "unchecked";
...
}
I know that there are a lot of general arguments for and against constants classes--and that's exactly what I'm not interested in. I want to know if a constants class specifically for annotations is a good idea or a bad idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于这个具体示例,我会坚持使用文字。 毕竟,您正在尝试抑制警告 - 如果您使用错误的文字,警告将不会被抑制,这会引起您对问题的注意。
For this specific example I'd stick with literals. After all, you're trying to suppress warnings - if you use the wrong literal, the warning won't be suppressed, which will draw your attention to the problem.
我想两者都说一点,拜托。
在 Spring 中,您可以说 @Scope(Scopes.SESSION) 或 @Scope(Scopes.REQUEST) 之类的内容,它将特定类型的行为附加到注释,所以我会说总是使用常量类,这有利于可追溯性。
如果您只是抑制警告,那么您不太可能真正想要跟踪它,因此只需按照字面意思进行即可。
I'd say a little bit of both, please.
In Spring, you could say something like @Scope(Scopes.SESSION) or @Scope(Scopes.REQUEST), which attaches a specific kind of behavior to the annotation so I'd say always use a constants class, which is good for traceability.
If you're just supressing warnings, there's little likelyhood that you want to really trace this, so just go with the literal.
我同意 Holsam 的 SuppressWarnings - 使用字符串
如果您正在编写自己的注释,我建议尽可能使用枚举来表示可以表示为一组常量的事物
I'd agree with Holsam for SuppressWarnings - use the strings
If you're writing your own annotation, I'd recommend usng enums where possible for things that could be represented as a set of constants
对于@SuppressWarning之类的东西,我不这么认为。 我更喜欢直接编写常量,并让编译器检查它们(如果可能)。 Eclipse 在这方面做得很好。
For
@SuppressWarning
and the likes, I don't think so. I prefer writing the constants directly, and have the compiler check them if possible. Eclipse does a nice job at that.