日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

去除编译警告@SuppressWarnings注解用法详解(转)

發(fā)布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 去除编译警告@SuppressWarnings注解用法详解(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用:
@SuppressWarnings(“”)
@SuppressWarnings({})
@SuppressWarnings(value={})

編碼時我們總會發(fā)現(xiàn)如下變量未被使用的警告提示:

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

@SuppressWarnings("unused")?去除這些“感嘆號”。

一.@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?的源碼可知,其注解目標為類、字段、函數(shù)、函數(shù)入參、構造函數(shù)和函數(shù)的局部變量。

?而大家建議注解應聲明在最接近警告發(fā)生的位置。?

二.抑制警告的關鍵字

關鍵字用途
allto suppress all warnings
boxing?to suppress warnings relative to boxing/unboxing operations
castto suppress warnings relative to cast operations
dep-annto suppress warnings relative to deprecated annotation
deprecationto suppress warnings relative to deprecation
fallthroughto suppress warnings relative to missing breaks in switch statements
finally?to suppress warnings relative to finally block that don’t return
hidingto suppress warnings relative to locals that hide variable
incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)
nlsto suppress warnings relative to non-nls string literals
nullto suppress warnings relative to null analysis
rawtypesto suppress warnings relative to un-specific types when using generics on class params
restrictionto suppress warnings relative to usage of discouraged or forbidden references
serialto suppress warnings relative to missing serialVersionUID field for a serializable class
static-accessto suppress warnings relative to incorrect static access
synthetic-access?to suppress warnings relative to unoptimized access from inner classes
uncheckedto suppress warnings relative to unchecked operations
unqualified-field-accessto suppress warnings relative to field access unqualified
unusedto suppress warnings relative to unused code

?

  • 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(抑制確在switch中缺失breaks的警告)
  • finally?to suppress warnings relative to finally block that don’t return (抑制finally模塊沒有返回的警告)
  • 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)(忽略沒有完整的switch語句)
  • nls?to suppress warnings relative to non-nls string literals(忽略非nls格式的字符)
  • null?to suppress warnings relative to null analysis(忽略對null的操作)
  • rawtypes?to suppress warnings relative to un-specific types when using generics on class params(使用generics時忽略沒有指定相應的類型)
  • 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(忽略在serializable類中沒有聲明serialVersionUID變量)
  • static-access?to suppress warnings relative to incorrect static access(抑制不正確的靜態(tài)訪問方式警告)
  • synthetic-access?to suppress warnings relative to unoptimized access from inner classes(抑制子類沒有按最優(yōu)方法訪問內部類的警告)
  • 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 ?(抑制沒被使用過的代碼的警告)

?

原文地址

轉載于:https://www.cnblogs.com/suruozhong/p/6264241.html

總結

以上是生活随笔為你收集整理的去除编译警告@SuppressWarnings注解用法详解(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。