Java 9のJdepsツールとJdeprscanツールの違いとは?使い方を徹底解説
Java 9には、アプリケーションの保守や新バージョンへの移行を支援する強力なコマンドラインツールが複数同梱されています。中でも特に重要なのが、クラスの依存関係を分析するJdepsと、非推奨(Deprecated)APIの使用箇所を検出するJdeprscanの2つです。
Jdepsはクラス間の依存関係を分析するためのツールです。例えば「jdeps -jdkinternals jararchive.jar」コマンドを実行すると、Java内部APIを使用しているすべてのクラスの一覧が出力され、依存関係に関する詳細な情報が得られます。
一方、Jdeprscanは非推奨APIスキャナーで、特に「--for-removal」フラグと組み合わせて使用すると便利です。指定されたjarアーカイブ内における非推奨APIの使用箇所をすべて表示します。ただし、このツールで検出できるのはJDKメソッドの非推奨な使用のみであり、サードパーティ製jarの非推奨状況をチェックすることはできません。
Jdepsツールの主な使い方
- 「jdeps」はクラス依存関係アナライザーであり、パッケージレベルおよびクラスレベルの依存関係を調査できます。
- 「jdeps class_file」コマンド:指定したクラスファイルのパッケージレベルの依存関係を出力します。
- 「jdeps -verbose」コマンド:クラスレベルの依存関係を出力します。
- 「jdeps jar_file」コマンド:指定したjarファイルのパッケージレベルの依存関係を出力します。
- 「jdeps --inverse --require module_name」コマンド:指定したJavaモジュールに対するパッケージレベルの逆依存関係を出力します。
以下のように「jdeps --help」コマンドを実行すると、利用可能なオプションの完全な一覧を確認できます。
C:\Users\user>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
Jdeprscanツールの主な使い方
- 「jdeprscan」はJava Deprecated API Scannerツールで、非推奨API要素をスキャンするために使用できます。
- 「jdeprscan class_file」コマンド:指定したJavaクラスファイルの非推奨APIをスキャンします。
- 「jdeprscan jar_file」コマンド:指定したjarファイルの非推奨APIをスキャンします。
- 「jdeprscan --release X」コマンド:特定のJDKリリースの非推奨APIをスキャンします。
- 「jdeprscan --list --release X」コマンド:特定のJDKリリースのすべての非推奨APIを一覧表示します。
以下のように「jdeprscan --help」コマンドを実行すると、オプションの完全な一覧を確認できます。
C:\Users\User>jdeprscan --help
Usage: jdeprscan [options] {dir|jar|class} ...
options:
--class-path PATH
--for-removal
--full-version
-h --help
-l --list
--release 6|7|8|9
-v --verbose
--version
Scans each argument for usages of deprecated APIs. An argument may be a directory specifying the root of a package hierarchy,
a JAR file, a class file, or a class name. The class name must be specified using a fully qualified class name using the $ separator
character for nested classes, for example,
java.lang.Thread$State
The --class-path option provides a search path for resolution of dependent classes.
The --for-removal option limits scanning or listing to APIs that are
deprecated for removal. Cannot be used with a release value of 6, 7, or 8.
The --full-version option prints out the full version string of the tool.
The --help option prints out a full help message.
The --list (-l) option prints out the set of deprecated APIs. No scanning is done,
so no directory, jar, or class arguments should be provided.
The --release option specifies the Java SE release that provides the set of deprecated APIs for scanning.
The --verbose (-v) option enables additional message output during processing.
The --version option prints out the abbreviated version string of the tool.
まとめ:JdepsとJdeprscanの使い分け
JdepsとJdeprscanは、目的が異なる補完関係にあるツールです。Jdepsは「どのクラスやモジュールが何に依存しているか」を可視化し、JDK内部APIへの依存を洗い出すのに役立ちます。一方、Jdeprscanは「将来的に削除される可能性のある非推奨APIをどこで使用しているか」を特定します。Java 9以降への移行を計画している場合は、まずJdepsで内部APIへの依存を確認し、次に「jdeprscan --for-removal」で削除予定のAPIの使用箇所を特定するという手順で進めるのが効果的です。
-
【Java Swing】JTextFieldとJFormattedTextFieldの違いを徹底解説
Swingには、テキスト入力を受け付けるコンポーネントとして JTextField と JFormattedTextField の2種類があります。JTextField はプレーンなテキストをそのまま入力するためのものである一方、JFormattedTextField は JTextField を継承したクラスで、電話番号・メールアドレス・日付などの特定の書式(フォーマット)を入力内容に適用できる点が大きな特徴です。 JTextFieldとは JTextField は、ユーザーが1行のテキストを入力できる、Swingの中でも最も重要なコンポーネントの一つです。 テキストフィールド内に入力を行
-
JavaのJScrollBarとJScrollPaneの違いとは?特徴と使い方を解説
JScrollBarはコンポーネントであり、自身のイベントを処理しません。一方、JScrollPaneはコンテナ(Container)であり、自身のイベントを処理するとともに、スクロール操作も自ら行います。また、JScrollBarの中にJScrollPaneを配置することはできませんが、JScrollPaneの中にJScrollBarを配置することは可能です。 JScrollBarとは JScrollBarクラスのオブジェクトは、水平方向および垂直方向のスクロールバーを追加するために使用されます。これにより、ユーザーは指定された最小値から最大値までの範囲で項目を選択できるようになります。