Search code examples
javaintellij-ideasuppress-warnings

SuppressWarnings("deprecation") for one line in java


I am working with threads and using thread.stop(); to end a thread for a temporary solution to a problem I was having. I understand that it is deprecated and should not be used, but how do I suppress the compiler warning for that one line only? I want to continue getting deprecated warnings for the rest of my class, just not that one line. I tried using the code below and got the error "Annotations are not allowed here" on the @SupressWarnings("deprecation") line. What is the correct way to suppress this error?

class Handeler {
   private Thread thread;
   .....
   void stopThread() {
      if(thread!=null && thread.isAlive()) {                 
         @SuppressWarnings("deprecation")
            thread.stop();
      }
   }
}

Solution

  • You can annotate the method instead:

    class Handeler {
       private Thread thread;
       .....
       @SuppressWarnings("deprecation")
       void stopThread() {
          if(thread!=null && thread.isAlive()) {                 
                thread.stop();
          }
       }
    }
    

    This annotation on a single line is not allowed.