如何检测程序中已弃用的方法?
我在网上搜索了一下,我发现的是这样的:
让编译器警告你 您使用了哪些方法的详细信息 已弃用使用
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 thejavac.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法弃用是为了警告程序员该方法目前不是实现所需功能的最佳方法。这是通过使用
@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 tojavac
, it's just tellingjavac
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.
要查找已弃用的方法和类,请按照引用的文本执行操作。您使用“-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.
如果您已为正在使用的类附加了所需的 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.
您可以使用 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.