Skip to content

Commit dd5bb14

Browse files
authored
Simplify the definition of external domain classes (#1291)
2 parents 985897c + cce8f54 commit dd5bb14

File tree

162 files changed

+1540
-1050
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+1540
-1050
lines changed

doma-core/src/main/java/org/seasar/doma/ExternalDomain.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* }
3939
*
4040
* public Salary fromValueToDomain(BigDecimal value) {
41+
* if (value == null) {
42+
* return null;
43+
* }
4144
* return new Salary(value);
4245
* }
4346
* }
@@ -46,7 +49,6 @@
4649
* <p>The annotated instance is required to be thread safe.
4750
*
4851
* @see DomainConverter
49-
* @see DomainConverters
5052
*/
5153
@Target(ElementType.TYPE)
5254
@Retention(RetentionPolicy.RUNTIME)

doma-core/src/main/java/org/seasar/doma/jdbc/domain/DomainConverter.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*/
1616
package org.seasar.doma.jdbc.domain;
1717

18-
import org.seasar.doma.DomainConverters;
1918
import org.seasar.doma.ExternalDomain;
2019

2120
/**
22-
* A converter between domain objects and basic values.
21+
* A converter interface for mapping between domain objects and their corresponding basic values.
2322
*
24-
* <p>The implementation class should be annotated with {@link ExternalDomain} and be registered to
25-
* {@link DomainConverters}.
23+
* <p>The implementation class should be annotated with {@link ExternalDomain}.
2624
*
2725
* <pre>
2826
* &#064;ExternalDomain
@@ -33,31 +31,33 @@
3331
* }
3432
*
3533
* public Salary fromValueToDomain(BigDecimal value) {
34+
* if (value == null) {
35+
* return null;
36+
* }
3637
* return new Salary(value);
3738
* }
3839
* }
3940
* </pre>
4041
*
4142
* @see ExternalDomain
42-
* @see DomainConverters
43-
* @param <DOMAIN> the domain type
44-
* @param <BASIC> the basic type
43+
* @param <DOMAIN> the type of the domain object
44+
* @param <BASIC> the type of the basic value
4545
*/
4646
public interface DomainConverter<DOMAIN, BASIC> {
4747

4848
/**
49-
* Converts from a domain object to a basic value.
49+
* Converts a domain object into a basic value.
5050
*
51-
* @param domain the domain object
52-
* @return the basic value
51+
* @param domain the domain object; must not be null
52+
* @return the basic value; can be null
5353
*/
5454
BASIC fromDomainToValue(DOMAIN domain);
5555

5656
/**
57-
* Converts from a basic value to a domain object.
57+
* Converts a basic value into a domain object.
5858
*
59-
* @param value the basic value
60-
* @return the domain object
59+
* @param value the basic value; can be null
60+
* @return the domain object; can be null
6161
*/
6262
DOMAIN fromValueToDomain(BASIC value);
6363
}

doma-core/src/main/java/org/seasar/doma/message/Message.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,7 @@ public enum Message implements MessageResource {
479479
DOMA4093("The field annotated with @Version must be numeric."),
480480
DOMA4095("The field annotated with @GeneratedValue must be numeric."),
481481
DOMA4096(
482-
"The class \"{0}\" is not supported as a persistent type. "
483-
+ "If you intend to map the class to the external domain class with @ExternalDomain, the configuration may be not enough. "
484-
+ "Check the class annotated with @DomainConverters and the annotation processing option \"doma.domain.converters\"."),
482+
"The class \"{0}\" is not supported as a persistent type. By using @ExternalDomain, you can make that type persistent."),
485483
DOMA4097("The return type must be {0}."),
486484
DOMA4098("The type of the parameter annotated with @Out must be org.seasar.doma.jdbc.Reference."),
487485
DOMA4100("{0} is illegal as the type argument of Reference."),
@@ -852,10 +850,7 @@ public enum Message implements MessageResource {
852850
DOMA4297(
853851
"The embeddable class \"{0}\" cannot be used as a persistent property in the embeddable class."),
854852
DOMA4298(
855-
"The class \"{0}\" is not supported as a persistent type. "
856-
+ "If you intend to map the class to the external domain class with @ExternalDomain, "
857-
+ "the configuration may be not enough. "
858-
+ "Check the class annotated with @DomainConverters and the annotation processing option \"doma.domain.converters\"."),
853+
"The class \"{0}\" is not supported as a persistent type. By using @ExternalDomain, you can make that type persistent."),
859854
DOMA4299("The raw type of the class \"{0}\" cannot be used as a persistent property."),
860855
DOMA4301(
861856
"The type argument of the class \"{0}\" that is a wildcard or type variable cannot be used as a persistent property."),
@@ -957,7 +952,7 @@ public enum Message implements MessageResource {
957952
"If a method annotated with @MultiInsert targets immutable entities for insertion, the return type must be org.seasar.doma.jdbc.MultiResult."
958953
+ "The type argument of org.seasar.doma.jdbc.MultiResult must be the immutable entity class."),
959954
DOMA4462("The property \"{0}\" is not found in the entity class \"{1}\"."),
960-
DOMA4463("'{'\"execTimeMillis\": {0}, \"processor\": \"{1}\", \"element\": \"{2}\"'}'"),
955+
DOMA4463("'{'\"execTimeMillis\": {0}, \"annotation\": \"{1}\", \"element\": \"{2}\"'}'"),
961956
DOMA4464("Fields annotated with AssociationLinker must be static."),
962957
DOMA4465("Fields annotated with AssociationLinker must be java.util.function.BiFunction."),
963958
DOMA4466("The {0} type parameter of BiFunction must be an entity class."),
@@ -1006,6 +1001,8 @@ public enum Message implements MessageResource {
10061001
DOMA4488(
10071002
"To specify propertyPath=\"{0}\", a separate definition of @AssociationLinker(propertyPath=\"{1}\") is required."),
10081003
DOMA4489("The property path \"{0}\" is duplicated in another @AssociationLinker."),
1004+
DOMA4490(
1005+
"Multiple @ExternalDomain definitions were found for type \"{0}\". \"{1}\" conflicts with \"{2}\"."),
10091006

10101007
// other
10111008
DOMA5001(

doma-processor/src/main/java/module-info.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,5 @@
1818
requires org.seasar.doma.core;
1919

2020
provides javax.annotation.processing.Processor with
21-
org.seasar.doma.internal.apt.processor.DomainProcessor,
22-
org.seasar.doma.internal.apt.processor.DataTypeProcessor,
23-
org.seasar.doma.internal.apt.processor.ExternalDomainProcessor,
24-
org.seasar.doma.internal.apt.processor.DomainConvertersProcessor,
25-
org.seasar.doma.internal.apt.processor.EmbeddableProcessor,
26-
org.seasar.doma.internal.apt.processor.EntityProcessor,
27-
org.seasar.doma.internal.apt.processor.DaoProcessor,
28-
org.seasar.doma.internal.apt.processor.ScopeProcessor;
21+
org.seasar.doma.internal.apt.DomaProcessor;
2922
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.internal.apt;
17+
18+
public final class AnnotationTypes {
19+
public static final String AGGREGATE_STRATEGY = "org.seasar.doma.AggregateStrategy";
20+
public static final String DAO = "org.seasar.doma.Dao";
21+
public static final String DATA_TYPE = "org.seasar.doma.DataType";
22+
public static final String DOMAIN_CONVERTERS = "org.seasar.doma.DomainConverters";
23+
public static final String DOMAIN = "org.seasar.doma.Domain";
24+
public static final String EMBEDDABLE = "org.seasar.doma.Embeddable";
25+
public static final String ENTITY = "org.seasar.doma.Entity";
26+
public static final String EXTERNAL_DOMAIN = "org.seasar.doma.ExternalDomain";
27+
public static final String SCOPE = "org.seasar.doma.Scope";
28+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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.internal.apt;
17+
18+
import static org.seasar.doma.internal.apt.AnnotationTypes.AGGREGATE_STRATEGY;
19+
import static org.seasar.doma.internal.apt.AnnotationTypes.DAO;
20+
import static org.seasar.doma.internal.apt.AnnotationTypes.DATA_TYPE;
21+
import static org.seasar.doma.internal.apt.AnnotationTypes.DOMAIN;
22+
import static org.seasar.doma.internal.apt.AnnotationTypes.DOMAIN_CONVERTERS;
23+
import static org.seasar.doma.internal.apt.AnnotationTypes.EMBEDDABLE;
24+
import static org.seasar.doma.internal.apt.AnnotationTypes.ENTITY;
25+
import static org.seasar.doma.internal.apt.AnnotationTypes.EXTERNAL_DOMAIN;
26+
import static org.seasar.doma.internal.apt.AnnotationTypes.SCOPE;
27+
28+
import java.util.List;
29+
import java.util.Objects;
30+
import java.util.Set;
31+
import java.util.function.BiConsumer;
32+
import javax.annotation.processing.AbstractProcessor;
33+
import javax.annotation.processing.ProcessingEnvironment;
34+
import javax.annotation.processing.RoundEnvironment;
35+
import javax.annotation.processing.SupportedAnnotationTypes;
36+
import javax.annotation.processing.SupportedOptions;
37+
import javax.lang.model.SourceVersion;
38+
import javax.lang.model.element.Element;
39+
import javax.lang.model.element.TypeElement;
40+
import org.seasar.doma.internal.apt.processor.AggregateStrategyProcessor;
41+
import org.seasar.doma.internal.apt.processor.DaoProcessor;
42+
import org.seasar.doma.internal.apt.processor.DataTypeProcessor;
43+
import org.seasar.doma.internal.apt.processor.DomainConvertersProcessor;
44+
import org.seasar.doma.internal.apt.processor.DomainProcessor;
45+
import org.seasar.doma.internal.apt.processor.EmbeddableProcessor;
46+
import org.seasar.doma.internal.apt.processor.EntityProcessor;
47+
import org.seasar.doma.internal.apt.processor.ExternalDomainProcessor;
48+
import org.seasar.doma.internal.apt.processor.ScopeProcessor;
49+
50+
@SupportedAnnotationTypes({
51+
AGGREGATE_STRATEGY,
52+
DAO,
53+
DATA_TYPE,
54+
DOMAIN_CONVERTERS,
55+
DOMAIN,
56+
EMBEDDABLE,
57+
ENTITY,
58+
EXTERNAL_DOMAIN,
59+
SCOPE,
60+
})
61+
@SupportedOptions({
62+
Options.CONFIG_PATH,
63+
Options.DAO_PACKAGE,
64+
Options.DAO_SUBPACKAGE,
65+
Options.DAO_SUFFIX,
66+
Options.DEBUG,
67+
Options.DOMAIN_CONVERTERS,
68+
Options.ENTITY_FIELD_PREFIX,
69+
Options.EXPR_FUNCTIONS,
70+
Options.LOMBOK_ALL_ARGS_CONSTRUCTOR,
71+
Options.LOMBOK_VALUE,
72+
Options.METAMODEL_ENABLED,
73+
Options.METAMODEL_PREFIX,
74+
Options.METAMODEL_SUFFIX,
75+
Options.RESOURCES_DIR,
76+
Options.SQL_VALIDATION,
77+
Options.TEST,
78+
Options.TRACE,
79+
Options.VERSION_VALIDATION,
80+
})
81+
public class DomaProcessor extends AbstractProcessor {
82+
83+
private final List<Operator> operators;
84+
private ProcessingContext processingContext;
85+
86+
public DomaProcessor() {
87+
operators =
88+
List.of(
89+
new Operator(EXTERNAL_DOMAIN, DomaProcessor::processExternalDomain),
90+
new Operator(DATA_TYPE, DomaProcessor::processDataType),
91+
new Operator(DOMAIN, DomaProcessor::processDomain),
92+
new Operator(DOMAIN_CONVERTERS, DomaProcessor::processDomainConverters),
93+
new Operator(EMBEDDABLE, DomaProcessor::processEmbeddable),
94+
new Operator(ENTITY, DomaProcessor::processEntity),
95+
new Operator(AGGREGATE_STRATEGY, DomaProcessor::processAggregateStrategy),
96+
new Operator(DAO, DomaProcessor::processDao),
97+
new Operator(SCOPE, DomaProcessor::processScope));
98+
}
99+
100+
@Override
101+
public synchronized void init(ProcessingEnvironment processingEnv) {
102+
super.init(processingEnv);
103+
processingContext = new ProcessingContext(processingEnv);
104+
processingContext.init();
105+
}
106+
107+
@Override
108+
public SourceVersion getSupportedSourceVersion() {
109+
return SourceVersion.latest();
110+
}
111+
112+
@Override
113+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
114+
if (roundEnv.processingOver()) {
115+
return true;
116+
}
117+
118+
var roundContext = new RoundContext(processingContext, roundEnv, annotations);
119+
120+
for (var operator : operators) {
121+
var elements = roundContext.getElementsAnnotatedWith(operator.name);
122+
if (!elements.isEmpty()) {
123+
operator.consumer.accept(roundContext, elements);
124+
}
125+
}
126+
127+
return true;
128+
}
129+
130+
private static void processAggregateStrategy(
131+
RoundContext roundContext, Set<? extends Element> elements) {
132+
var processor = new AggregateStrategyProcessor(roundContext);
133+
processor.process(elements);
134+
}
135+
136+
private static void processDao(RoundContext roundContext, Set<? extends Element> elements) {
137+
var processor = new DaoProcessor(roundContext);
138+
processor.process(elements);
139+
}
140+
141+
private static void processDataType(RoundContext roundContext, Set<? extends Element> elements) {
142+
var processor = new DataTypeProcessor(roundContext);
143+
processor.process(elements);
144+
}
145+
146+
private static void processDomainConverters(
147+
RoundContext roundContext, Set<? extends Element> elements) {
148+
var processor = new DomainConvertersProcessor(roundContext);
149+
processor.process(elements);
150+
}
151+
152+
private static void processDomain(RoundContext roundContext, Set<? extends Element> elements) {
153+
var processor = new DomainProcessor(roundContext);
154+
processor.process(elements);
155+
}
156+
157+
private static void processEmbeddable(
158+
RoundContext roundContext, Set<? extends Element> elements) {
159+
var processor = new EmbeddableProcessor(roundContext);
160+
processor.process(elements);
161+
}
162+
163+
private static void processEntity(RoundContext roundContext, Set<? extends Element> elements) {
164+
var processor = new EntityProcessor(roundContext);
165+
processor.process(elements);
166+
}
167+
168+
private static void processExternalDomain(
169+
RoundContext roundContext, Set<? extends Element> elements) {
170+
var processor = new ExternalDomainProcessor(roundContext);
171+
processor.process(elements);
172+
}
173+
174+
private static void processScope(RoundContext roundContext, Set<? extends Element> elements) {
175+
var processor = new ScopeProcessor(roundContext);
176+
processor.process(elements);
177+
}
178+
179+
private record Operator(String name, BiConsumer<RoundContext, Set<? extends Element>> consumer) {
180+
Operator {
181+
Objects.requireNonNull(name);
182+
Objects.requireNonNull(consumer);
183+
}
184+
}
185+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454

5555
public class MoreElements implements Elements {
5656

57-
private final Context ctx;
57+
private final ProcessingContext ctx;
5858

5959
private final Elements elementUtils;
6060

6161
private final Map<String, TypeElement> typeElementCache = new HashMap<>(64);
6262

63-
public MoreElements(Context ctx, Elements elementUtils) {
63+
public MoreElements(ProcessingContext ctx, Elements elementUtils) {
6464
assertNotNull(ctx, elementUtils);
6565
this.ctx = ctx;
6666
this.elementUtils = elementUtils;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343

4444
public class MoreTypes implements Types {
4545

46-
private final Context ctx;
46+
private final ProcessingContext ctx;
4747

4848
private final Types typeUtils;
4949

50-
public MoreTypes(Context ctx, Types typeUtils) {
50+
public MoreTypes(ProcessingContext ctx, Types typeUtils) {
5151
assertNotNull(ctx, typeUtils);
5252
this.ctx = ctx;
5353
this.typeUtils = typeUtils;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
public class Names {
2727

28-
private final Context ctx;
28+
private final RoundContext ctx;
2929

30-
Names(Context ctx) {
30+
Names(RoundContext ctx) {
3131
assertNotNull(ctx);
3232
this.ctx = ctx;
3333
}

0 commit comments

Comments
 (0)