- Non-static access code to static member.
- Indirect access to static member.
- Unqualified access to instance field
- Undocumented empty block
- デフォルトレベル:Ignore
- メッセージ: Empty block should be documented
- 抑止:なし
- 代替手段:コメントを入れてください。
- Access to a non-accessible member of an enclosing type
- Method with a constructor name
- デフォルトレベル:Warning
- メッセージ:this method has a constructor name
- 抑止:なし。
- 代替手段:素直にメソッド名を変えます。
- Parameter assignment
- Non-externalized strings (missing/unused $NON-NLS$ tag)
Potential programming problems
- Serializable class without serialVersionUID
- 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
- Empty statement
- デフォルトレベル:Ignore
- メッセージ:Unnecessary semicolon
- 抑止:なし
- 代替手段:セミコロンを消す。
- Using a char array in string concatenation
- 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
- 'switch' case fall-through
- Null pointer access
- Potential null pointer access
- Comparing identical values ('x == x')
- デフォルトレベル:Warning
- メッセージ:Comparing identical expressions
- 抑止:なし
- 代替手段:同じ物を比較してんじゃないよ!ってことだよ。
- Missing synchronized modifier on inherited method
- 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
- 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
- Type parameter hides another type
- 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
Deprecated and restricted API
- Deprecated API
- 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を展開すると
ってのが出てきます。そうです、ここです!
JREが提供するものは、だいたいjavaとかjavaxで始まる類のものは、
みんなアクセス可能であると書いています。
アクセス可能であるというinclude条件にマッチするもの以外をアクセス不許可にしています。
どのJarにも現在は160のルールが設定されているはずです。
アクセスルールは3種類の分類があります。
- Forbidden
- Discouraged
- 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
- Parameter is never read
- Unused Import
- デフォルトレベル:Warning
- メッセージ:The import ${クラス名} is never used
- 抑止:@SuppressWarnings("unused")
- 代替手段:クラスにunusedが付くよりは、import編成した方が良い。
- Unused local or private member
- 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の解析をする時にこのコーディングを使うとより高速になるだろう。
- Redundant super interface
Generic Types
- Unchecked generic type operation
- Usage of a raw type
- Generic type parameter declared with final type bound
Annotations
- Missing '@Override' annotation
- Missing '@Deprecated' annotation
- Annotation is used as super interface
- 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")