0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

commons-lang3のReflectionToStringBuilderにjava.lang.String等を指定するとInaccessibleObjectException

0
Posted at

環境

  • Java 21
  • org.apache.commons:commons-lang3:3.19.0

ソースコードなど

java.lang.Stringとかjava.lang.EnumとかをReflectionToStringBuilder.toStringの引数に直接指定すると下記のような実行時エラーになる。標準ライブラリのクラスのprivateフィールドにアクセスしようとしてしまうためアクセス例外となってしまう。おそらく、Java 8以前なら動いてしまうのではないか? と思われる。

System.out.println(ReflectionToStringBuilder.toString("str"));
System.out.println(ReflectionToStringBuilder.toString(SampleEnum.HOGE));
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field static final boolean java.lang.String.COMPACT_STRINGS accessible: module java.base does not "opens java.lang" to unnamed module @506c589e
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private int java.lang.Enum.hash accessible: module java.base does not "opens java.lang" to unnamed module @446cdf90
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?