Skip to content

Commit

Permalink
Recognize external domain metadata generated in a different module (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamura-to authored Feb 15, 2025
2 parents 24a01c3 + ab91909 commit 0acb4bb
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.seasar.doma.jdbc.Result;
import org.seasar.doma.jdbc.SelectOptions;
import org.seasar.doma.jdbc.domain.DomainConverter;
import org.seasar.doma.jdbc.domain.DomainType;
import org.seasar.doma.message.Message;
import org.seasar.doma.wrapper.ArrayWrapper;
import org.seasar.doma.wrapper.BigDecimalWrapper;
Expand Down Expand Up @@ -306,6 +307,56 @@ private DomainInfo getDomainInfo(TypeElement typeElement, DataType dataType) {
}

private DomainInfo getExternalDomainInfo(TypeMirror domainType) {
DomainInfo info = getExternalDomainInfoFromMetadata(domainType);
if (info != null) {
return info;
}
return getExternalDomainInfoFromConverterType(domainType);
}

/**
* This method searches for metadata of an External Domain class, extracts the relevant
* information, and returns it as a DomainInfo.
*/
private DomainInfo getExternalDomainInfoFromMetadata(TypeMirror domainType) {
TypeElement domainTypeElement = ctx.getMoreTypes().toTypeElement(domainType);
if (domainTypeElement == null) {
return null;
}
ClassName className =
ClassNames.newExternalDomainTypeClassName(domainTypeElement.getQualifiedName());
TypeElement externalDomainMetadataElement = ctx.getMoreElements().getTypeElement(className);
if (externalDomainMetadataElement == null) {
return null;
}
TypeMirror[] argTypes = getDomainTypeArgTypes(externalDomainMetadataElement.asType());
if (argTypes == null) {
return null;
}
return new DomainInfo(argTypes[0], true);
}

private TypeMirror[] getDomainTypeArgTypes(TypeMirror typeMirror) {
for (TypeMirror supertype : ctx.getMoreTypes().directSupertypes(typeMirror)) {
if (!ctx.getMoreTypes().isAssignableWithErasure(supertype, DomainType.class)) {
continue;
}
if (ctx.getMoreTypes().isSameTypeWithErasure(supertype, DomainType.class)) {
DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(supertype);
assertNotNull(declaredType);
List<? extends TypeMirror> args = declaredType.getTypeArguments();
assertEquals(2, args.size());
return new TypeMirror[] {args.get(0), args.get(1)};
}
TypeMirror[] argTypes = getDomainTypeArgTypes(supertype);
if (argTypes != null) {
return argTypes;
}
}
return null;
}

private DomainInfo getExternalDomainInfoFromConverterType(TypeMirror domainType) {
String csv = ctx.getOptions().getDomainConverters();
if (csv != null) {
for (String value : csv.split(",")) {
Expand Down
13 changes: 13 additions & 0 deletions integration-test-java-common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/bin
/target
/build/
/.apt_generated/
/.gradle/
/.settings/
/.project
/.factorypath
/.classpath
.idea
out
.sdkmanrc
test.db
52 changes: 52 additions & 0 deletions integration-test-java-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
java
alias(libs.plugins.themrmilchmann.ecj) apply false
}

val compiler: String by project

dependencies {
annotationProcessor(project(":doma-processor"))
implementation(project(":doma-core"))
if (compiler == "ecj") {
implementation(libs.ecj)
// See https://github.com/TheMrMilchmann/gradle-ecj/issues/30
compileOnly(project(":doma-processor"))
}
}

val commonArgs = listOf(
"-Adoma.domain.converters=org.seasar.doma.it.domain.CommonDomainConverterProvider",
)

// The processors are not automatically detected, so it must be explicitly specified.
val ecjArgs = listOf(
"-processor",
listOf(
"org.seasar.doma.internal.apt.processor.ExternalDomainProcessor",
).joinToString(","),
)

when (compiler) {
"javac" -> {
tasks {
compileJava {
options.compilerArgs.addAll(commonArgs)
}
}
}

"ecj" -> {
apply(plugin = libs.plugins.themrmilchmann.ecj.get().pluginId)
tasks {
compileJava {
@Suppress("UnstableApiUsage")
options.forkOptions.jvmArgumentProviders.add(
CommandLineArgumentProvider { commonArgs + ecjArgs }
)
}
}
}

else -> error("Unknown compiler: $compiler")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Doma Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.seasar.doma.it.domain;

import org.seasar.doma.DomainConverters;

@DomainConverters({
AgeConverter.class,
})
public class CommonDomainConverterProvider {}
1 change: 1 addition & 0 deletions integration-test-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ val compiler: String by project
dependencies {
annotationProcessor(project(":doma-processor"))
implementation(project(":doma-core"))
implementation(project(":integration-test-java-common"))
testImplementation(project(":integration-test-common"))
if (compiler == "ecj") {
implementation(libs.ecj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.seasar.doma.DomainConverters;

@DomainConverters({
AgeConverter.class,
LocationConverter.class,
HiredateConverter.class,
StringArrayConverter.class,
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ include("doma-template")

include("integration-test-common")
include("integration-test-java")
include("integration-test-java-common")
include("integration-test-kotlin")

0 comments on commit 0acb4bb

Please sign in to comment.