在方法参数上使用@Deprecated 的原因是什么?

What is the reason of using @Deprecated on method parameter?

@Deprecated 注释目标定义为:

@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})

另一方面,JavaDoc 说:

Use of the @Deprecated annotation on a local variable declaration or on a parameter declaration or a package declaration has no effect on the warnings issued by a compiler.

方法参数可能需要 @Deprecated 时可能出现的情况是什么?

原因是向后兼容。

在 Java 5 和 6 中,@Deprecated 没有 @Target 元注释。 Java 7 添加了具有相同效果的 @Target 元注释。

根据 @Target documentation:

If an @Target meta-annotation is not present on an annotation type T, then an annotation of type T may be written as a modifier for any declaration except a type parameter declaration.

Oracle 对永不破坏行为偏执:任何在 JDK 6 下编译的代码也应该在 JDK 7 下编译。额外的元素类型如 PARAMETER 仅包含在如果某些地方的代码在形式参数上有 @Deprecated

ANNOTATION_TYPE 元素类型从 @Target 元注释中丢失似乎是一个错误,但实际上以下代码在使用 JDK 8 或 JDK11:

@Deprecated
public @interface DeprecatedAnnotation { }