Skip to content

Commit 1768ac1

Browse files
committed
Refactor annotation processing workflow for clarity and efficiency
1 parent 9318d95 commit 1768ac1

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

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

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import static org.seasar.doma.internal.apt.AnnotationTypes.EXTERNAL_DOMAIN;
2626
import static org.seasar.doma.internal.apt.AnnotationTypes.SCOPE;
2727

28-
import java.util.List;
29-
import java.util.Objects;
28+
import java.util.HashSet;
29+
import java.util.Map;
3030
import java.util.Set;
3131
import java.util.function.Function;
3232
import javax.annotation.processing.AbstractProcessor;
@@ -81,17 +81,17 @@
8181
})
8282
public class DomaProcessor extends AbstractProcessor {
8383

84-
private static final List<Operator> operators =
85-
List.of(
86-
new Operator(EXTERNAL_DOMAIN, ExternalDomainProcessor::new),
87-
new Operator(DATA_TYPE, DataTypeProcessor::new),
88-
new Operator(DOMAIN, DomainProcessor::new),
89-
new Operator(DOMAIN_CONVERTERS, DomainConvertersProcessor::new),
90-
new Operator(EMBEDDABLE, EmbeddableProcessor::new),
91-
new Operator(ENTITY, EntityProcessor::new),
92-
new Operator(AGGREGATE_STRATEGY, AggregateStrategyProcessor::new),
93-
new Operator(DAO, DaoProcessor::new),
94-
new Operator(SCOPE, ScopeProcessor::new));
84+
private static final Map<String, Function<RoundContext, ElementProcessor>> functionMap =
85+
Map.of(
86+
EXTERNAL_DOMAIN, ExternalDomainProcessor::new,
87+
DATA_TYPE, DataTypeProcessor::new,
88+
DOMAIN, DomainProcessor::new,
89+
DOMAIN_CONVERTERS, DomainConvertersProcessor::new,
90+
EMBEDDABLE, EmbeddableProcessor::new,
91+
ENTITY, EntityProcessor::new,
92+
AGGREGATE_STRATEGY, AggregateStrategyProcessor::new,
93+
DAO, DaoProcessor::new,
94+
SCOPE, ScopeProcessor::new);
9595

9696
private ProcessingContext processingContext;
9797

@@ -113,24 +113,39 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
113113
return true;
114114
}
115115

116-
var roundContext = processingContext.createRoundContext(annotations, roundEnv);
116+
var roundContext = processingContext.createRoundContext(roundEnv);
117117

118-
for (var operator : operators) {
119-
var elements = roundContext.getElementsAnnotatedWith(operator.annotationName);
120-
if (!elements.isEmpty()) {
121-
var processor = operator.function.apply(roundContext);
122-
processor.process(elements);
118+
var externalDomainAnnotation = new HashSet<TypeElement>(1);
119+
var otherAnnotations = new HashSet<TypeElement>(functionMap.size() - 1);
120+
121+
for (var annotation : annotations) {
122+
if (annotation.getQualifiedName().contentEquals(EXTERNAL_DOMAIN)) {
123+
externalDomainAnnotation.add(annotation);
124+
} else {
125+
otherAnnotations.add(annotation);
123126
}
124127
}
125128

129+
// process ExternalDomain annotation first
130+
processWithRoundContext(externalDomainAnnotation, roundContext);
131+
132+
// process other annotations
133+
processWithRoundContext(otherAnnotations, roundContext);
134+
126135
return true;
127136
}
128137

129-
private record Operator(
130-
String annotationName, Function<RoundContext, ElementProcessor> function) {
131-
Operator {
132-
Objects.requireNonNull(annotationName);
133-
Objects.requireNonNull(function);
138+
private void processWithRoundContext(Set<TypeElement> annotations, RoundContext roundContext) {
139+
for (var annotation : annotations) {
140+
String annotationName = annotation.getQualifiedName().toString();
141+
var function = functionMap.get(annotationName);
142+
if (function != null) {
143+
var elements = roundContext.getElementsAnnotatedWith(annotation);
144+
if (!elements.isEmpty()) {
145+
var processor = function.apply(roundContext);
146+
processor.process(elements);
147+
}
148+
}
134149
}
135150
}
136151
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
package org.seasar.doma.internal.apt;
1717

1818
import java.util.Objects;
19-
import java.util.Set;
2019
import javax.annotation.processing.ProcessingEnvironment;
2120
import javax.annotation.processing.RoundEnvironment;
22-
import javax.lang.model.element.TypeElement;
2321

2422
public class ProcessingContext {
2523

@@ -68,11 +66,9 @@ private boolean isDebug(ProcessingEnvironment env) {
6866
return Boolean.parseBoolean(debug);
6967
}
7068

71-
public RoundContext createRoundContext(
72-
Set<? extends TypeElement> annotationElements, RoundEnvironment roundEnvironment) {
73-
Objects.requireNonNull(annotationElements);
69+
public RoundContext createRoundContext(RoundEnvironment roundEnvironment) {
7470
Objects.requireNonNull(roundEnvironment);
75-
var roundContext = new RoundContext(this, roundEnvironment, annotationElements);
71+
var roundContext = new RoundContext(this, roundEnvironment);
7672
roundContext.init();
7773
return roundContext;
7874
}

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,16 @@ public class RoundContext {
3131

3232
private final ProcessingContext processingContext;
3333
private final RoundEnvironment roundEnvironment;
34-
private final Set<? extends TypeElement> annotationElements;
3534
private final List<ExternalDomainMeta> externalDomainMetaList = new ArrayList<>();
3635
private boolean initialized;
3736
private Annotations annotations;
3837
private Declarations declarations;
3938
private CtTypes ctTypes;
4039
private Names names;
4140

42-
RoundContext(
43-
ProcessingContext processingContext,
44-
RoundEnvironment roundEnvironment,
45-
Set<? extends TypeElement> annotationElements) {
41+
RoundContext(ProcessingContext processingContext, RoundEnvironment roundEnvironment) {
4642
this.processingContext = Objects.requireNonNull(processingContext);
4743
this.roundEnvironment = Objects.requireNonNull(roundEnvironment);
48-
this.annotationElements = Objects.requireNonNull(annotationElements);
4944
}
5045

5146
void init() {
@@ -114,14 +109,10 @@ public List<ExternalDomainMeta> getExternalDomainMetaList() {
114109
return externalDomainMetaList;
115110
}
116111

117-
public Set<? extends Element> getElementsAnnotatedWith(String annotationName) {
112+
public Set<? extends Element> getElementsAnnotatedWith(TypeElement annotation) {
118113
assertInitialized();
119-
Objects.requireNonNull(annotationName);
120-
return annotationElements.stream()
121-
.filter(a -> a.getQualifiedName().contentEquals(annotationName))
122-
.findFirst()
123-
.map(roundEnvironment::getElementsAnnotatedWith)
124-
.orElse(Set.of());
114+
Objects.requireNonNull(annotation);
115+
return roundEnvironment.getElementsAnnotatedWith(annotation);
125116
}
126117

127118
private void assertInitialized() {

doma-processor/src/test/java/org/seasar/doma/internal/apt/TestProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public boolean process(
6666
if (roundEnv.processingOver() || handled) {
6767
return true;
6868
}
69-
RoundContext roundContext = ctx.createRoundContext(annotations, roundEnv);
69+
RoundContext roundContext = ctx.createRoundContext(roundEnv);
7070
run(roundContext);
7171
handled = true;
7272
return false;

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/MyAnnotationProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
5757
if (roundEnv.processingOver()) {
5858
return true;
5959
}
60-
var roundContext = ctx.createRoundContext(annotations, roundEnv);
61-
var elements = roundContext.getElementsAnnotatedWith(MyAnnotation.class.getName());
60+
var roundContext = ctx.createRoundContext(roundEnv);
61+
var elements = roundContext.getElementsAnnotatedWith(annotations.iterator().next());
6262
var processor = new MyElementProcessor(roundContext, handler);
6363
processor.process(elements);
6464
return true;

0 commit comments

Comments
 (0)