Powered by SmartDoc

using Jlint

Fri Nov 02 00:47:04 JST 2001
TAKAHASHI, Toru
torutk@alles.or.jp
http://www.alles.or.jp/~torutk/oojava/
Jlintは、プログラムの文法的検証を行うanticコマンドと意味的検証を行うjlintコマンドの2つで構成されます。

目次

1 Jlintのインストール

jlintを入手し、実行可能にするまでの設定を述べます。

1.1 Jlintの入手

まずjlintを入手します。入手先は、ツール/ライブラリリンクのページのテスト・デバッグ節を参照して下さい。

1.2 Jlintの展開

入手したアーカイブファイルを適当な場所に展開します。ここでは、/java/jlint(D:\java\jlint)にインストールするように展開します。

ソースコードで配布されているので、各自コンパイルして実行ファイルを構築する必要がありますが、Windows OS用には既にコンパイル済みの実行ファイルが用意されています。

図 1.2.1 Jlintの展開(Cygwin)
jlint$ cd /java
java$ unzip jlint.zip
Archive:  jlint.zip
   creating: jlint/
  inflating: jlint/antic.c
  inflating: jlint/antic.exe
  inflating: jlint/jlint.cpp
  inflating: jlint/jlint.d
  inflating: jlint/jlint.exe
  inflating: jlint/jlint.h
  inflating: jlint/jlint.msg
  inflating: jlint/make.bat
  inflating: jlint/makefile
  inflating: jlint/makefile.mvc
  inflating: jlint/ReadMe.htm
  inflating: jlint/test.java
  inflating: jlint/VENDINFO.DIZ
/java$ 

UNIX用のメイクファイルがmakefile、Windows(VC++)用のメイクファイルがmakefile.mvcです。

1.3 Jlintの設定

jlintには2つのコマンドanticとjlintがあります。環境変数PATHを通す等を行い、コマンドを実行できるようにします。Cygwin(bash)では、環境変数PATHを設定したくない場合にaliasを定義することによって起動できるようにすることもできます。

alias antic='/java/jlint/antic.exe'
alias jlint='/java/jlint/jlint.exe'

2 Jlintの実行

Jlintは、2つのコマンドantic、jlintからなります。

2.1 anticコマンドの実行

anitcコマンドは、java(c/c++)のソースコードファイルを検証します。

2.1.1 anticコマンド書式

antic [-java] [-tab <tabsize>] [-relax-else] file|directory [file|directory]...

2.1.2 anticコマンド実行例

ソースファイルのあるディレクトリにおいて下記のように実行します。

図 2.1.2.1 anticの実行例(Cygwin)
jlint$ antic .
.\test.java:15:13: May be wrong assumption about 
ELSE branch association
.\test.java:19:9: Nested comments
.\test.java:95:21: May be wrong assumption about 
IF body
.\test.java:97:15: Possible miss of BREAK before 
CASE/DEFAULT
.\test.java:99:21: May be wrong assumption about 
loop body
.\test.java:100:26: May be wrong assumption about 
shift operator priority
.\test.java:105:17: May be 'l' is used instead of 
'1' at the end of integer constant
.\test.java:111:15: May be '=' used instead of '=='
Verification completed: 8 reported messages

上記は、Jlintに添付されている検証サンプルソースファイルtest.javaを対象にしたものです。

2.2 jlintコマンドの実行

jlintコマンドは、javaのバイトコードファイルを検証します。

2.2.1 jlintコマンドの書式

jlint オプション|バイトコードファイル
表 2.2.1.1 jlintオプション
オプション 概要
-source パス バイトコードとソースファイルが異なるディレクトリにあるときに指定
-history ファイル
-max_shown_paths 数値
-help
(+-)verbose
(+-)message_category
(+-)all
(+-)message_code

2.2.2 jlintコマンドの実行例

図 2.2.2.1 jlintの実行例
jlint$ jlint .
test.java:7: Result of operation & is always 0
test.java:7: Comparison always produce the same result
test.java:9: Comparison always produce the same result
test.java:12: Inequality comparison can be replaced with equality comparison
test.java:23: Value of referenced variable '???' may be NULL
test.java:29: Result of operation & is always 0
test.java:29: Comparison always produce the same result
test.java:34: Comparison always produce the same result
test.java:36: Compared expressions can be equal only when both of them are 0
test.java:37: Comparison always produce the same result
test.java:39: Range of expression value has no intersection with short type doma
in
test.java:44: Data can be lost as a result of truncation to int
test.java:47: Result of operation & is always 0
test.java:47: Comparison always produce the same result
test.java:48: Shift << count range [2,33] is out of domain
test.java:58: Index [0,9] may be out of array bounds
test.java:59: Index [-1,8] may be out of array bounds
test.java:63: Index [10,10] is out of array bounds
test.java:66: Array length [-1,2147483646] may be less than zero
test.java:68: Array length [-2147483648,-1] is less than zero
test.java:81: Compare strings as object references
test.java:84: Shift << count range [1,32] is out of domain
test.java:85: Result of operation >> is always 0
test.java:88: Switch case constant 4 can't be produced by switch expression
test.java:90: Zero operand for * operation
test.java:90: Result of operation * is always 0
test.java:93: Comparison always produce the same result
test.java:98: Comparison always produce the same result
test.java:100: Shift << with count <= than -2
test.java:103: May be type cast is not correctly applied
test.java:104: Range of expression value has no intersection with short type dom
ain
test.java:107: Zero operand for * operation
test.java:107: Result of operation * is always 0
test.java:122: Method finalize() doesn't call super.finalize()
test.java:126: Value of referenced variable '???' may be NULL
test.java:127: Method notify is called from non-synchronized method
test.java:159: Method wait() can be invoked with monitor of other object locked
test.java:195: Call sequence to method Thread1.f() can cause deadlock in wait()
test.java:153: Method Thread1.run() implementing 'Runnable' interface is not syn
chronized
test.java:170: Synchronized method Thread1.f() is overridden by non-synchronized
 method of derived class 'Thread1_derived'
test.java:1: Component 'x' in class 'derived_class' shadows one in base class 'b
ase_class'
test.java:125: Method base_class.refer(derived_class) is not overridden by metho
d with the same name of derived class 'derived_class'
test.java:78: Method base_class.foo(int) is not overridden by method with the sa
me name of derived class 'derived_class'
test.java:147: Field 'x' of class 'Thread2' can be accessed from different threa
ds and is not volatile
test.java:132: Method derived_class.equal(boolean, boolean) can be called from d
ifferent threads and is not synchronized
test.java:52: Method base_class.increment(base_class) can be invoked with NULL a
s 0 parameter and this parameter is used without check for NULL
test.java:190: Loop 1/1: invocation of method Thread1_derived.f() forms the loop
 in class dependency graph
test.java:171: Loop 1: invocation of synchronized method Thread1.h() can cause d
eadlock
test.java:164: Loop 1: invocation of synchronized method Thread2.h(Thread1_deriv
ed) can cause deadlock
test.java:180: Loop 2: invocation of synchronized method Thread1.f() can cause d
eadlock
test.java:148: Loop 2: invocation of synchronized method Thread2.f() can cause d
eadlock
test.java:183: Loop 3: invocation of synchronized method Thread1.foo() can cause
 deadlock
test.java:148: Loop 3: invocation of synchronized method Thread2.f() can cause d
eadlock
test.java:195: Loop 4: invocation of synchronized method Thread1.f() can cause d
eadlock
test.java:148: Loop 4: invocation of synchronized method Thread2.f() can cause d
eadlock
test.java:131: Loop 5: invocation of synchronized method derived_class.monitor(d
erived_class) can cause deadlock
Verification completed: 56 reported messages

3 anticによる文法的検証

anticは、ソースファイルを文法的に解析し、問題のありそうな点をレポートします。