How to declare or mark a Java method as deprecated?
I would like to make one of my methods "deprecated" = not used anymore.
But still I would like to have it in my API. I just want to show "warning" to anyone using that method.
How can I achieve that?
Use @Deprecated on method. Don't forget about clarifying javadoc field:
/**
* Does some thing in old style.
*
* @deprecated use {@link #new()} instead.
*/
@Deprecated
public void old() {
// ...
}
Use both @Deprecated
annotation and the @deprecated
JavaDoc tag.
The @deprecated
JavaDoc tag is used for documentation purposes.
The @Deprecated
annotation instructs the compiler that the method is deprecated. Here is what it says in Sun/Oracles document on the subject:
Using the @Deprecated
annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element. In contrast, there is no guarantee that all compilers will always issue warnings based on the @deprecated
Javadoc tag, though the Sun compilers currently do so. Other compilers may not issue such warnings. Thus, using the @Deprecated
annotation to generate warnings is more portable that relying on the @deprecated
Javadoc tag.
You can find the full document at How and When to Deprecate APIs
There are two things you can do:
@Deprecated
annotation to the method, and @deprecated
tag to the javadoc of the method You should do both !
Quoting the java documentation on this subject:
Starting with J2SE 5.0, you deprecate a class, method, or field by using the @Deprecated annotation. Additionally, you can use the @deprecated Javadoc tag tell developers what to use instead.
Using the annotation causes the Java compiler to generate warnings when the deprecated class, method, or field is used. The compiler suppresses deprecation warnings if a deprecated compilation unit uses a deprecated class, method, or field. This enables you to build legacy APIs without generating warnings.
You are strongly recommended to use the Javadoc @deprecated tag with appropriate comments explaining how to use the new API. This ensures developers will have a workable migration path from the old API to the new API
链接地址: http://www.djcxy.com/p/13284.html上一篇: 为什么在允许某些Unicode字符的注释中执行Java代码?
下一篇: 如何将Java方法声明或标记为已弃用?