Java
 Computer >> コンピューター >  >> プログラミング >> Java

Java 9のjdepsツールとは?重要性と主なオプションを徹底解説


jdepsとは?――Javaクラス依存関係アナライザー

jdepsは、Javaに標準搭載されたクラス依存関係分析ツール(Java Class Dependency Analyzer)です。コマンドラインから手軽に実行でき、指定したJavaクラスファイルのパッケージレベルおよびクラスレベルの依存関係を表示できます。

分析対象の入力として指定できるのは、以下のいずれかです。

  • .classファイルへのパス名
  • ディレクトリ
  • JARファイル
  • 完全修飾クラス名(該当するすべてのクラスファイルを解析)

Java 9においてjdepsが重要な理由

Java 9ではプラットフォーム・モジュールシステム(JPMS)が導入され、アプリケーションのモジュール化が本格的に進みました。jdepsを使えば既存コードの依存関係を可視化できるため、モジュール化への移行JDK内部APIの利用箇所の洗い出しに欠かせないツールとなっています。特に

jdepsの場所と基本的な使い方

jdepsはJDK 8からJDKに同梱されており、「%JAVA_HOME%\bin\jdeps.exe」という実行ファイルとして提供されています。環境変数「PATH」に「%JAVA_HOME%\bin」ディレクトリが含まれていれば、次のコマンドで利用可能なすべてのオプション一覧を確認できます。

jdeps --help

jdeps --help の出力例(全オプション一覧)

C:\Program Files\Java\jdk-9.0.4>jdeps --help
Usage: jdeps <options> <path ...>]
<path> can be a pathname to a .class file, a directory, a JAR file.

Possible options include:
  -dotoutput <dir>
  --dot-output <dir>      Destination directory for DOT file output
  -s          -summary    Print dependency summary only.
  -v          -verbose    Print all class level dependences
                          Equivalent to -verbose:class -filter:none.
  -verbose:package        Print package-level dependences excluding
                          dependences within the same package by default
  -verbose:class          Print class-level dependences excluding
                          dependences within the same package by default
  -apionly
  --api-only              Restrict analysis to APIs i.e. dependences
                          from the signature of public and protected
                          members of public classes including field
                          type, method parameter types, returned type,
                          checked exception types etc.
  -jdkinternals
  --jdk-internals         Finds class-level dependences on JDK internal
                          APIs. By default, it analyzes all classes
                          on --class-path and input files unless -include
                          option is specified. This option cannot be
                          used with -p, -e and -s options.
                          WARNING: JDK internal APIs are inaccessible.
--check <module-name>[,<module-name>...
                          Analyze the dependence of the specified modules
                          It prints the module descriptor, the resulting
                          module dependences after analysis and the
                          graph after transition reduction. It also
                          identifies any unused qualified exports.
  --generate-module-info <dir>
                          Generate module-info.java under the specified
                          directory. The specified JAR files will be
                          analyzed. This option cannot be used with
                          --dot-output or --class-path. Use
                          --generate-open-module option for open modules.
  --generate-open-module <dir>
                          Generate module-info.java for the specified
                          JAR files under the specified directory as
                          open modules. This option cannot be used with
                          --dot-output or --class-path.
  --list-deps             Lists the dependences and use of JDK internal APIs.
  --list-reduced-deps     Same as --list-deps with not listing
                          the implied reads edges from the module graph
                          If module M1 depends on M2 and M3,
                          M2 requires public on M3, then M1 reading M3 is
                          implied and removed from the module graph.
  -cp <path>
  -classpath <path>
  --class-path <path>     Specify where to find class files
  --module-path <module path>
                          Specify module path
  --upgrade-module-path <module path>
                          Specify upgrade module path
  --system <java-home>    Specify an alternate system module path
  --add-modules <module-name>[,<module-name>...]
                          Adds modules to the root set for analysis
  -m <module-name>
  --module <module-name>  Specify the root module for analysis
  --multi-release <version>
                          Specifies the version when processing
                          multi-release jar files. should
                          be integer >= 9 or base.

Options to filter dependences:
  -p <pkg>
  -package <pkg>
  --package <pkg>         Finds dependences matching the given package
                          name (may be given multiple times).
  -e <regex>
  -regex <regex>
  --regex <regex>         Finds dependences matching the given pattern.
  --require <module-name> Finds dependences matching the given module
                          name (may be given multiple times). --package,
                          --regex, --require are mutual exclusive.
  -f <regex> -filter <regex> Filter dependences matching the given
                          pattern. If given multiple times, the last
                          one will be used.
  -filter:package         Filter dependences within the same package.
                          This is the default.
  -filter:archive         Filter dependences within the same archive.
  -filter:module          Filter dependences within the same module.
  -filter:none            No -filter:package and -filter:archive
                          filtering. Filtering specified via the
                          -filter option still applies.

Options to filter classes to be analyzed:
  -include <regex>        Restrict analysis to classes matching pattern
                          This option filters the list of classes to
                          be analyzed. It can be used together with
                          -p and -e which apply pattern to the dependences
  -P          -profile    Show profile containing a package
  -R          -recursive  Recursively traverse all run-time dependences.
                          The -R option implies -filter:none. If -p,
                          -e, -f option is specified, only the matching
                          dependences are analyzed.
  -I          --inverse   Analyzes the dependences per other given options
                          and then find all artifacts that directly
                          and indirectly depend on the matching nodes.
                          This is equivalent to the inverse of
                          compile-time view analysis and print
                          dependency summary. This option must use
                          with --require, --package or --regex option.
  --compile-time          Compile-time view of transitive dependences
                          i.e. compile-time view of -R option.
                          Analyzes the dependences per other given options
                          If a dependence is found from a directory,
                          a JAR file or a module, all classes in that
                          containing archive are analyzed.
  -q          -quiet      Do not show missing dependences from
                          --generate-module-info output.
  -version    --version   Version information

押さえておきたい主要オプションの解説

1. 出力形式・詳細度を指定するオプション

オプション説明
-s / -summary依存関係のサマリーのみを出力する
-v / -verboseクラスレベルの依存関係をすべて出力(-verbose:class -filter:none と同等)
-verbose:packageパッケージレベルの依存関係を出力(デフォルトで同一パッケージ内の依存は除外)
-verbose:classクラスレベルの依存関係を出力(デフォルトで同一パッケージ内の依存は除外)
--dot-output <dir>依存関係グラフをDOT形式のファイルとして指定ディレクトリに出力する

2. JDK内部APIの利用を検出するオプション

  • --jdk-internals:JDK内部APIへのクラスレベル依存を検出します。移行前に非公開APIの使用箇所を特定する際に不可欠です。
  • --api-only:public/protectedメンバーのシグネチャ(フィールド型、メソッド引数、戻り値型、検査例外など)からの依存のみを解析対象に限定します。

3. モジュール関連のオプション(Java 9の目玉機能)

  • --check <module-name>:指定モジュールの依存関係を分析し、モジュール記述子や解析後のモジュール依存グラフ、未使用のqualified exportsを表示します。
  • --generate-module-info <dir>:指定したJARファイルを解析し、module-info.javaを自動生成します。オープンモジュール用には--generate-open-moduleを使用します。
  • --list-deps:依存関係とJDK内部APIの使用状況を一覧表示します。
  • --list-reduced-deps--list-depsの結果から、モジュールグラフ上で暗黙的に読み取り可能なエッジ(implied reads)を除外して表示します。

4. 依存関係を絞り込むフィルタ系オプション

  • -p / --package <pkg>:指定パッケージ名に一致する依存関係を検索(複数回指定可)
  • -e / --regex <regex>:正規表現パターンに一致する依存関係を検索
  • --require <module-name>:指定モジュール名に一致する依存関係を検索(※--package、--regex、--requireは相互排他)
  • -f / -filter <regex>:指定パターンに一致する依存関係をフィルタリング
  • -filter:package / -filter:archive / -filter:module / -filter:none:同一パッケージ・同一アーカイブ・同一モジュール内の依存を除外(noneで解除)
  • -include <regex>:解析対象のクラスをパターンに一致するものだけに制限

5. 解析範囲・その他の便利なオプション

  • -R / -recursive:実行時の依存関係を再帰的にたどって解析(-filter:noneが暗黙適用)
  • -I / --inverse:逆方向の解析を行い、指定ノードに直接・間接的に依存する成果物をすべて特定(--require、--package、--regexとの併用必須)
  • --compile-time:推移的な依存関係のコンパイル時ビューで解析
  • --multi-release <version>:マルチリリースJARを処理する際のバージョンを指定(9以上の整数またはbase)
  • --module-path / --upgrade-module-path / --system:モジュールパスや代替システムモジュールパスを指定

まとめ

jdepsは単なる依存関係チェッカーではなく、Java 9のモジュールシステムへ安全に移行するための羅針盤となる重要ツールです。--jdk-internalsでJDK内部APIの使用を洗い出し、--generate-module-infoでmodule-info.javaの雛形を自動生成するなど、モジュール化作業を大幅に効率化できます。まずはjdeps --helpでオプションを確認し、自身のプロジェクトの依存関係を可視化することから始めてみましょう。

  1. JavaのSwingWorkerクラスとは?GUIアプリで非同期処理を行う重要性と使い方を解説

    SwingWorkerクラスは、時間のかかる処理(長時間実行タスク)などをワーカースレッド上で非同期に実行し、その結果に基づいてイベントディスパッチスレッド(EDT)からSwingコンポーネントを安全に更新するための仕組みを提供します。このクラスはJava 1.6で導入されました。Swingでは、すべてのUI更新はEDT上で行う必要があります。しかし、EDT上で重い処理を直接実行すると画面がフリーズしてしまい、ユーザー体験を大きく損ないます。SwingWorkerを使えば、バックグラウンドでの処理とUIの更新を適切に分離できるため、応答性の高いデスクトップアプリケーションを実現できます。Swi

  2. JavaのSwingUtilitiesクラスとは?invokeAndWait()とinvokeLater()で実現するスレッドセーフなGUI操作

    JavaのSwingでは、コンポーネントが画面に表示された後、それらを操作できるのは「イベントディスパッチスレッド(Event Handling Thread)」と呼ばれる単一のスレッドだけです。これはSwingの重要な設計原則であり、複数のスレッドから同時にGUIを操作すると不整合や予期しない動作が発生するため、すべての描画・更新処理をこのスレッドに集約することで安全性を確保しています。開発者は、別途作成したコードブロックの参照をイベントディスパッチスレッドに渡すことができます。SwingUtilitiesクラスには、そのための2つの重要なstaticメソッド、invokeAndWait()