2424
2525@ GenerateAPContext
2626@ 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 ,
3333 Constants .TESTSCOPE ,
3434 Constants .CONTROLLER ,
3535 ImportPrism .PRISM_TYPE ,
@@ -61,56 +61,58 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
6161 pluginFileProvided .forEach (defaultScope ::pluginProvided );
6262 }
6363
64- /**
65- * Loads provider files generated by avaje-inject-maven-plugin
66- */
64+ /** Loads provider files generated by avaje-inject-maven-plugin */
6765 void loadProvidedFiles (Filer filer ) {
6866 pluginFileProvided .addAll (lines (filer , "target/avaje-plugin-provides.txt" , "/target/classes" ));
6967 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" ));
7272 }
7373
7474 private static List <String > lines (Filer filer , String relativeName , String replace ) {
7575 try {
7676 final String resource = resource (filer , relativeName , replace );
7777 try (var inputStream = new URI (resource ).toURL ().openStream ();
78- var reader = new BufferedReader (new InputStreamReader (inputStream ))) {
78+ var reader = new BufferedReader (new InputStreamReader (inputStream ))) {
7979 return reader .lines ().collect (Collectors .toList ());
8080 }
8181 } catch (final Exception e ) {
8282 return Collections .emptyList ();
8383 }
8484 }
8585
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 {
8788 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 , "" );
9293 }
9394
9495 @ Override
9596 public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
9697
9798 APContext .setProjectModuleElement (annotations , roundEnv );
9899 readModule (roundEnv );
100+
99101 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+
102105 if (defaultScope .includeSingleton ()) {
103- readBeans (roundEnv . getElementsAnnotatedWith ( typeElement ( Constants . SINGLETON )) );
106+ getElements (roundEnv , SingletonPrism . PRISM_TYPE ). ifPresent ( this :: readBeans );
104107 }
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+
107111 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+
114116 allScopes .readBeans (roundEnv );
115117 defaultScope .write (roundEnv .processingOver ());
116118 allScopes .write (roundEnv .processingOver ());
@@ -121,13 +123,20 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
121123 return false ;
122124 }
123125
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+
124132 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 ());
131140 }
132141
133142 private boolean notAlreadyProvided (TypeElement e ) {
@@ -136,12 +145,10 @@ private boolean notAlreadyProvided(TypeElement e) {
136145 }
137146
138147 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 ));
145152 }
146153
147154 private void readScopes (Set <? extends Element > scopes ) {
@@ -154,9 +161,7 @@ private void readScopes(Set<? extends Element> scopes) {
154161 addTestScope ();
155162 }
156163
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. */
160165 private void addTestScope () {
161166 final var testScopeType = elementUtils .getTypeElement (Constants .TESTSCOPE );
162167 if (testScopeType != null ) {
@@ -177,7 +182,8 @@ private void readImported(Set<? extends Element> beans) {
177182 }
178183
179184 /** 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 ) {
181187 for (final var typeElement : beans ) {
182188 if (typeElement .getKind () == ElementKind .INTERFACE ) {
183189 continue ;
@@ -196,9 +202,7 @@ private void readChangedBeans(Set<TypeElement> beans, boolean factory, boolean i
196202 }
197203 }
198204
199- /**
200- * Find the scope if the Factory has a scope annotation.
201- */
205+ /** Find the scope if the Factory has a scope annotation. */
202206 private ScopeInfo findScope (Element element ) {
203207 for (final AnnotationMirror annotationMirror : element .getAnnotationMirrors ()) {
204208 final var scopeInfo = allScopes .get (annotationMirror .getAnnotationType ().toString ());
@@ -209,9 +213,7 @@ private ScopeInfo findScope(Element element) {
209213 return null ;
210214 }
211215
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). */
215217 private void readModule (RoundEnvironment roundEnv ) {
216218 if (readModuleInfo ) {
217219 // only read the module meta data once
@@ -229,20 +231,22 @@ private void readModule(RoundEnvironment roundEnv) {
229231 readInjectModule (roundEnv );
230232 }
231233
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) */
235235 private void readInjectModule (RoundEnvironment roundEnv ) {
236+
236237 // 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+ });
247251 }
248252}
0 commit comments