Skip to content

Commit c403249

Browse files
authored
Merge pull request #448 from avaje/feautre/tidy-Processor
Rename Processor.getElements() -> maybeElements() and reformat some bits
2 parents 4bf5ac1 + 22b644f commit c403249

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/FieldReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ final class FieldReader {
2424
this.isBeanMap = QualifiedMapPrism.isPresent(element);
2525
this.fieldType = Util.unwrapProvider(utype.rawType(isBeanMap));
2626
this.type = GenericType.parse(utype.rawType(isBeanMap));
27-
if (nullable || element.asType().toString().startsWith("java.util.Optional<"))
27+
if (nullable || element.asType().toString().startsWith("java.util.Optional<")) {
2828
ProcessingContext.addOptionalType(fieldType);
29+
}
2930
}
3031

3132
boolean isGenericParam() {

inject-generator/src/main/java/io/avaje/inject/generator/Processor.java

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.avaje.inject.generator;
22

3-
import static io.avaje.inject.generator.APContext.*;
4-
import static io.avaje.inject.generator.ProcessingContext.*;
3+
import io.avaje.prism.GenerateAPContext;
54

65
import javax.annotation.processing.*;
76
import javax.lang.model.SourceVersion;
@@ -12,16 +11,16 @@
1211
import javax.lang.model.util.ElementFilter;
1312
import javax.lang.model.util.Elements;
1413
import javax.tools.StandardLocation;
15-
16-
import io.avaje.prism.GenerateAPContext;
17-
1814
import java.io.BufferedReader;
1915
import java.io.IOException;
2016
import java.io.InputStreamReader;
2117
import java.net.URI;
2218
import java.util.*;
2319
import java.util.stream.Collectors;
2420

21+
import static io.avaje.inject.generator.APContext.typeElement;
22+
import static io.avaje.inject.generator.ProcessingContext.*;
23+
2524
@GenerateAPContext
2625
@SupportedAnnotationTypes({
2726
InjectModulePrism.PRISM_TYPE,
@@ -61,57 +60,55 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
6160
pluginFileProvided.forEach(defaultScope::pluginProvided);
6261
}
6362

64-
/** Loads provider files generated by avaje-inject-maven-plugin */
63+
/**
64+
* Loads provider files generated by avaje-inject-maven-plugin
65+
*/
6566
void loadProvidedFiles(Filer filer) {
6667
pluginFileProvided.addAll(lines(filer, "target/avaje-plugin-provides.txt", "/target/classes"));
6768
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"));
7271
}
7372

7473
private static List<String> lines(Filer filer, String relativeName, String replace) {
7574
try {
7675
final String resource = resource(filer, relativeName, replace);
7776
try (var inputStream = new URI(resource).toURL().openStream();
78-
var reader = new BufferedReader(new InputStreamReader(inputStream))) {
77+
var reader = new BufferedReader(new InputStreamReader(inputStream))) {
7978
return reader.lines().collect(Collectors.toList());
8079
}
8180
} catch (final Exception e) {
8281
return Collections.emptyList();
8382
}
8483
}
8584

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 {
8886
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, "");
9391
}
9492

9593
@Override
9694
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
97-
9895
APContext.setProjectModuleElement(annotations, roundEnv);
9996
readModule(roundEnv);
10097

10198
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);
104101

105102
if (defaultScope.includeSingleton()) {
106-
getElements(roundEnv, SingletonPrism.PRISM_TYPE).ifPresent(this::readBeans);
103+
maybeElements(roundEnv, SingletonPrism.PRISM_TYPE).ifPresent(this::readBeans);
107104
}
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);
110107

111108
readImported(importedElements(roundEnv));
112109

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);
115112

116113
allScopes.readBeans(roundEnv);
117114
defaultScope.write(roundEnv.processingOver());
@@ -124,19 +121,18 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
124121
}
125122

126123
// 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) {
129125
return Optional.ofNullable(typeElement(name)).map(round::getElementsAnnotatedWith);
130126
}
131127

132128
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());
140136
}
141137

142138
private boolean notAlreadyProvided(TypeElement e) {
@@ -145,10 +141,10 @@ private boolean notAlreadyProvided(TypeElement e) {
145141
}
146142

147143
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));
152148
}
153149

154150
private void readScopes(Set<? extends Element> scopes) {
@@ -161,7 +157,9 @@ private void readScopes(Set<? extends Element> scopes) {
161157
addTestScope();
162158
}
163159

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+
*/
165163
private void addTestScope() {
166164
final var testScopeType = elementUtils.getTypeElement(Constants.TESTSCOPE);
167165
if (testScopeType != null) {
@@ -181,9 +179,10 @@ private void readImported(Set<? extends Element> beans) {
181179
readChangedBeans(ElementFilter.typesIn(beans), false, true);
182180
}
183181

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) {
187186
for (final var typeElement : beans) {
188187
if (typeElement.getKind() == ElementKind.INTERFACE) {
189188
continue;
@@ -202,7 +201,9 @@ private void readChangedBeans(
202201
}
203202
}
204203

205-
/** Find the scope if the Factory has a scope annotation. */
204+
/**
205+
* Find the scope if the Factory has a scope annotation.
206+
*/
206207
private ScopeInfo findScope(Element element) {
207208
for (final AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
208209
final var scopeInfo = allScopes.get(annotationMirror.getAnnotationType().toString());
@@ -213,7 +214,9 @@ private ScopeInfo findScope(Element element) {
213214
return null;
214215
}
215216

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+
*/
217220
private void readModule(RoundEnvironment roundEnv) {
218221
if (readModuleInfo) {
219222
// only read the module meta data once
@@ -231,22 +234,22 @@ private void readModule(RoundEnvironment roundEnv) {
231234
readInjectModule(roundEnv);
232235
}
233236

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+
*/
235240
private void readInjectModule(RoundEnvironment roundEnv) {
236-
237241
// 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+
});
251254
}
252255
}

0 commit comments

Comments
 (0)