@SuppressWarnings({ "unchecked", "static-access" })

  • Non-static access code to static member.
    • デフォルトレベル:Warning
    • メッセージ: The static field ${クラス名}.${クラス変数名} should be accessed in a static way
    • 抑止:@SuppressWarnings("static-access")
    • 代替手段:コードを直そう。

  • Indirect access to static member.
    • デフォルトレベル:Ignore
    • メッセージ: The static field ${クラス名}.${変数名} should be accessed directly
    • 抑止:@SuppressWarnings("static-access")
    • 代替手段:コードを直そう。


  • Undocumented empty block
    • デフォルトレベル:Ignore
    • メッセージ: Empty block should be documented
    • 抑止:なし
    • 代替手段:コメントを入れてください。

  • Access to a non-accessible member of an enclosing type
    • デフォルトレベル:Ignore
    • メッセージ: Read access to enclosing field ${クラス名}.${変数名} is emulated by a synthetic accessor method
    • 抑止:@SuppressWarnings("synthetic-access")
    • 代替手段:可能であれば、変数にfinal修飾子を付けるだとかアクセス修飾子を考える。

  • Method with a constructor name
    • デフォルトレベル:Warning
    • メッセージ:this method has a constructor name
    • 抑止:なし。
    • 代替手段:素直にメソッド名を変えます。


  • Non-externalized strings (missing/unused $NON-NLS$ tag)
    • デフォルトレベル:Ignore
    • メッセージ(missing):Non-externalized string literal; it should be followed by //$NON-NLS-<n>$
    • メッセージ(unused):Unnecessary $NON-NLS$ tag
    • 抑止:@SuppressWarnings("nls")
    • 代替手段(unused):消すよ普通。
    • 代替手段(missing):外部化ウィザードでやる。
    • $NON-NLS$の数字の秘密はこういうことだよ。String[] a = {"",""}; //$NON-NLS-1$ //$NON-NLS-2$

Potential programming problems


  • Serializable class without serialVersionUID
    • デフォルトレベル:Warning
    • メッセージ:The serializable class ${クラス名} does not declare a static final serialVersionUID field of type long
    • 抑止:@SuppressWarnings("serial")
    • 代替手段:生成シリアルバージョンを入れる。

  • Assignment has no effect (e.g. 'x = x')
    • デフォルトレベル:Warning
    • メッセージ:The assignment to variable object has no effect
    • 抑止:なし。
    • 代替手段:コードを直そう。きっとそのコードは不要だ。

  • Possible accidental boolean assignment (e.g. 'if(a = b)')
    • デフォルトレベル:Ignore
    • メッセージ:Possible accidental assignment in place of a comparison. A condition expression should not be reduced to an assignment
    • 抑止:なし
    • 代替手段:コードが合ってれば直す必要はないが、ずっと警告出てしまうから、出ない書き方すれば?

  • 'finally' does not complete normally
    • デフォルトレベル:Warning
    • メッセージ:finally block does not complete normally
    • 抑止:@SuppressWarnings("finally")
    • 代替手段:特になし。致命的なバグの可能性もあるので見逃して良い警告じゃない。

  • Empty statement
    • デフォルトレベル:Ignore
    • メッセージ:Unnecessary semicolon
    • 抑止:なし
    • 代替手段:セミコロンを消す。

  • Using a char array in string concatenation
    • デフォルトレベル:Warning
    • メッセージ:Must explicitly convert the char[] to a String
    • 抑止:なし
    • 代替手段:まあ、StringBuilderとかで連結しましょう。

  • Hidden catch block
    • デフォルトレベル:Warning
    • メッセージ:Unreachable catch block for ${例外クラス名}. Only more specific exceptions are thrown and handled by previous catch block(s).
    • 抑止:@SuppressWarnings("hiding")
    • 代替手段:特に思いつかない。

  • Inexpect type match for vararg arguments
    • デフォルトレベル:Warning
    • メッセージ:The argument of type null should explicitly be cast to ${可変長クラス名}[] for the invocation of the varargs method foo(${可変長クラス名}...) from type ${クラス名}. It could alternatively be cast to ${可変長クラス名} for a varargs invocation
    • 抑止:なし
    • 代替手段:メッセージに書いてる通りキャストする。

  • Boxing and unboxing conversions
    • デフォルトレベル:Ignore
    • メッセージ(boxing):The expression of type ${プリミティブ型} is boxed into ${ラッパー型}
    • メッセージ(unboxing):The expression of type ${ラッパー型} is unboxed into ${プリミティブ型}
    • 抑止:@SuppressWarnings("boxing")
    • 代替手段:明示的に書くこと。

  • Enum type constant not covered on 'switch'
    • デフォルトレベル:Ignore
    • メッセージ:The enum constant ${他の定数名} needs a corresponding case label in this enum switch on ${Enumクラス名}
    • 抑止:@SuppressWarnings("incomplete-switch")
    • 代替手段:なし。必要ないのを全部書くのは馬鹿らしいよね。

  • 'switch' case fall-through
    • デフォルトレベル:Ignore
    • メッセージ:Switch case may be entered by falling through previous case. If intended, add a new comment //$FALL-THROUGH$ on the line above
    • 抑止:@SuppressWarnings("fallthrough")
    • 代替手段:メッセージにある通りか、またはバグならbreakを書くとか。

  • Null pointer access
    • デフォルトレベル:Warning
    • メッセージ:Null pointer access: The variable i can only be null at this location
    • 抑止:@SuppressWarnings("null")
    • 代替手段:たぶんバグ、きっとバグ、だから直そう。

  • Potential null pointer access
    • デフォルトレベル:Warning
    • メッセージ:Potential null pointer access: The variable i may be null at this location
    • 抑止:@SuppressWarnings("null")
    • 代替手段:その地点までにnullである場合は例外にしちゃうかもね。

  • Comparing identical values ('x == x')
    • デフォルトレベル:Warning
    • メッセージ:Comparing identical expressions
    • 抑止:なし
    • 代替手段:同じ物を比較してんじゃないよ!ってことだよ。

  • Missing synchronized modifier on inherited method
    • デフォルトレベル:Ignore
    • メッセージ:The method ${クラス名}.{メソッドシグニチャ} is overriding a synchronized method without being synchronized
    • 抑止:なし
    • 代替手段:synchronizedを付ける。

  • Class overrides 'equals()' but not 'hashCode()'
    • デフォルトレベル:Ignore
    • メッセージ:The type ${クラス名} should also implement hashCode() since it overrides Object.equals()
    • 抑止:なし
    • 代替手段:hashCodeをオーバーライドする。とりあえずフィールドから自動作成するクセを付けよう。

  • Dead Code (e.g. 'if(false)')
    • デフォルトレベル:Warning
    • メッセージ:Dead code
    • 抑止:なし
    • 代替手段:普通に消す。

Name shadowing and conflicts


  • Field Declaration hides another field or variable
    • デフォルトレベル:Ignore
    • メッセージ:The field ${クラス名}.${フィールド名} is hiding a field from type ${クラス名}
    • メッセージ:The field ${クラス名}.${フィールド名} is hiding another local variable defined in an enclosing type scope
    • 抑止:@SuppressWarnings("hiding")
    • 代替手段:スーパークラスおよびサブクラスのフィールドのアクセス修飾子が妥当か考えてみる。

  • Local variable declaration hides another field or variable
    • デフォルトレベル:Ignore
    • メッセージ:The local variable name is hiding a field from type ${クラス名}
    • 抑止:@SuppressWarnings("hiding")
    • 代替手段:どちらかの名前を変える。

  • Local variable declaration hides another field or variable
    • Include constructor or setter method parameters
    • デフォルトレベル:Ignore
    • メッセージ:The parameter name is hiding a field from type ${クラス名}
    • 抑止:@SuppressWarnings("hiding")
    • 代替手段:どちらかの名前を変える。パラメーターに@SuppressWarningsを付けることはお勧めしません。ひどく読みにくくなります。

  • Type parameter hides another type
    • デフォルトレベル:Warning
    • メッセージ:The type parameter ${型パラメータ名} is hiding the type ${クラス名}
    • 抑止:@SuppressWarnings("hiding")
    • 代替手段:クラス名の変更よりは、型パラメータの名前を変更した方がいい。
    • おかしなことに、総称型クラス<E>とそのインナークラス(E)で試したら、この警告は出なかった。変だ。バグ

  • Method does not override package visible method
    • デフォルトレベル:Warning
    • メッセージ:The method ${クラス名}.${メソッド名}() does not override the inherited method from ${クラス名} since it is private to a different package
    • 抑止:なし
    • 代替手段:なんだかパッケージプライベートにするように促されるんだけど意味ないよ。

  • Interface method conflicts with protected 'Object' method
    • デフォルトレベル:Warning
    • メッセージ:The return type is incompatible with Object.clone(), thus this interface cannot be implemented
    • 抑止:なし
    • 代替手段:なし。実装しようとするとコンパイルエラーが出る。だったら警告じゃなくてエラーにしとけよ。

Deprecated and restricted API


  • Deprecated API
    • Signal use of deprecated API inside deprecated code
    • Signal overriding or implementing deprecated method
    • デフォルトレベル:Warning
    • メッセージ:The method ${メソッド名} from the type ${クラス名} is deprecated
    • 抑止:@SuppressWarnings("deprecation")
    • 代替手段:なし。

  • Forbidden reference (access rule)
    • デフォルトレベル:Error
    • 以下はメッセージ例、汎化して書くのが大変だったのでそのまま書きます。これはsun.nio.cs.ext.SJISデフォルトコンストラタを使ってnewしています。
    • メッセージ:Access restriction: The constructor SJIS() is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre6\lib\charsets.jar
    • メッセージ:Access restriction: The type SJIS is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre6\lib\charsets.jar
    • 抑止:@SuppressWarnings("restriction")
    • 代替手段:そもそもがエラーですから、JDKを実装をしてる人くらいしか使わないはずです。

アクセスルールとは何か!?

eclipseWikiでは?で飛ばしていたのでちょっと僕の実験結果を書いておくよ。

ビルドパスの構成で、JREシステムライブラリーは必ず入れてると思います。

では、そのライブラリを展開すると、いくつかのJarが表示されるでしょう。

そのJarを展開すると

  1. Source attachment
  2. JavaDoc location
  3. Native library location
  4. Access rules

ってのが出てきます。そうです、ここです!

JREが提供するものは、だいたいjavaとかjavaxで始まる類のものは、

みんなアクセス可能であると書いています。

アクセス可能であるというinclude条件にマッチするもの以外をアクセス不許可にしています。

どのJarにも現在は160のルールが設定されているはずです。

アクセスルールは3種類の分類があります。

  1. Forbidden
  2. Discouraged
  3. Accessible

JREとかのは、AccessibleとForbiddenしか使ってないはずです。

クラスのアクセス修飾子とは関係がないっす。


  • Discouraged reference (access rules)
    • デフォルトレベル:Warning
    • 以下の例は、私の別のJavaプロジェクトをビルドパスに含め、そのプロジェクトにDiscouragedなアクセスルールを設定して行った場合の例を元にしています。他に、importや変数の型にも反応します。
    • メッセージ:Discouraged access: The type ${クラス名} is not accessible due to restriction on required project ${プロジェクト名}
    • 抑止:@SuppressWarnings("restriction")
    • 代替手段:なし。メソッドにSuppressWarnignsを付けても、たいていの場合はimport分にも付けるためクラスにSuppressWarningsを付けることになり、メソッドに付けた方は不要なアノテーションとみなされるでしょう。

Unnecessary Code


  • Local variable is never read
    • デフォルトレベル:Warning
    • メッセージ:The local variable ${ローカル変数名} is never read
    • 抑止:@SuppressWarnings("unused")
    • 代替手段:要らないなら消しておこう。

  • Parameter is never read
    • Ignore in overriding and implementing methods
    • Ignore parameters documented with '@param' tag
    • デフォルトレベル:Ignore
    • メッセージ:The parameter a is never read
    • 抑止:@SuppressWarnings("unused")
    • 代替手段:なし。消すとシグニチャーが変わるので影響がデカイ。

  • Unused Import
    • デフォルトレベル:Warning
    • メッセージ:The import ${クラス名} is never used
    • 抑止:@SuppressWarnings("unused")
    • 代替手段:クラスにunusedが付くよりは、import編成した方が良い。

  • Unused local or private member
    • デフォルトレベル:Warning
    • メッセージ:The type ${ローカルクラス名} is never used locally
    • メッセージ:The type ${privateなインナークラス名} is never used locally
    • メッセージ:The field ${クラス名}.${private変数名} is never read locally
    • 抑止:@SuppressWarnings("unused")
    • 代替手段:使わないなら消そう。

  • Redundant null check
    • デフォルトレベル:Ignore
    • メッセージ:Null comparison always yields false: The variable a cannot be null at this location
    • 抑止:@SuppressWarnings("null")
    • 代替手段:絶対にnullにならないので判定を消した方が良い。

  • Unnecessary 'else' statement
    • デフォルトレベル:Ignore
    • メッセージ:Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally
    • 抑止:なし
    • 代替手段:elseではなくす。けっこう人によって好みが現れるかもしれない。

  • Unnecessary cast or 'instanceof' operation
    • デフォルトレベル:Ignore
    • メッセージ:Unnecessary cast from ${クラス名} to ${クラス名}
    • メッセージ:The expression of type ${クラス名} is already an instance of type ${クラス名}
    • 抑止:@SuppressWarnings("cast")
    • 代替手段:大きな害はないけれど、不要なものは消した方が良い。

  • Unnecessary declaration of thrown exception
    • Ignore in overriding and implementing method
    • Ignore exceptions documented with '@throws' or '@exception' tags
    • Ignore 'Exception' and 'Throwable'
    • デフォルトレベル:Ignore
    • メッセージ:The declared exception ${例外クラス名} is not actually thrown by the method ${メソッド名} from type ${クラス名}
    • 抑止:@SuppressWarnings("unused")
    • 代替手段:使ってないなら消す。インターフェースからの物でも、範囲を狭めるなら許される。

  • Unused 'break' or 'continue' label
    • デフォルトレベル:Warning
    • メッセージ:The label ${ラベル名} is never explicitly referenced
    • 抑止:@SuppressWarnings("unused")
    • 代替手段:使ってないなら消す。そもそもラベル使うほどの特殊なコードを書く人は少ないだろうが…。
    • 使ったこと無い人が多いから付け加えておく。"break ラベル名;"や"continue ラベル名;"って使うんだよ。2つ以上ネストしたループで使うことになるだろう。少なくても日本人がこんなコードは書いてるところを見たことがない。そうだな、SQL文のパーサー、CSVの解析をする時にこのコーディングを使うとより高速になるだろう。


Generic Types


  • Unchecked generic type operation
    • デフォルトレベル:Warning
    • メッセージ:Type safety: The method ${メソッド名} belongs to the raw type ${クラス名}. References to generic type ${総称型クラス名} should be parameterized
    • 抑止:@SuppressWarnings("unchecked")
    • 代替手段:型パラメータが分かる場合はそれを使用する。

  • Usage of a raw type
    • デフォルトレベル:Warning
    • メッセージ:${クラス名} is a raw type. References to generic type ${総称型クラス名} should be parameterized
    • 抑止:@SuppressWarnings("unchecked")
    • 代替手段:出来るだけraw型を使わないようにする。ただし設計時にかなり考慮しないと完璧に回避することは困難。

  • Generic type parameter declared with final type bound
    • デフォルトレベル:Warning
    • メッセージ:The type parameter ${型パラメータ名} should not be bounded by the final type ${クラス名}. Final types cannot be further extended
    • 抑止:なし
    • 代替手段:extendedな書き方をしない。継承出来ないものを上限におくのだから、そのクラスしか許さないと言っているだけです。

Annotations


  • Missing '@Override' annotation
    • デフォルトレベル:Ignore
    • メッセージ:The method ${メソッド名} of type ${サブクラス名} should be tagged with @Override since it actually overrides a superclass method
    • 抑止:なし
    • 代替手段:足らない@Overrideを足す。

  • Missing '@Deprecated' annotation
    • デフォルトレベル:Ignore
    • メッセージ:The deprecated method ${メソッド名} of type ${クラス名} should be annotated with @Deprecated
    • 抑止:@SuppressWarnings("dep-ann")
    • 代替手段:@Deprecatedを付けた方が良いと思う。


  • Unhandled token in '@SuppressWarnings'
    • デフォルレベル:Warning
    • メッセージ:Unsupported @SuppressWarnings("${ハンドル出来ない文字列}")
    • 抑止:なし
    • 代替手段:クイックフィックスで出てくるのはミススペル程度のものです。


  • Unused '@SuppressWarnings' annotations
    • デフォルトレベル:Warning
    • メッセージ:Unnecessary @SuppressWarnings("${ハンドル出来る文字}")
    • 抑止:なし
    • 代替手段:要らないから消す。



コンパイラのError/Warningから紐解けた@SuppressWarningsの一覧

  • @SuppressWarnings("boxing")
  • @SuppressWarnings("cast")
  • @SuppressWarnings("dep-ann")
  • @SuppressWarnings("deprecation")
  • @SuppressWarnings("fallthrough")
  • @SuppressWarnings("finally")
  • @SuppressWarnings("hiding")
  • @SuppressWarnings("incomplete-switch")
  • @SuppressWarnings("nls")
  • @SuppressWarnings("null")
  • @SuppressWarnings("restriction")
  • @SuppressWarnings("serial")
  • @SuppressWarnings("static-access")
  • @SuppressWarnings("synthetic-access")
  • @SuppressWarnings("unchecked")
  • @SuppressWarnings("unqualified-field-access")
  • @SuppressWarnings("unused")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值