Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5738

"@Deprecated" code marked for removal should never be used

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      * Remove this call to a deprecated method, it has been marked for removal.
      * Remove this use of a deprecated [class|field], it has been marked for removal.
      * Remove this use of "xxx"; it is deprecated and has been marked for removal.
      * Don't override this deprecated method, it has been marked for removal.
      Show
      * Remove this call to a deprecated method, it has been marked for removal. * Remove this use of a deprecated [class|field], it has been marked for removal. * Remove this use of "xxx"; it is deprecated and has been marked for removal. * Don't override this deprecated method, it has been marked for removal.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Irrelevant for Languages:
      ABAP, APEX, C#, C, C++, Cobol, CSS, Flex, Go, HTML, JavaScript, Kotlin, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Ruby, Rust, Scala, Solidity, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Level:
      Semantic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • CERT:
      MET02-J.
    • CWE:
      CWE-477

      Description

      Java 9 introduced a flag for the @Deprecated annotation, which allows to explicitly say if the deprecated code is planned to be removed at some point or not. This is done using forRemoval=true as annotation parameter. The javadoc of the annotation explicitly mention the following:

      If true, it means that this API element is earmarked for removal in a future release.
      If false, the API element is deprecated, but there is currently no intention to remove it in a future release.

      While usually deprecated classes, interfaces, and their deprecated members should be avoided rather than used, inherited or extended, those already marked for removal are much more sensitive to causing trouble in your code soon. Consequently, any usage of such deprecated code should be avoided or removed.

      Noncompliant Code Example

      /**
       * @deprecated As of release 1.3, replaced by {@link #Fee}. Will be dropped with release 1.4.
       */
      @Deprecated(forRemoval=true)
      public class Foo { ... }
      
      public class Bar {
        /**
         * @deprecated  As of release 1.7, replaced by {@link #doTheThingBetter()}
         */
        @Deprecated(forRemoval=true)
        public void doTheThing() { ... }
      
        public void doTheThingBetter() { ... }
      
        /**
         * @deprecated As of release 1.14 due to poor performances.
         */
        @Deprecated(forRemoval=false)
        public void doTheOtherThing() { ... }
      }
      
      public class Qix extends Bar {
        @Override
        public void doTheThing() { ... } // Noncompliant; don't override a deprecated method marked for removal
      }
      
      public class Bar extends Foo {  // Noncompliant; Foo is deprecated and will be removed
      
        public void myMethod() {
          Bar bar = new Bar();  // okay; the class isn't deprecated
          bar.doTheThing();  // Noncompliant; doTheThing method is deprecated and will be removed
      
          bar.doTheOtherThing(); // Okay; deprecated, but not marked for removal
        }
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              michael.gumowski Michael Gumowski
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: