如何检测程序中已弃用的方法?

发布于 2024-08-28 00:06:19 字数 246 浏览 7 评论 0原文

我在网上搜索了一下,我发现的是这样的:

让编译器警告你 您使用了哪些方法的详细信息 已弃用使用 javac.exe -deprecation 开关。然后在 Javadoc 中查找已弃用的方法 找出推荐的替代品。 有时你只需要重命名。 有时替代品效果很好 不同。

但我不太明白它是如何工作的,有人可以帮助我吗?

I've searched through the web and what I've found out is this:

To make the compiler warn you of the
details of which methods you used that
were deprecated use the javac.exe
-deprecation switch. Then look in the Javadoc for the deprecated methods to
find out the recommended replacements.
Sometimes you just have to rename.
Sometimes the replacements work quite
differently.

But I'm not really understand how it works, can anybody help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千仐 2024-09-04 00:06:19

方法弃用是为了警告程序员该方法目前不是实现所需功能的最佳方法。这是通过使用 @deprecated javadoc 注释来完成的。当您对 javac 使用 -deprecated 标志时,它只是告诉 javac 在看到这些 javadoc 注释时发出警告。它告诉您使用已弃用方法的行号以及已弃用方法的名称。

从那里,您可以查看 Sun(呃...Oracle)站点上的 JDK 的 javadoc,以了解获得所需功能的建议。有时,替换涉及进行多个方法调用,而过去您只需要进行一次方法调用。通常,文档可以很好地为您提供示例,并在出现新的做事方式时为您指明正确的方向。

Method deprecation is done to warn programmers that the method is not currently the best one to use for the functionality that's desired. It's done by using the @deprecated javadoc annotation. When you use the -deprecated flag to javac, it's just telling javac to raise a warning when it sees these javadoc annotations. It tells you the line number where you used the deprecated method, and the name of the deprecated method.

From there, it's up to you to look at the JDK's javadoc on Sun's (er...Oracle's) site to see what the recommendations are for getting the functionality you desire. Sometimes the replacement involves making multiple method calls, where you would have made just one in the past. Usually the documentation is good about giving you examples, and pointing you in the right direction when there's a new way of doing things.

掩饰不了的爱 2024-09-04 00:06:19

要查找已弃用的方法和类,请按照引用的文本执行操作。您使用“-deprecation”开关进行编译,有问题的方法/类将显示为编译警告消息。 (如果您使用的是 IDE,则弃用警告将以其他方式启用/禁用。)

如果您实际上询问编译器如何知道方法或类已弃用,答案是类或方法被标记为通过将“@deprecated”标签放入相应的 javadoc 注释中来弃用。 java 编译器会注意到这一点,并在编译该类时在字节码文件中设置一个属性。然后,当您尝试在代码中使用该类/方法时,java 编译器会读取字节码,记录该属性并输出警告消息。

弃用旨在向开发人员强烈暗示不应再使用有问题的方法或类。已弃用的类或方法通常存在一些缺陷(主要或次要),在不破坏现有代码的情况下无法修复这些缺陷。相反,已经实现了不兼容的替换,需要您对源代码进行一些更改。

理论上,已弃用的类和方法可能会在未来的版本中删除。虽然 Sun 很少(如果有的话)这样做,但第三方库开发人员更愿意在清理 API 时删除已弃用的方法。

To find identify the deprecated methods and classes, you do what the quoted text says. You compile with the "-deprecation" switch and the offending methods/classes will be shown as compilation warning messages. (If you are using an IDE, then the deprecation warnings will be enabled / disabled some other way.)

If you are actually asking how the compiler knows that a method or class is deprecated, the answer is that a class or method is marked as deprecated by putting a "@deprecated" tag into the corresponding javadoc comment. The java compiler notes this, and sets an attribute in the bytecode file when it compiles the class. Then, when you try to use the class/method in your code, the java compiler reads the bytecodes, notes the attribute and outputs the warning message.

Deprecation is intended as a strong hint to the developer that the method or class in question should no longer be used. A deprecated class or method typically has some flaw in it (major or minor) that could not be fixed without breaking existing code. Rather, a non-compatible replacement has been implemented that requires you to make some changes to your source code.

In theory, deprecated classes and methods may be removed in future releases. While Sun rarely (if ever) does this, third-party library developers are more willing to remove deprecated methods when cleaning up their APIs.

地狱即天堂 2024-09-04 00:06:19

如果您已为正在使用的类附加了所需的 javadoc,则可以使用任何 IDE(例如 eclipse),它可以选择在编程时显示已弃用的方法。

You can use any IDE like eclipse which has a option of showing the deprecated methods while programming if you have attached the required javadoc for the classes which you are using.

舂唻埖巳落 2024-09-04 00:06:19

您可以使用 NetBeans IDE。它将通过点击该方法来显示已弃用的方法,您还可以查看原因和替代方法。

You can use NetBeans IDE. It will show the deprecated methods by strike that method and also you can view the reason and alternative methods for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文