Skip to content

Commit fdd92f1

Browse files
committed
Refactor RoundContext to avoid leaking the 'this' reference
1 parent 01bcd85 commit fdd92f1

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
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/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/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)