Lazerpent
Lazerpent

Reputation: 35

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();
      }
   }
}

Upvotes: 2

Views: 2060

Answers (2)

y.bedrov
y.bedrov

Reputation: 6014

You could suppress warning using comment:

void stopThread() {
      if(thread!=null && thread.isAlive()) {

          //noinspection deprecation
          thread.stop();
      }
}

Upvotes: 4

Uwe Allner
Uwe Allner

Reputation: 3467

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.

Upvotes: 3

Related Questions