java忽略安全警告注解@SuppressWarnings


java中注解用法詳解——@SuppressWarnings

一、前言

    注釋類型:
        當你的編碼可能存在警告時,比如安全警告,可以用它來消除。

        api中是這樣描述的:
            指示應該在注釋元素(以及包含在該注釋元素中的所有程序元素)中取消顯示指定的編譯器警告。
            注意,在給定元素中取消顯示的警告集是所有包含元素中取消顯示的警告的超集。
            例如,如果注釋一個類來取消顯示某個警告,同時注釋一個方法來取消顯示另一個警告,那么將在此方法中同時取消顯示這兩個警告。

            根據風格不同,程序員應該始終在最里層的嵌套元素上使用此注釋,在那里使用才有效。
            如果要在特定的方法中取消顯示某個警告,則應該注釋該方法而不是注釋它的類。

  在java編譯過程中會出現很多警告,有很多是安全的,但是每次編譯有很多警告影響我們對error的過濾和修改,我們可以在代碼中加上 @SuppressWarnings(“XXXX”) 來解決

  例如:@SuppressWarnings("deprecation") 表示不顯示使用了不贊成使用的類或方法時的警告。 

  再比如:

  編碼時我們總會發現如下:變量未被使用的警告提示

image-20201225151652573

  上述代碼編譯通過且可以運行,但每行前面的“感嘆號”就嚴重阻礙了我們判斷該行是否設置的斷點了。這時我們可以在方法前添加 @SuppressWarnings("unused") 去除這些“感嘆號”。

  再比如:

image-20201225151730673

  這時我們可以在方法前添加 @SuppressWarnings("resource") 去除這些“感嘆號”。

二、 @SuppressWarings注解

  作用:用於抑制編譯器產生警告信息。

    //示例1——抑制單類型的警告:
        @SuppressWarnings("unchecked")
        public void addItems(String item){
            @SuppressWarnings("rawtypes")
            List items = new ArrayList();
            items.add(item);
        }

    //示例2——抑制多類型的警告:
        @SuppressWarnings(value={"unchecked", "rawtypes"})
        public void addItems(String item){
            List items = new ArrayList();
            items.add(item);
        }

    //示例3——抑制所有類型的警告:
        @SuppressWarnings("all")
        public void addItems(String item){
            List items = new ArrayList();
            items.add(item);
        }

三、注解目標

  通過 @SuppressWarnings 的源碼可知,其注解目標為類、字段、函數、函數入參、構造函數和函數的局部變量。  而專家建議注解應聲明在最接近警告發生的位置。

四、抑制警告的關鍵字

It depends on your IDE or compiler.

Here is a list for Eclipse Galileo:

  • all to suppress all warnings
  • boxing to suppress warnings relative to boxing/unboxing operations
  • cast to suppress warnings relative to cast operations
  • dep-ann to suppress warnings relative to deprecated annotation
  • deprecation to suppress warnings relative to deprecation
  • fallthrough to suppress warnings relative to missing breaks in switch statements
  • finally to suppress warnings relative to finally block that don’t return
  • hiding to suppress warnings relative to locals that hide variable
  • incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • restriction to suppress warnings relative to usage of discouraged or forbidden references
  • serial to suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-access to suppress warnings relative to incorrect static access
  • synthetic-access to suppress warnings relative to unoptimized access from inner classes
  • unchecked to suppress warnings relative to unchecked operations
  • unqualified-field-access to suppress warnings relative to field access unqualified
  • unused to suppress warnings relative to unused code

List for Indigo adds:

  • javadoc to suppress warnings relative to javadoc warnings
  • rawtypes to suppress warnings relative to usage of raw types
  • static-method to suppress warnings relative to methods that could be declared as static
  • super to suppress warnings relative to overriding a method without super invocations

List for Juno adds:

  • resource to suppress warnings relative to usage of resources of type Closeable
  • sync-override to suppress warnings because of missing synchronize when overriding a synchronized method

部分翻譯如下:

@SuppressWarnings(“unchecked”)   // 抑制未檢查的轉化,例如集合沒有指定類型的警告  
@SuppressWarnings(“unused”)	    // 抑制未使用的變量的警告   
@SuppressWarnings(“resource”)	  	// 抑制與使用Closeable類型資源相關的警告   
@SuppressWarnings(“path”)	   // 抑制在類路徑,原文件路徑中有不存在的路徑的警告   
@SuppressWarnings("deprecation")  // 抑制使用了某些不贊成使用的類和方法的警告 
@SuppressWarnings("fallthrough")   // 抑制switch語句執行到底沒有break關鍵字的警告   
@SuppressWarnings("serial")	 // 抑制某類實現Serializable,但是沒有定義serialVersionUID,這個需要但是不必須的字的警告   
@SuppressWarnings("rawtypes")    // 抑制沒有傳遞帶有泛型的參數的警告   
@SuppressWarnings("all")        // 抑制全部類型的警告


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM