Skip to content

Commit 295c1ab

Browse files
committed
Simplify the definition of external domain classes.
We no longer need to create a class annotated with ``@DomainConverters` or use the `doma.domain.converters` option.
1 parent 985897c commit 295c1ab

File tree

137 files changed

+1319
-1000
lines changed

Some content is hidden

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

137 files changed

+1319
-1000
lines changed

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

+1-8
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
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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.ArrayList;
29+
import java.util.List;
30+
import java.util.Objects;
31+
import java.util.Set;
32+
import java.util.function.BiConsumer;
33+
import javax.annotation.processing.AbstractProcessor;
34+
import javax.annotation.processing.ProcessingEnvironment;
35+
import javax.annotation.processing.RoundEnvironment;
36+
import javax.annotation.processing.SupportedAnnotationTypes;
37+
import javax.annotation.processing.SupportedOptions;
38+
import javax.lang.model.SourceVersion;
39+
import javax.lang.model.element.Element;
40+
import javax.lang.model.element.TypeElement;
41+
import org.seasar.doma.internal.apt.processor.AggregateStrategyProcessor;
42+
import org.seasar.doma.internal.apt.processor.DaoProcessor;
43+
import org.seasar.doma.internal.apt.processor.DataTypeProcessor;
44+
import org.seasar.doma.internal.apt.processor.DomainConvertersProcessor;
45+
import org.seasar.doma.internal.apt.processor.DomainProcessor;
46+
import org.seasar.doma.internal.apt.processor.EmbeddableProcessor;
47+
import org.seasar.doma.internal.apt.processor.EntityProcessor;
48+
import org.seasar.doma.internal.apt.processor.ExternalDomainProcessor;
49+
import org.seasar.doma.internal.apt.processor.ScopeProcessor;
50+
51+
@SupportedAnnotationTypes({
52+
AGGREGATE_STRATEGY,
53+
DAO,
54+
DATA_TYPE,
55+
DOMAIN_CONVERTERS,
56+
DOMAIN,
57+
EMBEDDABLE,
58+
ENTITY,
59+
EXTERNAL_DOMAIN,
60+
SCOPE,
61+
})
62+
@SupportedOptions({
63+
Options.CONFIG_PATH,
64+
Options.DAO_PACKAGE,
65+
Options.DAO_SUBPACKAGE,
66+
Options.DAO_SUFFIX,
67+
Options.DEBUG,
68+
Options.DOMAIN_CONVERTERS,
69+
Options.ENTITY_FIELD_PREFIX,
70+
Options.EXPR_FUNCTIONS,
71+
Options.LOMBOK_ALL_ARGS_CONSTRUCTOR,
72+
Options.LOMBOK_VALUE,
73+
Options.METAMODEL_ENABLED,
74+
Options.METAMODEL_PREFIX,
75+
Options.METAMODEL_SUFFIX,
76+
Options.RESOURCES_DIR,
77+
Options.SQL_VALIDATION,
78+
Options.TEST,
79+
Options.TRACE,
80+
Options.VERSION_VALIDATION,
81+
})
82+
public class DomaProcessor extends AbstractProcessor {
83+
84+
private final List<Operator> operators = new ArrayList<>();
85+
private ProcessingContext processingContext;
86+
87+
public DomaProcessor() {
88+
operators.add(new Operator(EXTERNAL_DOMAIN, DomaProcessor::processExternalDomainElements));
89+
operators.add(new Operator(DATA_TYPE, DomaProcessor::processDataTypeElements));
90+
operators.add(new Operator(DOMAIN, DomaProcessor::processDomainElements));
91+
operators.add(new Operator(DOMAIN_CONVERTERS, DomaProcessor::processDomainConvertersElements));
92+
operators.add(new Operator(EMBEDDABLE, DomaProcessor::processEmbeddableElements));
93+
operators.add(new Operator(ENTITY, DomaProcessor::processEntityElements));
94+
operators.add(
95+
new Operator(AGGREGATE_STRATEGY, DomaProcessor::processAggregateStrategyElements));
96+
operators.add(new Operator(DAO, DomaProcessor::processDaoElements));
97+
operators.add(new Operator(SCOPE, DomaProcessor::processScopeElements));
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 processAggregateStrategyElements(
131+
RoundContext roundContext, Set<? extends Element> elements) {
132+
var processor = new AggregateStrategyProcessor(roundContext);
133+
processor.process(elements);
134+
}
135+
136+
private static void processDaoElements(
137+
RoundContext roundContext, Set<? extends Element> elements) {
138+
var processor = new DaoProcessor(roundContext);
139+
processor.process(elements);
140+
}
141+
142+
private static void processDataTypeElements(
143+
RoundContext roundContext, Set<? extends Element> elements) {
144+
var processor = new DataTypeProcessor(roundContext);
145+
processor.process(elements);
146+
}
147+
148+
private static void processDomainConvertersElements(
149+
RoundContext roundContext, Set<? extends Element> elements) {
150+
var processor = new DomainConvertersProcessor(roundContext);
151+
processor.process(elements);
152+
}
153+
154+
private static void processDomainElements(
155+
RoundContext roundContext, Set<? extends Element> elements) {
156+
var processor = new DomainProcessor(roundContext);
157+
processor.process(elements);
158+
}
159+
160+
private static void processEmbeddableElements(
161+
RoundContext roundContext, Set<? extends Element> elements) {
162+
var processor = new EmbeddableProcessor(roundContext);
163+
processor.process(elements);
164+
}
165+
166+
private static void processEntityElements(
167+
RoundContext roundContext, Set<? extends Element> elements) {
168+
var processor = new EntityProcessor(roundContext);
169+
processor.process(elements);
170+
}
171+
172+
private static void processExternalDomainElements(
173+
RoundContext roundContext, Set<? extends Element> elements) {
174+
var processor = new ExternalDomainProcessor(roundContext);
175+
var metaList = processor.process(elements);
176+
roundContext.getExternalDomainMetaList().addAll(metaList);
177+
}
178+
179+
private static void processScopeElements(
180+
RoundContext roundContext, Set<? extends Element> elements) {
181+
var processor = new ScopeProcessor(roundContext);
182+
processor.process(elements);
183+
}
184+
185+
private record Operator(String name, BiConsumer<RoundContext, Set<? extends Element>> consumer) {
186+
Operator {
187+
Objects.requireNonNull(name);
188+
Objects.requireNonNull(consumer);
189+
}
190+
}
191+
}

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

+2-2
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

+2-2
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

+2-2
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
}

doma-processor/src/main/java/org/seasar/doma/internal/apt/Context.java renamed to doma-processor/src/main/java/org/seasar/doma/internal/apt/ProcessingContext.java

+2-42
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,19 @@
1616
package org.seasar.doma.internal.apt;
1717

1818
import javax.annotation.processing.ProcessingEnvironment;
19-
import org.seasar.doma.internal.apt.annot.Annotations;
20-
import org.seasar.doma.internal.apt.cttype.CtTypes;
21-
import org.seasar.doma.internal.apt.decl.Declarations;
2219

23-
public class Context {
20+
public class ProcessingContext {
2421

2522
private final ProcessingEnvironment env;
2623

2724
private boolean initialized;
28-
2925
private MoreElements moreElements;
30-
3126
private MoreTypes moreTypes;
32-
3327
private Options options;
34-
3528
private Reporter reporter;
36-
3729
private Resources resources;
3830

39-
private Annotations annotations;
40-
41-
private Declarations declarations;
42-
43-
private CtTypes ctTypes;
44-
45-
private Names names;
46-
47-
public Context(ProcessingEnvironment env) {
31+
public ProcessingContext(ProcessingEnvironment env) {
4832
this.env = env;
4933
}
5034

@@ -55,10 +39,6 @@ public void init() {
5539
moreElements = new MoreElements(this, env.getElementUtils());
5640
moreTypes = new MoreTypes(this, env.getTypeUtils());
5741
reporter = new Reporter(env.getMessager());
58-
annotations = new Annotations(this);
59-
declarations = new Declarations(this);
60-
ctTypes = new CtTypes(this);
61-
names = new Names(this);
6242
resources = new Resources(env.getFiler(), env.getOptions().get(Options.RESOURCES_DIR));
6343
options = new Options(env.getOptions(), resources);
6444
initialized = true;
@@ -89,26 +69,6 @@ public Resources getResources() {
8969
return resources;
9070
}
9171

92-
public Annotations getAnnotations() {
93-
assertInitialized();
94-
return annotations;
95-
}
96-
97-
public Declarations getDeclarations() {
98-
assertInitialized();
99-
return declarations;
100-
}
101-
102-
public CtTypes getCtTypes() {
103-
assertInitialized();
104-
return ctTypes;
105-
}
106-
107-
public Names getNames() {
108-
assertInitialized();
109-
return names;
110-
}
111-
11272
private void assertInitialized() {
11373
if (!initialized) {
11474
throw new AptIllegalStateException("not yet initialized");

0 commit comments

Comments
 (0)