Skip to content

Commit 7ac5f95

Browse files
authored
Refactor RoundContext (#1296)
2 parents c98fdb9 + 8dc58d2 commit 7ac5f95

File tree

7 files changed

+42
-11
lines changed

7 files changed

+42
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
116116
}
117117

118118
var roundContext = new RoundContext(processingContext, roundEnv, annotations);
119+
roundContext.init();
119120

120121
for (var operator : operators) {
121122
var elements = roundContext.getElementsAnnotatedWith(operator.name);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.seasar.doma.internal.apt;
1717

18+
import java.util.Objects;
1819
import javax.annotation.processing.ProcessingEnvironment;
1920

2021
public class ProcessingContext {
@@ -29,7 +30,7 @@ public class ProcessingContext {
2930
private Resources resources;
3031

3132
public ProcessingContext(ProcessingEnvironment env) {
32-
this.env = env;
33+
this.env = Objects.requireNonNull(env);
3334
}
3435

3536
public void init() {

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public class RoundContext {
3232
private final ProcessingContext processingContext;
3333
private final RoundEnvironment roundEnvironment;
3434
private final Set<? extends TypeElement> annotationElements;
35-
private final Annotations annotations;
36-
private final Declarations declarations;
37-
private final CtTypes ctTypes;
38-
private final Names names;
3935
private final List<ExternalDomainMeta> externalDomainMetaList = new ArrayList<>();
36+
private boolean initialized;
37+
private Annotations annotations;
38+
private Declarations declarations;
39+
private CtTypes ctTypes;
40+
private Names names;
4041

4142
public RoundContext(
4243
ProcessingContext processingContext,
@@ -45,62 +46,87 @@ public RoundContext(
4546
this.processingContext = Objects.requireNonNull(processingContext);
4647
this.roundEnvironment = Objects.requireNonNull(roundEnvironment);
4748
this.annotationElements = Objects.requireNonNull(annotationElements);
48-
this.annotations = new Annotations(this);
49-
this.declarations = new Declarations(this);
50-
this.ctTypes = new CtTypes(this);
51-
this.names = new Names(this);
49+
}
50+
51+
public void init() {
52+
if (initialized) {
53+
throw new AptIllegalStateException("already initialized");
54+
}
55+
annotations = new Annotations(this);
56+
declarations = new Declarations(this);
57+
ctTypes = new CtTypes(this);
58+
names = new Names(this);
59+
initialized = true;
5260
}
5361

5462
public RoundEnvironment getRoundEnvironment() {
63+
assertInitialized();
5564
return roundEnvironment;
5665
}
5766

5867
public MoreElements getMoreElements() {
68+
assertInitialized();
5969
return processingContext.getMoreElements();
6070
}
6171

6272
public MoreTypes getMoreTypes() {
73+
assertInitialized();
6374
return processingContext.getMoreTypes();
6475
}
6576

6677
public Options getOptions() {
78+
assertInitialized();
6779
return processingContext.getOptions();
6880
}
6981

7082
public Reporter getReporter() {
83+
assertInitialized();
7184
return processingContext.getReporter();
7285
}
7386

7487
public Resources getResources() {
88+
assertInitialized();
7589
return processingContext.getResources();
7690
}
7791

7892
public Annotations getAnnotations() {
93+
assertInitialized();
7994
return annotations;
8095
}
8196

8297
public Declarations getDeclarations() {
98+
assertInitialized();
8399
return declarations;
84100
}
85101

86102
public CtTypes getCtTypes() {
103+
assertInitialized();
87104
return ctTypes;
88105
}
89106

90107
public Names getNames() {
108+
assertInitialized();
91109
return names;
92110
}
93111

94112
public List<ExternalDomainMeta> getExternalDomainMetaList() {
113+
assertInitialized();
95114
return externalDomainMetaList;
96115
}
97116

98117
public Set<? extends Element> getElementsAnnotatedWith(String annotationName) {
118+
assertInitialized();
99119
Objects.requireNonNull(annotationName);
100120
return annotationElements.stream()
101121
.filter(a -> a.getQualifiedName().contentEquals(annotationName))
102122
.findFirst()
103123
.map(roundEnvironment::getElementsAnnotatedWith)
104124
.orElse(Set.of());
105125
}
126+
127+
private void assertInitialized() {
128+
if (!initialized) {
129+
throw new AptIllegalStateException("not yet initialized");
130+
}
131+
}
106132
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Objects;
2324
import java.util.function.BiFunction;
2425
import java.util.function.Function;
2526
import java.util.stream.Collectors;
@@ -68,7 +69,7 @@ public class Annotations {
6869
private final RoundContext ctx;
6970

7071
public Annotations(RoundContext ctx) {
71-
this.ctx = ctx;
72+
this.ctx = Objects.requireNonNull(ctx);
7273
}
7374

7475
public AggregateStrategyAnnot newAggregateStrategyAnnot(TypeElement typeElement) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public class CtTypes {
122122
private final RoundContext ctx;
123123

124124
public CtTypes(RoundContext ctx) {
125-
this.ctx = ctx;
125+
this.ctx = Objects.requireNonNull(ctx);
126126
}
127127

128128
public AggregateStrategyCtType newAggregateStrategyCtType(TypeMirror type) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public boolean process(
6767
return true;
6868
}
6969
RoundContext roundContext = new RoundContext(ctx, roundEnv, annotations);
70+
roundContext.init();
7071
run(roundContext);
7172
handled = true;
7273
return false;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
5959
return true;
6060
}
6161
var roundContext = new RoundContext(ctx, roundEnv, annotations);
62+
roundContext.init();
6263
var elements = roundContext.getElementsAnnotatedWith(MyAnnotation.class.getName());
6364
var processor = new MyElementProcessor(roundContext, handler);
6465
processor.process(elements);

0 commit comments

Comments
 (0)