Java注释:在变量初始化时,但不在赋值时?

发布于 2024-12-20 09:09:36 字数 1341 浏览 0 评论 0原文

我一直无法理解注释必须或可以放置的确切位置。

使用此方法的类可以编译,但会给出警告“未选中”:

<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 技术交流群。

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

发布评论

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

评论(3

丢了幸福的猪 2024-12-27 09:09:36

来自 Java 语言规范 - 接口 >注释

注释可以在任何声明中用作修饰符,无论是包、类、接口、字段、方法、参数、构造函数还是局部变量。

它只能在声明中使用。

From Java Language Specification - Interfaces > Annotation

Annotations may be used as modifiers in any declaration, whether package, class, interface, field, method, parameter, constructor, or local variable..

It can only use in a declaration.

甜尕妞 2024-12-27 09:09:36

注释是声明的一部分;就像你不能写 Object obj 除非在声明 obj 的地方,也不能写 final obj 除了作为 final Object obj@Deprecated obj 也是被禁止的。

至于优雅 - 无论如何,理想情况下你的方法不应该很长和复杂,但是如果你确实发现你想用这个注释来标记特定的赋值,你总是可以使用一个简单的包装方法:(

@SuppressWarnings("unchecked")
private static <T extends ClassThatDeclaresCloneAsPublic> T cloneObj(T obj)
    { return (T) obj.clone(); }

尽管在在这种特定情况下,我想您可以编写 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 where obj is declared, nor final obj except as final 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:

@SuppressWarnings("unchecked")
private static <T extends ClassThatDeclaresCloneAsPublic> T cloneObj(T obj)
    { return (T) obj.clone(); }

(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.)

二智少女 2024-12-27 09:09:36

注释是声明的一部分(其他人已经回答了这部分)。

但是,回答你问题的第二部分:
当方法像这样短时,我从来没有遇到过用这个注释标记整个方法的问题。我个人更喜欢方法/类级别的注释,而不是方法内部的注释,因为我认为它们只会分散读者的注意力。

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.

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