查找 Java 文档弃用消息

Finding the Java docs deprecated messages

如何导航到 Java 文档中有关已弃用 class 的信息性消息?

我在其他答案中看到过类似的内容:

来自 StringTokenizer 文档:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

但是如何在 Java 文档中找到这条信息性消息?我只能看到顶部的 DEPRECATED link。我需要额外的信息,这样我才能理解它被弃用的原因并使用它的替代品。

没有任何可靠的方法可以做到这一点。您找到的文本是关于 class 的非正式建议。 class 并未正式弃用。弃用意味着在 Java 上下文中非常具体。比"use is discouraged in new code".

强多了

如果 class 或 class 成员 确实 在 Java 中弃用,您将在 @Deprecated 中看到注释源代码。这就是Java工具链要注意的地方。在适当的情况下,可能还会有一个 @deprecated javadoc 标记,其中包含对备选方案的解释和建议。这就是在 javadocs 中变成 "Deprecated:" 标题的内容。

要找到像您确定的示例这样的非正式建议,可能需要搜索 Java 库代码库或 javadoc 树以查找 "legacy" 和 "not recommended" 等关键短语。然后您需要手动过滤掉误报。

大多数情况下,class 被弃用的原因是找到了更好的替代方案,或者不再需要它。

至于你提到的deprecated link at the top,我假设你的意思是:https://docs.oracle.com/javase/8/docs/api/deprecated-list.html

好吧,正如您所说,这些 您会找到的所有信息,而且它们的描述性也很好。

我们以构造函数Date(int year, int month, int day)为例,如果向下滚动到deprecated constructor section,您可以找到以下内容:

java.sql.Date(int, int, int)

instead use the constructor Date(long date)

并且它对 JDK 中标有 @Deprecated 注释的每个 classes/methods/interfaces 执行相同的操作。

此外,对于 StringTokenizer,如果您在引用的行下方阅读,它会显示以下内容:

The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

String[] result = "this is a test".split("\s");
for (int x=0; x<result.length; x++)
    System.out.println(result[x]);

基本上,StringTokenizer 不再是必需的,因为 String 对象现在有一个 split 方法,这使得 StringTokenizer 无用。