Skip to content

プロジェクトクラスパスから Entity の superclassName を解決できるようにする#294

Open
tomo-yamaguchi-cre wants to merge 2 commits intodomaframework:masterfrom
tomo-yamaguchi-cre:fix/codegen-superclass-classloader
Open

プロジェクトクラスパスから Entity の superclassName を解決できるようにする#294
tomo-yamaguchi-cre wants to merge 2 commits intodomaframework:masterfrom
tomo-yamaguchi-cre:fix/codegen-superclass-classloader

Conversation

@tomo-yamaguchi-cre
Copy link

こんにちは。
Ant ベースの doma-gen から Gradle の doma-codegen-plugin へ移行する中で、エンティティ生成時に superclassName で指定したプロジェクト内クラスを解決できず、以下のエラーになる問題に遭遇しました。

[DOMAGEN0013] The class "base.AbstractEntity" to which the parameter
"superclassName" refers is not found.
java.lang.ClassNotFoundException: base.AbstractEntity

プロジェクト内で定義したスーパークラス(例: AbstractEntity)を superclassName に指定したい場面があるのですが、現状はコード生成タスクの ClassLoader がプロジェクトのビルド出力や domaCodeGen 構成のクラスパスを参照しないため、解決できませんでした。
また、docs/codegen.md には superclassName の設定例がある一方で、プロジェクト内クラスを参照する場合のクラスパス設定については補足がなく、この挙動に気づきにくいかもしれないと感じました。

このPRでは、プロジェクトクラスパスを使って superclassName を解決できるように対応します。

変更内容

  • CodeGenConfig に ClassLoader を供給する仕組みを追加
    • domaCodeGen 構成のクラスパス等を含む ClassLoader を遅延評価で作成できるようにしました(設定フェーズでの解決を避けるため)。
  • CodeGenEntityDescTask へ ClassLoader を伝播
    • ClassLoader が提供されている場合はそれを使用し、未提供の場合は従来通りプラグインの ClassLoader を利用します(後方互換性)。
  • CodeGenPlugin 側で接続
    • connectProperties() で上記 Provider を各タスクに渡すようにしました。
  • 統合テストを追加
    • codegen-superclass-test モジュールで、監査フィールドを持つ AbstractEntitysuperclassName に指定しエンティティが生成できることを確認しています。

使い方(例)

プロジェクト内のスーパークラスを参照したい場合、以下のように domaCodeGen 構成へプロジェクト出力を追加できるようになります。

dependencies {
    domaCodeGen(libs.h2)
    domaCodeGen(sourceSets.main.get().output)
}

必要に応じて、コード生成タスクより先にスーパークラスがコンパイルされるよう依存関係を調整する必要があります。

後方互換性

ClassLoader を明示的に提供しない場合は、これまで通りプラグインの ClassLoader を使用するため、既存設定への影響はありません。

- Add codegen-superclass-test module to test entity superclass functionality
- Implement AbstractEntity with common audit fields (createdBy, createdAt, etc.)
- Implement AbstractEntityListener for automatic audit field population
- Add test database schema with EMPLOYEE, DEPARTMENT, and PROJECT tables
- Configure domaCodeGen to use custom superclass and listener
- Add DAO tests to verify generated code with superclass

The test demonstrates:
- Loading superclass from project's compiled classes via domaCodeGen classpath
- Three-phase build: compile base classes → generate code → compile all
- Generated entities properly extend AbstractEntity
- Generated listeners extend AbstractEntityListener
- Add ClassLoader provider in CodeGenConfig to resolve classes from domaCodeGen configuration
- Pass ClassLoader to CodeGenEntityDescTask for loading entity superclass
- Use custom ClassLoader in ClassUtil.forName when resolving superclass
- Enables loading user-defined superclasses from build classpath

This change allows the plugin to resolve entity superclass and listener
superclass from the domaCodeGen configuration classpath, rather than
only from the plugin's own classpath.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments