java @Deprecated 对方法参数和局部变量是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14627313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:53:34  来源:igfitidea点击:

What does @Deprecated mean on method parameters and local variables?

javadeprecated

提问by adarshr

DISCLAIMER: I know the meaning and purpose of @Deprecated.

免责声明:我知道@Deprecated.

The definition of the @Deprecatedannotation looks like this in the source code of Java:

@DeprecatedJava 源代码中注解的定义如下所示:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

I understand the purpose of having target values of CONSTRUCTOR, FIELD, TYPE, METHODand PACKAGE.

我理解为目标值的目的CONSTRUCTORFIELDTYPEMETHODPACKAGE

However, what does it mean to mark a method parameter or a local variable as @Deprecated?

但是,将方法参数或局部变量标记为 是什么意思@Deprecated

Strangely the below example compiles without any warnings.

奇怪的是,下面的示例编译时没有任何警告。

interface Doable {
    void doIt(@Deprecated Object input);
}

class Action implements Doable {

    @Override
    public void doIt(@Deprecated Object input) {

        String string = String.valueOf(input); // no warning!

        @Deprecated
        String localVariable = "hello";

        System.out.println("Am I using a deprecated variable?" + localVariable); // no warning!
    }
}

Is this something they might intend to implement in the future?

这是他们将来可能打算实施的吗?



FWIW, I use JDK 1.7.0_11 on Ubuntu 12.04 64bit. The result is the same whether I run the program from Eclipse or command line.

FWIW,我在 Ubuntu 12.04 64 位上使用 JDK 1.7.0_11。无论我是从 Eclipse 还是命令行运行程序,结果都是一样的。

The compiler does spit out warnings for normalusage of @Deprecated, such as using one of the deprecated constructors of the java.util.Dateclass. Just to prove that I don't have a faulty terminal or set up, here is the output:

编译器确实会针对 的正常使用发出警告@Deprecated,例如使用java.util.Date该类的已弃用构造函数之一。只是为了证明我没有错误的终端或设置,这里是输出:

$ javac com/adarshr/Test.java -Xlint:deprecation
com/adarshr/Test.java:12: warning: [deprecation] Date(int,int,int) in Date has been deprecated
        new Date(2013, 1, 31);
        ^
1 warning
$ 
$ javac -version
javac 1.7.0_11

采纳答案by Matt Ball

What does it mean to mark a method parameter or a local variable as @Deprecated?

将方法参数或局部变量标记为 是什么意思@Deprecated

It has the same meaning as when applied to any other element:

与应用于任何其他元素的含义相同:

A program element annotated @Deprecatedis one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

注释的程序元素@Deprecated是程序员不鼓励使用的元素,通常是因为它很危险,或者因为存在更好的替代方案。当在非弃用代码中使用或覆盖已弃用的程序元素时,编译器会发出警告。



Why doesn't the compiler omit warnings for deprecated parameters and fields in Java 7?
Because that's exactly what the JLS (§ 9.6.3.6) dictates.

为什么编译器不忽略 Java 7 中不推荐使用的参数和字段的警告?
因为这正是 JLS(第 9.6.3.6 节)所规定的。

A Java compiler must produce a deprecation warning when a type, method, field, or constructor whose declaration is annotated with the annotation @Deprecatedis used (i.e. overridden, invoked, or referenced by name), unless:

  • The use is within an entity that is itself annotated with the annotation @Deprecated; or

  • The use is within an entity that is annotated to suppress the warning with the annotation @SuppressWarnings("deprecation"); or

  • The use and declaration are both within the same outermost class.

Use of the @Deprecatedannotation on a local variable declaration or on a parameter declaration has no effect.

@Deprecated使用(即覆盖、调用或按名称引用)声明带有注解的类型、方法、字段或构造函数时,Java 编译器必须产生弃用警告,除非:

  • 使用在一个实体中,该实体本身用@Deprecated 注释;或者

  • 使用在一个被注释的实体中以抑制带有注释的警告@SuppressWarnings("deprecation");或者

  • 使用和声明都在同一个最外层类中。

@Deprecated在局部变量声明或参数声明上使用注释无效。

(emphasis added)

(强调)

回答by Stephen C

The JLS expressly states that the @Deprecation annotation is ignored on local variables. See Matt Ball's answer.

JLS 明确指出@Deprecation 注释在局部变量上被忽略。参见马特鲍尔的回答。

Is this something they might intend to implement in the future?

这是他们将来可能打算实施的吗?

I very much doubt it.

我非常怀疑。

  • What could it mean ... apart from its current meaning as an informal reminder to the implementor (and maybe style checkers / PMD / FindBugs / etc) that the local variable needs to be removed.

  • Any materialchange is likely to break source compatibility for people who currently use the annotation as above. The Java maintainers try very hard to avoid breaking old code.

  • 这意味着什么......除了作为对实现者(可能还有样式检查器 / PMD / FindBugs / 等)的非正式提醒的当前含义之外,需要删除局部变量。

  • 对于当前使用上述注释的人来说,任何重大更改都可能破坏源代码兼容性。Java 维护人员非常努力地避免破坏旧代码。

回答by Sandeep T

If you compile such a code (ex:mvn clean compile) you get following warning message

如果您编译这样的代码(例如:mvn clean compile),您会收到以下警告消息

@Deprecated annotation has no effect on this variable declaration

@Deprecated annotation has no effect on this variable declaration

meaning its useless :)

意思是没用:)