Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i detect the deprecated methods in a program?

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?

like image 754
keitamike Avatar asked Apr 06 '10 06:04

keitamike


2 Answers

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.

like image 133
Rob Heiser Avatar answered Oct 06 '22 01:10

Rob Heiser


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.

like image 23
Stephen C Avatar answered Oct 05 '22 23:10

Stephen C