Skip to content

Commit 0acb4bb

Browse files
authored
Recognize external domain metadata generated in a different module (#1284)
2 parents 24a01c3 + ab91909 commit 0acb4bb

File tree

10 files changed

+141
-2
lines changed

10 files changed

+141
-2
lines changed

doma-processor/src/main/java/org/seasar/doma/internal/apt/cttype/CtTypes.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import org.seasar.doma.jdbc.Result;
8383
import org.seasar.doma.jdbc.SelectOptions;
8484
import org.seasar.doma.jdbc.domain.DomainConverter;
85+
import org.seasar.doma.jdbc.domain.DomainType;
8586
import org.seasar.doma.message.Message;
8687
import org.seasar.doma.wrapper.ArrayWrapper;
8788
import org.seasar.doma.wrapper.BigDecimalWrapper;
@@ -306,6 +307,56 @@ private DomainInfo getDomainInfo(TypeElement typeElement, DataType dataType) {
306307
}
307308

308309
private DomainInfo getExternalDomainInfo(TypeMirror domainType) {
310+
DomainInfo info = getExternalDomainInfoFromMetadata(domainType);
311+
if (info != null) {
312+
return info;
313+
}
314+
return getExternalDomainInfoFromConverterType(domainType);
315+
}
316+
317+
/**
318+
* This method searches for metadata of an External Domain class, extracts the relevant
319+
* information, and returns it as a DomainInfo.
320+
*/
321+
private DomainInfo getExternalDomainInfoFromMetadata(TypeMirror domainType) {
322+
TypeElement domainTypeElement = ctx.getMoreTypes().toTypeElement(domainType);
323+
if (domainTypeElement == null) {
324+
return null;
325+
}
326+
ClassName className =
327+
ClassNames.newExternalDomainTypeClassName(domainTypeElement.getQualifiedName());
328+
TypeElement externalDomainMetadataElement = ctx.getMoreElements().getTypeElement(className);
329+
if (externalDomainMetadataElement == null) {
330+
return null;
331+
}
332+
TypeMirror[] argTypes = getDomainTypeArgTypes(externalDomainMetadataElement.asType());
333+
if (argTypes == null) {
334+
return null;
335+
}
336+
return new DomainInfo(argTypes[0], true);
337+
}
338+
339+
private TypeMirror[] getDomainTypeArgTypes(TypeMirror typeMirror) {
340+
for (TypeMirror supertype : ctx.getMoreTypes().directSupertypes(typeMirror)) {
341+
if (!ctx.getMoreTypes().isAssignableWithErasure(supertype, DomainType.class)) {
342+
continue;
343+
}
344+
if (ctx.getMoreTypes().isSameTypeWithErasure(supertype, DomainType.class)) {
345+
DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(supertype);
346+
assertNotNull(declaredType);
347+
List<? extends TypeMirror> args = declaredType.getTypeArguments();
348+
assertEquals(2, args.size());
349+
return new TypeMirror[] {args.get(0), args.get(1)};
350+
}
351+
TypeMirror[] argTypes = getDomainTypeArgTypes(supertype);
352+
if (argTypes != null) {
353+
return argTypes;
354+
}
355+
}
356+
return null;
357+
}
358+
359+
private DomainInfo getExternalDomainInfoFromConverterType(TypeMirror domainType) {
309360
String csv = ctx.getOptions().getDomainConverters();
310361
if (csv != null) {
311362
for (String value : csv.split(",")) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/bin
2+
/target
3+
/build/
4+
/.apt_generated/
5+
/.gradle/
6+
/.settings/
7+
/.project
8+
/.factorypath
9+
/.classpath
10+
.idea
11+
out
12+
.sdkmanrc
13+
test.db
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
plugins {
2+
java
3+
alias(libs.plugins.themrmilchmann.ecj) apply false
4+
}
5+
6+
val compiler: String by project
7+
8+
dependencies {
9+
annotationProcessor(project(":doma-processor"))
10+
implementation(project(":doma-core"))
11+
if (compiler == "ecj") {
12+
implementation(libs.ecj)
13+
// See https://github.com/TheMrMilchmann/gradle-ecj/issues/30
14+
compileOnly(project(":doma-processor"))
15+
}
16+
}
17+
18+
val commonArgs = listOf(
19+
"-Adoma.domain.converters=org.seasar.doma.it.domain.CommonDomainConverterProvider",
20+
)
21+
22+
// The processors are not automatically detected, so it must be explicitly specified.
23+
val ecjArgs = listOf(
24+
"-processor",
25+
listOf(
26+
"org.seasar.doma.internal.apt.processor.ExternalDomainProcessor",
27+
).joinToString(","),
28+
)
29+
30+
when (compiler) {
31+
"javac" -> {
32+
tasks {
33+
compileJava {
34+
options.compilerArgs.addAll(commonArgs)
35+
}
36+
}
37+
}
38+
39+
"ecj" -> {
40+
apply(plugin = libs.plugins.themrmilchmann.ecj.get().pluginId)
41+
tasks {
42+
compileJava {
43+
@Suppress("UnstableApiUsage")
44+
options.forkOptions.jvmArgumentProviders.add(
45+
CommandLineArgumentProvider { commonArgs + ecjArgs }
46+
)
47+
}
48+
}
49+
}
50+
51+
else -> error("Unknown compiler: $compiler")
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright Doma Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma.it.domain;
17+
18+
import org.seasar.doma.DomainConverters;
19+
20+
@DomainConverters({
21+
AgeConverter.class,
22+
})
23+
public class CommonDomainConverterProvider {}

integration-test-java/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ val compiler: String by project
88
dependencies {
99
annotationProcessor(project(":doma-processor"))
1010
implementation(project(":doma-core"))
11+
implementation(project(":integration-test-java-common"))
1112
testImplementation(project(":integration-test-common"))
1213
if (compiler == "ecj") {
1314
implementation(libs.ecj)

integration-test-java/src/main/java/org/seasar/doma/it/domain/DomainConverterProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.seasar.doma.DomainConverters;
1919

2020
@DomainConverters({
21-
AgeConverter.class,
2221
LocationConverter.class,
2322
HiredateConverter.class,
2423
StringArrayConverter.class,

integration-test-java/src/main/resources/doma.compile.config

Lines changed: 0 additions & 1 deletion
This file was deleted.

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ include("doma-template")
1515

1616
include("integration-test-common")
1717
include("integration-test-java")
18+
include("integration-test-java-common")
1819
include("integration-test-kotlin")

0 commit comments

Comments
 (0)