Java注释:在变量初始化时,但不在赋值时?
我一直无法理解注释必须或可以放置的确切位置。
使用此方法的类可以编译,但会给出警告“未选中”:
<B extends BitSet> void doStuff(LinkedList<B> list) {
B board = list.getFirst();
B cloneBoard;
cloneBoard = (B) board.clone(); //unchecked
}
此编译时没有警告:
<B extends BitSet> void doStuff(LinkedList<B> list) {
B board = list.getFirst();
@SuppressWarnings("unchecked")
B cloneBoard = (B) board.clone();
}
此不会编译,但会用错误标记cloneBoard:
<B extends BitSet> void doStuff(LinkedList<B> list) {
B board = list.getFirst();
B cloneBoard;
@SuppressWarnings("unchecked")
cloneBoard = (B) board.clone(); // cloneBoard cannot be resolved to a type;
// VariableDeclaratorID expected
}
在Sun关于注释的教程中,我找不到关于为什么会这样的答案是: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html。
语法定义也没有帮助我,因为我不太确定我是否正确理解它: http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1
在我看来,这里有什么问题注释可以专门用于变量,但只能在声明变量时使用;任何后续作业都不会被注释覆盖。这是正确的吗?有没有比抑制整个方法的未经检查的警告更优雅的解决方案?
I've been having trouble understanding where exactly an annotation has to or can be placed.
The class with this method compiles, but gives a warning "unchecked":
<B extends BitSet> void doStuff(LinkedList<B> list) {
B board = list.getFirst();
B cloneBoard;
cloneBoard = (B) board.clone(); //unchecked
}
This compiles without warning:
<B extends BitSet> void doStuff(LinkedList<B> list) {
B board = list.getFirst();
@SuppressWarnings("unchecked")
B cloneBoard = (B) board.clone();
}
This doesn't compile but marks cloneBoard with an error:
<B extends BitSet> void doStuff(LinkedList<B> list) {
B board = list.getFirst();
B cloneBoard;
@SuppressWarnings("unchecked")
cloneBoard = (B) board.clone(); // cloneBoard cannot be resolved to a type;
// VariableDeclaratorID expected
}
In the Sun tutorial on annotations, I couldn't find an answer as to why this is: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html.
The grammar definition didn't help me either, since I'm not quite sure I understand it correctly: http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1
It seems to me that what's the problem here is that annotations can be specifically used for variables, but only when they are declared; any later assignment will not be covered by the annotation. Is this correct? Is there a more elegant solution than suppressing unchecked warnings for the whole method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 Java 语言规范 - 接口 >注释
它只能在声明中使用。
From Java Language Specification - Interfaces > Annotation
It can only use in a declaration.
注释是声明的一部分;就像你不能写
Object obj
除非在声明obj
的地方,也不能写final obj
除了作为final Object obj
,@Deprecated obj
也是被禁止的。至于优雅 - 无论如何,理想情况下你的方法不应该很长和复杂,但是如果你确实发现你想用这个注释来标记特定的赋值,你总是可以使用一个简单的包装方法:(
尽管在在这种特定情况下,我想您可以编写
cloneBoard = board.getClass().cast(board.clone());
并完全省去注释(如果您愿意的话)。An annotation is part of a declaration; just as you can't write
Object obj
except at the point whereobj
is declared, norfinal obj
except asfinal Object obj
, so too is@Deprecated obj
forbidden.As for elegance — ideally your methods should not be very long and complicated, anyway, but if you do find that you'd like to mark a specific assignment with this annotation, you can always make use of a simple wrapper method:
(Though in this specific case, I suppose you could write
cloneBoard = board.getClass().cast(board.clone());
and dispense with the annotation altogether, if you wanted.)注释是声明的一部分(其他人已经回答了这部分)。
但是,回答你问题的第二部分:
当方法像这样短时,我从来没有遇到过用这个注释标记整个方法的问题。我个人更喜欢方法/类级别的注释,而不是方法内部的注释,因为我认为它们只会分散读者的注意力。
Annotations are part of the declaration (others have already answered this part).
However, to answer the second part of your question:
I have never had a problem marking the whole method with this annotation when the method is short like this. I personally prefer annotations on the method/class level rather than inside of methods because I think they just distract the reader.