Skip to content

Commit a6fae5b

Browse files
authored
Merge branch 'master' into NCDF
2 parents f521cf5 + f2b6ad1 commit a6fae5b

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

README.md

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,6 @@ BeanScope beanScope = BeanScope.builder().build()
5959
Example ex = beanScope.get(Example.class);
6060
```
6161

62-
### Java Module Usage
63-
When working with Java modules you need to add a `provides` statement in your `module-info.java` with the generated class.
64-
```java
65-
import io.avaje.inject.spi.InjectExtension;
66-
67-
module org.example {
68-
69-
requires io.avaje.inject;
70-
// you must define the fully qualified class name of the generated classes. if you use an import statement, compilation will fail
71-
provides InjectExtension with org.example.ExampleModule;
72-
}
73-
```
74-
7562
### Generated Wiring Class
7663
The inject annotation processor determines the dependency wiring order and generates an `AvajeModule` class that calls all the generated DI classes.
7764

@@ -80,18 +67,6 @@ The inject annotation processor determines the dependency wiring order and gener
8067
@InjectModule
8168
public final class ExampleModule implements AvajeModule {
8269

83-
private Builder builder;
84-
85-
@Override
86-
public Class<?>[] classes() {
87-
return new Class<?>[] {
88-
org.example.DependencyClass.class,
89-
org.example.DependencyClass2.class,
90-
org.example.Example.class,
91-
org.example.ExampleFactory.class,
92-
};
93-
}
94-
9570
/**
9671
* Creates all the beans in order based on constructor dependencies. The beans are registered
9772
* into the builder along with callbacks for field/method injection, and lifecycle
@@ -102,34 +77,34 @@ public final class ExampleModule implements AvajeModule {
10277
this.builder = builder;
10378
// create beans in order based on constructor dependencies
10479
// i.e. "provides" followed by "dependsOn"
105-
build_example_ExampleFactory();
106-
build_example_DependencyClass();
107-
build_example_DependencyClass2();
108-
build_example_Example();
80+
build_example_ExampleFactory(builder);
81+
build_example_DependencyClass(builder);
82+
build_example_DependencyClass2(builder);
83+
build_example_Example(builder);
10984
}
11085

11186
@DependencyMeta(type = "org.example.ExampleFactory")
112-
private void build_example_ExampleFactory() {
87+
private void build_example_ExampleFactory(Builder builder) {
11388
ExampleFactory$DI.build(builder);
11489
}
11590

11691
@DependencyMeta(type = "org.example.DependencyClass")
117-
private void build_example_DependencyClass() {
92+
private void build_example_DependencyClass(Builder builder) {
11893
DependencyClass$DI.build(builder);
11994
}
12095

12196
@DependencyMeta(
12297
type = "org.example.DependencyClass2",
12398
method = "org.example.ExampleFactory$DI.build_bean", // factory method
12499
dependsOn = {"org.example.ExampleFactory"}) //factory beans naturally depend on the factory
125-
private void build_example_DependencyClass2() {
100+
private void build_example_DependencyClass2(Builder builder) {
126101
ExampleFactory$DI.build_bean(builder);
127102
}
128103

129104
@DependencyMeta(
130105
type = "org.example.Example",
131106
dependsOn = {"org.example.DependencyClass", "org.example.DependencyClass2"})
132-
private void build_example_Example() {
107+
private void build_example_Example(Builder builder) {
133108
Example$DI.build(builder);
134109
}
135110
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void readBeans(RoundEnvironment roundEnv) {
4343
for (Data data : scopeAnnotations.values()) {
4444
for (Element customBean : roundEnv.getElementsAnnotatedWith(data.type)) {
4545
if (customBean instanceof TypeElement) {
46+
ProcessingContext.processingOver(false);
4647
data.scopeInfo.read((TypeElement) customBean, false, false);
4748
}
4849
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static io.avaje.inject.generator.ProcessingContext.delayedElements;
77
import static io.avaje.inject.generator.ProcessingContext.loadMetaInfCustom;
88
import static io.avaje.inject.generator.ProcessingContext.loadMetaInfServices;
9+
import static io.avaje.inject.generator.ProcessingContext.processingOver;
910
import static java.util.stream.Collectors.joining;
1011
import static java.util.stream.Collectors.toList;
1112

@@ -71,6 +72,8 @@ public final class InjectProcessor extends AbstractProcessor {
7172
private final Set<String> pluginFileProvided = new HashSet<>();
7273
private final Set<String> moduleFileProvided = new HashSet<>();
7374
private final List<ModuleData> moduleData = new ArrayList<>();
75+
private int rounds;
76+
7477

7578
@Override
7679
public SourceVersion getSupportedSourceVersion() {
@@ -143,16 +146,14 @@ private List<String> lines(String relativeName) {
143146

144147
@Override
145148
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
146-
if (roundEnv.errorRaised()) {
149+
if (processingOver() || roundEnv.errorRaised()) {
147150
return false;
148151
}
152+
processingOver(rounds++ > 0);
149153

150154
APContext.setProjectModuleElement(annotations, roundEnv);
151155
readModule(roundEnv);
152156

153-
final var processingOver = roundEnv.processingOver();
154-
ProcessingContext.processingOver(processingOver);
155-
156157
readBeans(delayedElements());
157158
addImportedAspects(importedAspects(roundEnv));
158159
maybeElements(roundEnv, QualifierPrism.PRISM_TYPE).stream()
@@ -192,11 +193,12 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
192193
maybeElements(roundEnv, ServiceProviderPrism.PRISM_TYPE).ifPresent(this::registerSPI);
193194
maybeElements(roundEnv, PluginProvidesPrism.PRISM_TYPE).ifPresent(this::registerSPI);
194195
allScopes.readBeans(roundEnv);
195-
defaultScope.write(processingOver);
196-
allScopes.write(processingOver);
196+
final var over = processingOver();
197+
defaultScope.write(over);
198+
allScopes.write(over);
197199

198-
if (processingOver) {
199-
var order =
200+
if (processingOver()) {
201+
final var order =
200202
new FactoryOrder(ProcessingContext.modules(), defaultScope.pluginProvided())
201203
.orderModules();
202204

@@ -234,8 +236,13 @@ private void validateQualifier(ExecutableElement method) {
234236
}
235237

236238
// Optional because these annotations are not guaranteed to exist
237-
private static Optional<? extends Set<? extends Element>> maybeElements(RoundEnvironment round, String name) {
238-
return Optional.ofNullable(typeElement(name)).map(round::getElementsAnnotatedWith);
239+
private Optional<? extends Set<? extends Element>> maybeElements(RoundEnvironment round, String name) {
240+
final var op = Optional.ofNullable(typeElement(name))
241+
.map(round::getElementsAnnotatedWith);
242+
243+
// reset processingOver flag if anything needs processing in this round
244+
processingOver(processingOver() && op.filter(n -> !n.isEmpty()).isEmpty());
245+
return op;
239246
}
240247

241248
private Set<TypeElement> importedElements(RoundEnvironment roundEnv) {
@@ -264,7 +271,7 @@ private boolean notAlreadyProvided(TypeElement e) {
264271
return !moduleFileProvided.contains(type) && !pluginFileProvided.contains(type);
265272
}
266273

267-
private static Map<String, AspectImportPrism> importedAspects(RoundEnvironment roundEnv) {
274+
private Map<String, AspectImportPrism> importedAspects(RoundEnvironment roundEnv) {
268275
return maybeElements(roundEnv, AspectImportPrism.PRISM_TYPE).stream()
269276
.flatMap(Set::stream)
270277
.map(AspectImportPrism::getAllInstancesOn)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ static Set<TypeElement> delayedElements() {
232232
}
233233

234234
static boolean delayUntilNextRound(TypeElement element) {
235-
if (!processingOver) {
236-
CTX.get().delayQueue.add(element);
237-
}
238-
return !processingOver;
235+
return CTX.get().delayQueue.add(element);
239236
}
240237

241238
static void clear() {
@@ -263,6 +260,10 @@ static void processingOver(boolean over) {
263260
processingOver = over;
264261
}
265262

263+
static boolean processingOver() {
264+
return processingOver;
265+
}
266+
266267
static void writeSPIServicesFile() {
267268
addEventSPI();
268269
readExistingMetaInfServices();

0 commit comments

Comments
 (0)