Skip to content

Commit 0c4e307

Browse files
committed
#171 - Tidy ScopeInfo and AllScopes and improve tests
1 parent 192bf9c commit 0c4e307

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ScopeInfo addScopeAnnotation(TypeElement type) {
3636
}
3737

3838
boolean providedByDefaultModule(String dependency) {
39-
return defaultScope.providesDependency(dependency);
39+
return defaultScope.providesDependencyLocally(dependency);
4040
}
4141

4242
void readBeans(RoundEnvironment roundEnv) {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,18 @@ boolean providedByOtherModule(String dependency) {
410410
return providesDependencyRecursive(dependency);
411411
}
412412

413-
boolean providesDependencyRecursive(String dependency) {
414-
if (providesDependency(dependency)) {
413+
/**
414+
* Recursively search including 'parent' scopes.
415+
*/
416+
private boolean providesDependencyRecursive(String dependency) {
417+
if (providesDependencyLocally(dependency)) {
415418
return true;
416419
}
417420
// look for required scopes ...
418421
for (String require : requires) {
419422
final ScopeInfo requiredScope = scopes.get(require);
420423
if (requiredScope != null) {
424+
// recursively search parent scope
421425
if (requiredScope.providesDependencyRecursive(dependency)) {
422426
// context.logWarn("dependency " + dependency + " provided by other scope " + requiredScope.name);
423427
return true;
@@ -428,9 +432,9 @@ boolean providesDependencyRecursive(String dependency) {
428432
}
429433

430434
/**
431-
* Return true if this module provides the dependency.
435+
* Return true if this module provides the dependency (non-recursive, local only).
432436
*/
433-
boolean providesDependency(String dependency) {
437+
boolean providesDependencyLocally(String dependency) {
434438
if (requires.contains(dependency)) {
435439
return true;
436440
}

inject-test/src/test/java/org/example/custom4/LinuxOne.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
@LinuxScope
44
public class LinuxOne {
55

6-
private final Machine machine;
7-
private final Build build;
6+
final Machine machine;
7+
final Build build;
88

99
LinuxOne(Machine machine, Build build) {
1010
this.machine = machine;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.example.custom4;
2+
3+
import io.avaje.inject.BeanScope;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class LinuxScopeTest {
9+
10+
@Test
11+
void test_via_nested_scopes() {
12+
Build buildExternal = new Build();
13+
BuildModule buildModule = new BuildModule(buildExternal);
14+
15+
// our top scope
16+
try (BeanScope buildScope = BeanScope.newBuilder()
17+
.withModules(buildModule)
18+
.build()) {
19+
20+
Build build = buildScope.get(Build.class);
21+
assertThat(build).isSameAs(buildExternal);
22+
23+
Machine machineExternal = new Machine();
24+
MachineModule machineModule = new MachineModule(machineExternal);
25+
26+
// our middle scope (depends on top scope)
27+
try (BeanScope machineScope = BeanScope.newBuilder()
28+
.withParent(buildScope)
29+
.withModules(machineModule)
30+
.build()) {
31+
32+
MachineOne machineOne = machineScope.get(MachineOne.class);
33+
assertThat(machineOne.build).isSameAs(buildExternal);
34+
assertThat(machineOne.machine).isSameAs(machineExternal);
35+
36+
// bottom scope depends on middle scope and transitively depends on top scope
37+
// this is our case for Issue 171 where LinuxOne depends on Build
38+
// which is transitively supplied via MachineScope
39+
try (BeanScope linuxScope = BeanScope.newBuilder()
40+
.withParent(machineScope)
41+
.withModules(new LinuxModule())
42+
.build()) {
43+
44+
MachineOne machineOne2 = linuxScope.get(MachineOne.class);
45+
assertThat(machineOne2).isSameAs(machineOne);
46+
assertThat(machineOne2.build).isSameAs(buildExternal);
47+
assertThat(machineOne2.machine).isSameAs(machineExternal);
48+
49+
LinuxOne linuxOne = linuxScope.get(LinuxOne.class);
50+
assertThat(linuxOne.build).isSameAs(buildExternal);
51+
assertThat(linuxOne.machine).isSameAs(machineExternal);
52+
}
53+
}
54+
}
55+
}
56+
57+
58+
@Test
59+
void test_via_flattened_module_structure() {
60+
// external dependencies
61+
Build buildExternal = new Build();
62+
Machine machineExternal = new Machine();
63+
64+
// our 'flattened' bean scope
65+
try (BeanScope flatScope = BeanScope.newBuilder()
66+
// all our scope modules
67+
.withModules(new BuildModule(buildExternal), new MachineModule(machineExternal), new LinuxModule())
68+
.build()) {
69+
70+
Build build = flatScope.get(Build.class);
71+
assertThat(build).isSameAs(buildExternal);
72+
73+
MachineOne machineOne = flatScope.get(MachineOne.class);
74+
assertThat(machineOne.build).isSameAs(buildExternal);
75+
assertThat(machineOne.machine).isSameAs(machineExternal);
76+
77+
LinuxOne linuxOne = flatScope.get(LinuxOne.class);
78+
assertThat(linuxOne.build).isSameAs(buildExternal);
79+
assertThat(linuxOne.machine).isSameAs(machineExternal);
80+
}
81+
}
82+
}

inject-test/src/test/java/org/example/custom4/MachineOne.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
@MachineScope
44
public class MachineOne {
55

6-
private final Build build;
7-
private final Machine machine;
6+
final Build build;
7+
final Machine machine;
88

99
MachineOne(Build build, Machine machine) {
1010
this.build = build;

0 commit comments

Comments
 (0)