Skip to content

Commit

Permalink
Rename Processor.getElements() -> maybeElements() and reformat some bits
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Oct 27, 2023
1 parent 4bf5ac1 commit 22b644f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ final class FieldReader {
this.isBeanMap = QualifiedMapPrism.isPresent(element);
this.fieldType = Util.unwrapProvider(utype.rawType(isBeanMap));
this.type = GenericType.parse(utype.rawType(isBeanMap));
if (nullable || element.asType().toString().startsWith("java.util.Optional<"))
if (nullable || element.asType().toString().startsWith("java.util.Optional<")) {
ProcessingContext.addOptionalType(fieldType);
}
}

boolean isGenericParam() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.avaje.inject.generator;

import static io.avaje.inject.generator.APContext.*;
import static io.avaje.inject.generator.ProcessingContext.*;
import io.avaje.prism.GenerateAPContext;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
Expand All @@ -12,16 +11,16 @@
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.tools.StandardLocation;

import io.avaje.prism.GenerateAPContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;

import static io.avaje.inject.generator.APContext.typeElement;
import static io.avaje.inject.generator.ProcessingContext.*;

@GenerateAPContext
@SupportedAnnotationTypes({
InjectModulePrism.PRISM_TYPE,
Expand Down Expand Up @@ -61,57 +60,55 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
pluginFileProvided.forEach(defaultScope::pluginProvided);
}

/** Loads provider files generated by avaje-inject-maven-plugin */
/**
* Loads provider files generated by avaje-inject-maven-plugin
*/
void loadProvidedFiles(Filer filer) {
pluginFileProvided.addAll(lines(filer, "target/avaje-plugin-provides.txt", "/target/classes"));
moduleFileProvided.addAll(lines(filer, "target/avaje-module-provides.txt", "/target/classes"));
pluginFileProvided.addAll(
lines(filer, "build/avaje-plugin-provides.txt", "/build/classes/java/main"));
moduleFileProvided.addAll(
lines(filer, "build/avaje-module-provides.txt", "/build/classes/java/main"));
pluginFileProvided.addAll(lines(filer, "build/avaje-plugin-provides.txt", "/build/classes/java/main"));
moduleFileProvided.addAll(lines(filer, "build/avaje-module-provides.txt", "/build/classes/java/main"));
}

private static List<String> lines(Filer filer, String relativeName, String replace) {
try {
final String resource = resource(filer, relativeName, replace);
try (var inputStream = new URI(resource).toURL().openStream();
var reader = new BufferedReader(new InputStreamReader(inputStream))) {
var reader = new BufferedReader(new InputStreamReader(inputStream))) {
return reader.lines().collect(Collectors.toList());
}
} catch (final Exception e) {
return Collections.emptyList();
}
}

private static String resource(Filer filer, String relativeName, String replace)
throws IOException {
private static String resource(Filer filer, String relativeName, String replace) throws IOException {
return filer
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
.toUri()
.toString()
.replace(replace, "");
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
.toUri()
.toString()
.replace(replace, "");
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

APContext.setProjectModuleElement(annotations, roundEnv);
readModule(roundEnv);

addImportedAspects(importedAspects(roundEnv));
getElements(roundEnv, ScopePrism.PRISM_TYPE).ifPresent(this::readScopes);
getElements(roundEnv, FactoryPrism.PRISM_TYPE).ifPresent(this::readFactories);
maybeElements(roundEnv, ScopePrism.PRISM_TYPE).ifPresent(this::readScopes);
maybeElements(roundEnv, FactoryPrism.PRISM_TYPE).ifPresent(this::readFactories);

if (defaultScope.includeSingleton()) {
getElements(roundEnv, SingletonPrism.PRISM_TYPE).ifPresent(this::readBeans);
maybeElements(roundEnv, SingletonPrism.PRISM_TYPE).ifPresent(this::readBeans);
}
getElements(roundEnv, ComponentPrism.PRISM_TYPE).ifPresent(this::readBeans);
getElements(roundEnv, PrototypePrism.PRISM_TYPE).ifPresent(this::readBeans);
maybeElements(roundEnv, ComponentPrism.PRISM_TYPE).ifPresent(this::readBeans);
maybeElements(roundEnv, PrototypePrism.PRISM_TYPE).ifPresent(this::readBeans);

readImported(importedElements(roundEnv));

getElements(roundEnv, Constants.CONTROLLER).ifPresent(this::readBeans);
getElements(roundEnv, ProxyPrism.PRISM_TYPE).ifPresent(this::readBeans);
maybeElements(roundEnv, Constants.CONTROLLER).ifPresent(this::readBeans);
maybeElements(roundEnv, ProxyPrism.PRISM_TYPE).ifPresent(this::readBeans);

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

// Optional because these annotations are not guaranteed to exist
private static Optional<? extends Set<? extends Element>> getElements(
RoundEnvironment round, String name) {
private static Optional<? extends Set<? extends Element>> maybeElements(RoundEnvironment round, String name) {
return Optional.ofNullable(typeElement(name)).map(round::getElementsAnnotatedWith);
}

private Set<TypeElement> importedElements(RoundEnvironment roundEnv) {
return getElements(roundEnv, ImportPrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.map(ImportPrism::getInstanceOn)
.flatMap(p -> p.value().stream())
.map(ProcessingContext::asElement)
.filter(this::notAlreadyProvided)
.collect(Collectors.toSet());
return maybeElements(roundEnv, ImportPrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.map(ImportPrism::getInstanceOn)
.flatMap(p -> p.value().stream())
.map(ProcessingContext::asElement)
.filter(this::notAlreadyProvided)
.collect(Collectors.toSet());
}

private boolean notAlreadyProvided(TypeElement e) {
Expand All @@ -145,10 +141,10 @@ private boolean notAlreadyProvided(TypeElement e) {
}

private static Map<String, AspectImportPrism> importedAspects(RoundEnvironment roundEnv) {
return getElements(roundEnv, AspectImportPrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.map(AspectImportPrism::getInstanceOn)
.collect(Collectors.toMap(p -> p.value().toString(), p -> p));
return maybeElements(roundEnv, AspectImportPrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.map(AspectImportPrism::getInstanceOn)
.collect(Collectors.toMap(p -> p.value().toString(), p -> p));
}

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

/** Add built-in test scope for <code>@TestScope</code> if available. */
/**
* Add built-in test scope for <code>@TestScope</code> if available.
*/
private void addTestScope() {
final var testScopeType = elementUtils.getTypeElement(Constants.TESTSCOPE);
if (testScopeType != null) {
Expand All @@ -181,9 +179,10 @@ private void readImported(Set<? extends Element> beans) {
readChangedBeans(ElementFilter.typesIn(beans), false, true);
}

/** Read the beans that have changed. */
private void readChangedBeans(
Set<TypeElement> beans, boolean factory, boolean importedComponent) {
/**
* Read the beans that have changed.
*/
private void readChangedBeans(Set<TypeElement> beans, boolean factory, boolean importedComponent) {
for (final var typeElement : beans) {
if (typeElement.getKind() == ElementKind.INTERFACE) {
continue;
Expand All @@ -202,7 +201,9 @@ private void readChangedBeans(
}
}

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

/** Read the existing meta-data from InjectModule (if found) and the factory bean (if exists). */
/**
* Read the existing meta-data from InjectModule (if found) and the factory bean (if exists).
*/
private void readModule(RoundEnvironment roundEnv) {
if (readModuleInfo) {
// only read the module meta data once
Expand All @@ -231,22 +234,22 @@ private void readModule(RoundEnvironment roundEnv) {
readInjectModule(roundEnv);
}

/** Read InjectModule for things like package-info etc (not for custom scopes) */
/**
* Read InjectModule for things like package-info etc (not for custom scopes)
*/
private void readInjectModule(RoundEnvironment roundEnv) {

// read other that are annotated with InjectModule
getElements(roundEnv, InjectModulePrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.forEach(
element -> {
final var scope = ScopePrism.getInstanceOn(element);
if (scope == null) {
// it it not a custom scope annotation
final var annotation = InjectModulePrism.getInstanceOn(element);
if (annotation != null) {
defaultScope.details(annotation.name(), element);
}
}
});
maybeElements(roundEnv, InjectModulePrism.PRISM_TYPE).stream()
.flatMap(Set::stream)
.forEach(element -> {
final var scope = ScopePrism.getInstanceOn(element);
if (scope == null) {
// it it not a custom scope annotation
final var annotation = InjectModulePrism.getInstanceOn(element);
if (annotation != null) {
defaultScope.details(annotation.name(), element);
}
}
});
}
}

0 comments on commit 22b644f

Please sign in to comment.