Non-static access code to static member. デフォルトレベル:Warning メッセージ: The static field ${クラス名}.${クラス変数名} should be accessed in a static way 抑止: @SuppressWarnings( static-access ) 代替手段:コードを直そう。 Indirect a
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")代替手段:コードを直そう。
Unqualified access to instance field
デフォルトレベル:Ignoreメッセージ: Unqualified
access to
the field ${クラス名}.{インスタンス変数名}抑止:@SuppressWarnings("unqualified-field-access")代替手段:thisを付ければ普通に消える。
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抑止:なし。代替手段:素直にメソッド名を変えます。
Parameter assignment
デフォルトレベル:Ignoreメッセージ:The parameter ${パラメータ変数名} should be assigned抑止:なし代替手段:パラメータ変数にfinal付ける習慣を持つと、コンパイルエラーにしてくれるよ。
Non-externalized strings (missing/unused $NON-NLS$ tag)
デフォルトレベル:Ignoreメッセージ(missing):Non-externalized string literal; it should be followed by //$NON-NLS-$メッセージ(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)で試したら、この警告は出なかった。変だ。バグ?
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 codeSignal 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を展開すると
- Source attachment
- JavaDoc location
- Native library location
- Access rules
ってのが出てきます。そうです、ここです!
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
デフォルトレベル:Warningメッセージ:The local variable ${ローカル変数名} is never read抑止:@SuppressWarnings("unused")代替手段:要らないなら消しておこう。
Parameter is never read
Ignore in overriding and implementing methodsIgnore 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 methodIgnore exceptions documented with '@throws' or '@exception' tagsIgnore '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
デフォルトレベル:Ignoreメッセージ:Redundant superinterface ${インターフェース名} for the type ${サブクラス名},
already defined by ${スーパークラス名}抑止:@SuppressWarnings("unused")代替手段:普通はサブクラスにあるのを消す。まあ、意図するところがあるならば、スーパークラスでimplementsしない。
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を付けた方が良いと思う。
Annotation is used as super
interface
デフォルトレベル:Warningメッセージ:The annotation type ${アノテーション名} should not be used as a superinterface for ${インターフェース名}抑止:なし代替手段:間違った使い方だろうから直した方がいいね。
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")