關於java中的批註@SuppressWarnings

來源:互聯網
上載者:User

標籤:

J2SE提供的最後一個批註是@SuppressWarnings.該批註的作用是給編譯器一條指令,告訴它對批註的代碼元素內部的某些警告保持沉默。

背景:J2SE5.0為java語言添加了幾個特性和新的警告,並承諾將來會有更多的警告,您可以為 "javac" 添加 -Xlint 參數來控制是否報告這些警告。

預設情況下:sun編譯器以簡單的兩行的形式輸出警告。通過添加 -Xlint:keyword 標記(如:-Xlint:finally), 您可以獲得關鍵字keyword類型的完整說明。
通過在關鍵字前添加破折號 -Xlint:-finally,您可以取消警告。

目前關鍵字清單:

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
rawtypes to suppress warnings relative to un-specific types when using generics on class params
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

deprecation 使用了不贊成使用的類或方法時的警告
unchecked 執行了未檢查的轉換時的警告,例如當使用集合時沒有用泛型 (Generics) 來指定集合儲存的類型。
fallthrough 當 Switch 程式塊直接通往下一種情況而沒有 Break 時的警告。
path 在類路徑、源檔案路徑等中有不存在的路徑時的警告。
serial 當在可序列化的類上缺少 serialVersionUID 定義時的警告。
finally 任何 finally 子句不能正常完成時的警告。
all 關於以上所有情況的警告

@SuppressWarnings 批註允許你選擇性的取消特點程式碼片段(類或方法)中的警告。是這樣想的:當你看到警告時,你將調查它,如果你確定它不是問題,您就可以通過用@SuppressWarnings批註,以使你不會再看到警告。雖然聽起來似乎會屏蔽潛在的錯誤,但實際上它將提高代碼的安全性。(每一個警告都將值得注意)。

下面是使用 @SuppressWarnings 來取消 deprecation 警告的一個例子:

 

@SuppressWarnings 批註接受一個"value"變數,該變數是一個字串數組,它指示將取消的警告。合法字串的集合隨這編譯器的變化而變化,但在 JDK 上,可以傳遞給-Xlint 的是相同的關鍵字集合,並且要求編譯器忽略任何它們不能識別的關鍵字。

因為 @SuppressWarnings 批註僅接收一個參數,並為該參數使用了特殊的名稱 "value",所以您可以選擇省略 value=,作為一種方便的縮寫:

public class DeprecatedUser2 {@SuppressWarnings({"deprecation"})public static void main(String[] args) {DeprecatedExample2.foo();}}

 

您可以將單個數組參數中的任意數量的字串值傳遞給批註,並在任何層級上放置批註。例如,以下範例程式碼指示將取消整個類的 deprecation 警告,而僅在 main() 方法代碼內取消 unchecked 和 fallthrough 警告:

import java.util.*;@SuppressWarnings({"deprecation"})public class NonGenerics {@SuppressWarnings({"unchecked","fallthrough"})public static void main(String[] args) {Runtime.runFinalizersOnExit();List list = new ArrayList();list.add("foo");}public static void foo() {List list = new ArrayList();list.add("foo");}}

 

關於java中的批註@SuppressWarnings

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.