【问题标题】:SuppressWarnings("deprecation") for one line in javaSuppressWarnings("deprecation") 在java中的一行
【发布时间】:2019-05-29 11:36:09
【问题描述】:

我正在使用线程并使用 thread.stop();结束一个线程以临时解决我遇到的问题。我知道它已被弃用且不应使用,但是如何仅针对该一行禁止编译器警告?我想继续为我班的其他人收到弃用的警告,而不是那一行。我尝试使用下面的代码并在 @SupressWarnings("deprecation") 行上收到错误“此处不允许注释”。抑制此错误的正确方法是什么?

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

【问题讨论】:

    标签: java intellij-idea suppress-warnings


    【解决方案1】:

    您可以使用注释来抑制警告:

    void stopThread() {
          if(thread!=null && thread.isAlive()) {
    
              //noinspection deprecation
              thread.stop();
          }
    }
    

    【讨论】:

    • 这似乎是特定于 IDE 的。它会停止编译器(javac 和 ecj)发出的警告吗?
    【解决方案2】:

    您可以改为注释方法:

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

    不允许单行注释。

    【讨论】:

      猜你喜欢
      • 2011-11-15
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多