java弃用标签_如何将Java方法声明或标记为已弃用?

我想让我的一个方法"弃用"=不再使用了。

但我仍然想在我的API中使用它。 我只想向使用该方法的任何人显示"警告"。

我怎样才能做到这一点?

@Deprecrated不适合你吗?

它是,但我不知道它...这就是为什么我问这个问题:)

请参阅docs.oracle.com/javase/1.5.0/docs/guide/javadoc/deprecation/

评论不是答案的地方!

在方法上使用@Deprecated。不要忘记澄清javadoc字段:

/**

* Does some thing in old style.

*

* @deprecated use {@link #new()} instead.

*/

@Deprecated

public void old() {

// ...

}

如果@Deprecated有一个参数来代替使用,那肯定会很好......

你如何链接外部库?例如:com.hello.api.PublicController #new

@LinuxLars完全同意了! Java 9添加了一些属性来开始认真考虑弃用,但添加另一个属性reason,默认值""不会有伤害

我希望评论中的@deprecated消息可以添加到@deprecated(一个点来解决所有问题)...

同时使用@Deprecated注释和@Deprecated JavaDoc标记。

@Deprecated JavaDoc标记用于文档目的。

@Deprecated注释指示编译器不推荐使用该方法。以下是Sun / Oracles关于该主题的文件:

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.

您可以在如何以及何时弃用API中找到完整的文档

不完全正确。 javadoc和annotation tell编译器方法都已弃用

@Bohemian实际上并不完全正确。注释在Java语言规范部分9.6.1.6(java.sun.com/docs/books/jls/third_edition/html/)中定义,而javadoc标记则不是。因此注释是语言的一部分。如果您决定编写自己的Java编译器,则可以忽略javadoc标记,但必须识别注释。

@ ShaMan-H_Fel我相信javadoc模型也有效。因为它是Java 5之前的唯一选择,它确实有效。当您使用@deprecated javadoc标记标记方法时(在Java 4-中),编译器将方法(类,字段)标记为已弃用,并且IDE显示警告,即使没有可用的源也是如此。

因为缺少一些小的解释

在这样的方法上使用@Deprecated注释

/**

* @param basePrice

*

* @deprecated  reason this method is deprecated

*              {will be removed in next version}

*              use {@link #setPurchasePrice()} instead like this:

*

*

*

[cc lang="java"]

* getProduct().setPurchasePrice(200)

*

*

*/

@Deprecated

public void setBaseprice(int basePrice) {

}

[/cc]

记得解释一下:

为什么不再推荐这种方法。使用它时会出现什么问题。提供有关此事的讨论的链接(如果有的话)。 (记得将线条分开以便于阅读

什么时候会被删除。 (让您的用户知道,如果他们决定坚持旧方式,他们仍然可以依赖这种方法)

提供解决方案或链接到您推荐的方法{@link #setPurchasePrice()}

不应该是,而不是?

@ argh1969,对!不记得从那时起我从哪里获得模板。但我可以确认这两个版本都有效。虽然我编辑赞成标准。

你可以做两件事:

将@Deprecated注释添加到方法中,然后

将@Deprecated标记添加到方法的javadoc

你应该两个都做!

引用关于这个主题的java文档:

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

这意味着除了注释之外,强烈建议使用javadoc注释,而不是替代!这就是为什么放两者总是更好的原因。

@ morellet.d感谢你指出这一点。我现在基本上重写了我的答案(我没有仔细阅读这份文件!)。干杯

使用注释@Deprecated作为您的方法,您还应该在javadoc中提及它。

链接现在已经破裂

看一下@Deprecated注释。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值