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