Java 注释:在变量初始化时,而不是赋值? [英] Java annotations: at variable initialization, but not assignment?

查看:37
本文介绍了Java 注释:在变量初始化时,而不是赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

编译时没有警告:

<B extends BitSet> void doStuff(LinkedList<B> list) {
    B board = list.getFirst();

    @SuppressWarnings("unchecked")
    B cloneBoard = (B) board.clone();
}

这不会编译,但将 cloneBoard 标记为错误:

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
}

在关于注解的 Sun 教程中,我找不到关于为什么会这样的答案:http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html.

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.

语法定义也没有帮助我,因为我不太确定我是否正确理解它:http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1

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?

推荐答案

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

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(); }

(尽管在这种特定情况下,我想您可以编写 cloneBoard = board.getClass().cast(board.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.)

这篇关于Java 注释:在变量初始化时,而不是赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆