Rule Definition
The purpose is to avoid to use the deprecated enity as currently good and appropriate new version of the same is available. The deprecated entity may be dropped sometime in near future without any intimation.
Remediation
Check the documentation of the deprecated entity and avoid not to use the same. Some other way can be approached or new method, type or class can be used instead
Violation Code Sample
class Test
{
public void funUsingDerecatedField()
{
value = 10;
}
@Deprecated
int value;
}
public class DeprecatedUse {
public static void main(String[] args) {
Test obj = new Test();
obj.funUsingDerecatedField(); // VIOLATION as function using a deprecated field is called
}
}
Fixed Code Sample
-Sample 1
private void dumpResponse(ServerResponse response) {
try {
String resp = new String(response.getByteArray());
logger.debug("Response Dump: "+resp);
response.setResponseStream(new StringBufferInputStream(resp)); // as StringBufferInputStream is Deprecated the preferred way to create a stream from a string is via the StringReader class.
} catch (CommunicationException e) {
e.printStackTrace();
}
}
---------------------------------------------------------------------------------
public class MyTest {
public static void main(String[] args) {
List list = new List();
list.addItem("MyItemOne"); // addItem is deprecated use add
list.addItem("MyItemTwo"); // addItem is deprecated use add
list.addItem("MyItemThree"); // addItem is deprecated use add
list.clear(); // clear is deprecated use removeall
}
}
Reference
https://stackoverflow.com/questions/9031832/how-to-declare-or-mark-a-java-method-as-deprecated
https://stackoverflow.com/questions/14627313/what-does-deprecated-mean-on-method-parameters-and-local-variables
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.