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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With