Show Table of Contents
4.2.5. ClassCastExceptions のデバッグと解決
ClassCastExceptions は、拡張するクラスではなく他のクラスによってクラスがロードされるときに発生することが多くあります。また、同じクラスが複数の JAR に存在することが原因である場合もあります。
ClassCastExceptionによって名前付けされたクラスが含まれる JAR をすべて見つけるため、アプリケーションを検索します。クラスに対して定義されたモジュールがある場合、アプリケーションの WAR や EAR から重複する JAR を探し、削除します。- クラスが含まれる JBoss モジュールを探し、
MANIFEST.MFファイルまたはjboss-deployment-structure.xmlファイルに依存関係を明示的に定義します。詳細については、https://access.redhat.com/site/documentation/JBoss_Enterprise_Application_Platform/ にある JBoss EAP 6 向け『開発ガイド』の章「クラスローディングとモジュール」の「クラスローディングとサブデプロイメント」を参照してください。 - 上記の手順に従っても解決されない場合、クラスローダーの情報をログに出力すると問題の原因を判断できることがあります。たとえば、次の
ClassCastExceptionがログに記録されているとします。java.lang.ClassCastException: com.example1.CustomClass1 cannot be cast to com.example2.CustomClass2
- コードで
ClassCastExceptionによって命名されたクラスに対するクラスローダーの情報をログに出力します。例は次のとおりです。logger.info("Class loader for CustomClass1: " + com.example1.CustomClass1.getClass().getClassLoader().toString()); logger.info("Class loader for CustomClass2: " + com.example2.CustomClass2.getClass().getClassLoader().toString()); - ログの情報には、どのモジュールがクラスをロードするかが記載されています。アプリケーションに基づき、競合する JAR を削除または移動する必要があります。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.