24
24
25
25
@ GenerateAPContext
26
26
@ SupportedAnnotationTypes ({
27
- Constants . INJECTMODULE ,
28
- Constants . FACTORY ,
29
- Constants . SINGLETON ,
30
- Constants . COMPONENT ,
31
- Constants . PROTOTYPE ,
32
- Constants . SCOPE ,
27
+ InjectModulePrism . PRISM_TYPE ,
28
+ FactoryPrism . PRISM_TYPE ,
29
+ SingletonPrism . PRISM_TYPE ,
30
+ ComponentPrism . PRISM_TYPE ,
31
+ PrototypePrism . PRISM_TYPE ,
32
+ ScopePrism . PRISM_TYPE ,
33
33
Constants .TESTSCOPE ,
34
34
Constants .CONTROLLER ,
35
35
ImportPrism .PRISM_TYPE ,
@@ -61,56 +61,58 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
61
61
pluginFileProvided .forEach (defaultScope ::pluginProvided );
62
62
}
63
63
64
- /**
65
- * Loads provider files generated by avaje-inject-maven-plugin
66
- */
64
+ /** Loads provider files generated by avaje-inject-maven-plugin */
67
65
void loadProvidedFiles (Filer filer ) {
68
66
pluginFileProvided .addAll (lines (filer , "target/avaje-plugin-provides.txt" , "/target/classes" ));
69
67
moduleFileProvided .addAll (lines (filer , "target/avaje-module-provides.txt" , "/target/classes" ));
70
- pluginFileProvided .addAll (lines (filer , "build/avaje-plugin-provides.txt" , "/build/classes/java/main" ));
71
- moduleFileProvided .addAll (lines (filer , "build/avaje-module-provides.txt" , "/build/classes/java/main" ));
68
+ pluginFileProvided .addAll (
69
+ lines (filer , "build/avaje-plugin-provides.txt" , "/build/classes/java/main" ));
70
+ moduleFileProvided .addAll (
71
+ lines (filer , "build/avaje-module-provides.txt" , "/build/classes/java/main" ));
72
72
}
73
73
74
74
private static List <String > lines (Filer filer , String relativeName , String replace ) {
75
75
try {
76
76
final String resource = resource (filer , relativeName , replace );
77
77
try (var inputStream = new URI (resource ).toURL ().openStream ();
78
- var reader = new BufferedReader (new InputStreamReader (inputStream ))) {
78
+ var reader = new BufferedReader (new InputStreamReader (inputStream ))) {
79
79
return reader .lines ().collect (Collectors .toList ());
80
80
}
81
81
} catch (final Exception e ) {
82
82
return Collections .emptyList ();
83
83
}
84
84
}
85
85
86
- private static String resource (Filer filer , String relativeName , String replace ) throws IOException {
86
+ private static String resource (Filer filer , String relativeName , String replace )
87
+ throws IOException {
87
88
return filer
88
- .getResource (StandardLocation .CLASS_OUTPUT , "" , relativeName )
89
- .toUri ()
90
- .toString ()
91
- .replace (replace , "" );
89
+ .getResource (StandardLocation .CLASS_OUTPUT , "" , relativeName )
90
+ .toUri ()
91
+ .toString ()
92
+ .replace (replace , "" );
92
93
}
93
94
94
95
@ Override
95
96
public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
96
97
97
98
APContext .setProjectModuleElement (annotations , roundEnv );
98
99
readModule (roundEnv );
100
+
99
101
addImportedAspects (importedAspects (roundEnv ));
100
- readScopes (roundEnv .getElementsAnnotatedWith (typeElement (Constants .SCOPE )));
101
- readFactories (roundEnv .getElementsAnnotatedWith (typeElement (Constants .FACTORY )));
102
+ getElements (roundEnv , ScopePrism .PRISM_TYPE ).ifPresent (this ::readScopes );
103
+ getElements (roundEnv , FactoryPrism .PRISM_TYPE ).ifPresent (this ::readFactories );
104
+
102
105
if (defaultScope .includeSingleton ()) {
103
- readBeans (roundEnv . getElementsAnnotatedWith ( typeElement ( Constants . SINGLETON )) );
106
+ getElements (roundEnv , SingletonPrism . PRISM_TYPE ). ifPresent ( this :: readBeans );
104
107
}
105
- readBeans (roundEnv .getElementsAnnotatedWith (typeElement (Constants .COMPONENT )));
106
- readBeans (roundEnv .getElementsAnnotatedWith (typeElement (Constants .PROTOTYPE )));
108
+ getElements (roundEnv , ComponentPrism .PRISM_TYPE ).ifPresent (this ::readBeans );
109
+ getElements (roundEnv , PrototypePrism .PRISM_TYPE ).ifPresent (this ::readBeans );
110
+
107
111
readImported (importedElements (roundEnv ));
108
- readBeans (roundEnv .getElementsAnnotatedWith (typeElement (Constants .PROTOTYPE )));
109
- final var typeElement = elementUtils .getTypeElement (Constants .CONTROLLER );
110
- if (typeElement != null ) {
111
- readBeans (roundEnv .getElementsAnnotatedWith (typeElement ));
112
- }
113
- readBeans (roundEnv .getElementsAnnotatedWith (typeElement (Constants .PROXY )));
112
+
113
+ getElements (roundEnv , Constants .CONTROLLER ).ifPresent (this ::readBeans );
114
+ getElements (roundEnv , ProxyPrism .PRISM_TYPE ).ifPresent (this ::readBeans );
115
+
114
116
allScopes .readBeans (roundEnv );
115
117
defaultScope .write (roundEnv .processingOver ());
116
118
allScopes .write (roundEnv .processingOver ());
@@ -121,13 +123,20 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
121
123
return false ;
122
124
}
123
125
126
+ // Optional because these annotations are not guaranteed to exist
127
+ private static Optional <? extends Set <? extends Element >> getElements (
128
+ RoundEnvironment round , String name ) {
129
+ return Optional .ofNullable (typeElement (name )).map (round ::getElementsAnnotatedWith );
130
+ }
131
+
124
132
private Set <TypeElement > importedElements (RoundEnvironment roundEnv ) {
125
- return roundEnv .getElementsAnnotatedWith (typeElement (ImportPrism .PRISM_TYPE )).stream ()
126
- .map (ImportPrism ::getInstanceOn )
127
- .flatMap (p -> p .value ().stream ())
128
- .map (ProcessingContext ::asElement )
129
- .filter (this ::notAlreadyProvided )
130
- .collect (Collectors .toSet ());
133
+ return getElements (roundEnv , ImportPrism .PRISM_TYPE ).stream ()
134
+ .flatMap (Set ::stream )
135
+ .map (ImportPrism ::getInstanceOn )
136
+ .flatMap (p -> p .value ().stream ())
137
+ .map (ProcessingContext ::asElement )
138
+ .filter (this ::notAlreadyProvided )
139
+ .collect (Collectors .toSet ());
131
140
}
132
141
133
142
private boolean notAlreadyProvided (TypeElement e ) {
@@ -136,12 +145,10 @@ private boolean notAlreadyProvided(TypeElement e) {
136
145
}
137
146
138
147
private static Map <String , AspectImportPrism > importedAspects (RoundEnvironment roundEnv ) {
139
- return Optional .ofNullable (typeElement (AspectImportPrism .PRISM_TYPE ))
140
- .map (roundEnv ::getElementsAnnotatedWith )
141
- .stream ()
142
- .flatMap (Set ::stream )
143
- .map (AspectImportPrism ::getInstanceOn )
144
- .collect (Collectors .toMap (p -> p .value ().toString (), p -> p ));
148
+ return getElements (roundEnv , AspectImportPrism .PRISM_TYPE ).stream ()
149
+ .flatMap (Set ::stream )
150
+ .map (AspectImportPrism ::getInstanceOn )
151
+ .collect (Collectors .toMap (p -> p .value ().toString (), p -> p ));
145
152
}
146
153
147
154
private void readScopes (Set <? extends Element > scopes ) {
@@ -154,9 +161,7 @@ private void readScopes(Set<? extends Element> scopes) {
154
161
addTestScope ();
155
162
}
156
163
157
- /**
158
- * Add built-in test scope for <code>@TestScope</code> if available.
159
- */
164
+ /** Add built-in test scope for <code>@TestScope</code> if available. */
160
165
private void addTestScope () {
161
166
final var testScopeType = elementUtils .getTypeElement (Constants .TESTSCOPE );
162
167
if (testScopeType != null ) {
@@ -177,7 +182,8 @@ private void readImported(Set<? extends Element> beans) {
177
182
}
178
183
179
184
/** Read the beans that have changed. */
180
- private void readChangedBeans (Set <TypeElement > beans , boolean factory , boolean importedComponent ) {
185
+ private void readChangedBeans (
186
+ Set <TypeElement > beans , boolean factory , boolean importedComponent ) {
181
187
for (final var typeElement : beans ) {
182
188
if (typeElement .getKind () == ElementKind .INTERFACE ) {
183
189
continue ;
@@ -196,9 +202,7 @@ private void readChangedBeans(Set<TypeElement> beans, boolean factory, boolean i
196
202
}
197
203
}
198
204
199
- /**
200
- * Find the scope if the Factory has a scope annotation.
201
- */
205
+ /** Find the scope if the Factory has a scope annotation. */
202
206
private ScopeInfo findScope (Element element ) {
203
207
for (final AnnotationMirror annotationMirror : element .getAnnotationMirrors ()) {
204
208
final var scopeInfo = allScopes .get (annotationMirror .getAnnotationType ().toString ());
@@ -209,9 +213,7 @@ private ScopeInfo findScope(Element element) {
209
213
return null ;
210
214
}
211
215
212
- /**
213
- * Read the existing meta-data from InjectModule (if found) and the factory bean (if exists).
214
- */
216
+ /** Read the existing meta-data from InjectModule (if found) and the factory bean (if exists). */
215
217
private void readModule (RoundEnvironment roundEnv ) {
216
218
if (readModuleInfo ) {
217
219
// only read the module meta data once
@@ -229,20 +231,22 @@ private void readModule(RoundEnvironment roundEnv) {
229
231
readInjectModule (roundEnv );
230
232
}
231
233
232
- /**
233
- * Read InjectModule for things like package-info etc (not for custom scopes)
234
- */
234
+ /** Read InjectModule for things like package-info etc (not for custom scopes) */
235
235
private void readInjectModule (RoundEnvironment roundEnv ) {
236
+
236
237
// read other that are annotated with InjectModule
237
- for (final Element element : roundEnv .getElementsAnnotatedWith (typeElement (Constants .INJECTMODULE ))) {
238
- final var scope = ScopePrism .getInstanceOn (element );
239
- if (scope == null ) {
240
- // it it not a custom scope annotation
241
- final var annotation = InjectModulePrism .getInstanceOn (element );
242
- if (annotation != null ) {
243
- defaultScope .details (annotation .name (), element );
244
- }
245
- }
246
- }
238
+ getElements (roundEnv , InjectModulePrism .PRISM_TYPE ).stream ()
239
+ .flatMap (Set ::stream )
240
+ .forEach (
241
+ element -> {
242
+ final var scope = ScopePrism .getInstanceOn (element );
243
+ if (scope == null ) {
244
+ // it it not a custom scope annotation
245
+ final var annotation = InjectModulePrism .getInstanceOn (element );
246
+ if (annotation != null ) {
247
+ defaultScope .details (annotation .name (), element );
248
+ }
249
+ }
250
+ });
247
251
}
248
252
}
0 commit comments