Skip to content

Commit fc5b928

Browse files
committed
Merge branch 'empty-list-fix' of https://github.com/doppelrittberger/avaje-inject into doppelrittberger-empty-list-fix
2 parents 7ee4b7b + 5822b91 commit fc5b928

File tree

9 files changed

+90
-24
lines changed

9 files changed

+90
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ BeanReader read() {
8484
return this;
8585
}
8686

87-
List<String> getDependsOn() {
88-
List<String> list = new ArrayList<>();
87+
List<Dependency> getDependsOn() {
88+
List<Dependency> list = new ArrayList<>();
8989
if (constructor != null) {
9090
for (MethodReader.MethodParam param : constructor.getParams()) {
9191
list.add(param.getDependsOn());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.avaje.inject.generator;
2+
3+
public class Dependency {
4+
private final String name;
5+
private final boolean softDependency;
6+
7+
public Dependency(String name) {
8+
this(name, false);
9+
}
10+
11+
public Dependency(String name, boolean softDependency) {
12+
this.name = name;
13+
this.softDependency = softDependency;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public boolean isSoftDependency() {
21+
return softDependency;
22+
}
23+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.Arrays;
77
import java.util.List;
88
import java.util.Set;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.Stream;
911

1012
/**
1113
* Holds the data as per <code>@DependencyMeta</code>
@@ -28,15 +30,15 @@ class MetaData {
2830
/**
2931
* The list of dependencies with optional and named.
3032
*/
31-
private List<String> dependsOn;
33+
private List<Dependency> dependsOn;
3234

3335
MetaData(DependencyMeta meta) {
3436
this.type = meta.type();
3537
this.name = trimName(meta.name());
3638
this.shortType = Util.shortName(type);
3739
this.method = meta.method();
3840
this.provides = asList(meta.provides());
39-
this.dependsOn = asList(meta.dependsOn());
41+
this.dependsOn = Stream.of(meta.dependsOn()).map(Dependency::new).collect(Collectors.toList());
4042
}
4143

4244
MetaData(String type, String name) {
@@ -112,7 +114,7 @@ List<String> getProvides() {
112114
return provides;
113115
}
114116

115-
List<String> getDependsOn() {
117+
List<Dependency> getDependsOn() {
116118
return dependsOn;
117119
}
118120

@@ -157,7 +159,7 @@ String buildMethod(MetaDataOrdering ordering) {
157159
appendProvides(sb, "provides", provides);
158160
}
159161
if (!dependsOn.isEmpty()) {
160-
appendProvides(sb, "dependsOn", dependsOn);
162+
appendProvides(sb, "dependsOn", dependsOn.stream().map(Dependency::getName).collect(Collectors.toList()));
161163
}
162164
sb.append(")").append(NEWLINE);
163165
sb.append(" protected void build_").append(getBuildName()).append("() {").append(NEWLINE);
@@ -167,7 +169,7 @@ String buildMethod(MetaDataOrdering ordering) {
167169
sb.append(" ").append(shortType).append(Constants.DI).append(".build(builder");
168170
}
169171

170-
for (String depend : dependsOn) {
172+
dependsOn.stream().map(Dependency::getName).forEach(depend-> {
171173
if (GenericType.isGeneric(depend) && !Util.isProvider(depend)) {
172174
// provide implementation of generic interface as a parameter to the build method
173175
final MetaData providerMeta = findProviderOf(depend, ordering);
@@ -176,7 +178,7 @@ String buildMethod(MetaDataOrdering ordering) {
176178
sb.append(", ").append(providerMeta.shortType).append("$DI.provider").append(type.shortName()).append("(builder)");
177179
}
178180
}
179-
}
181+
});
180182
sb.append(");").append(NEWLINE);
181183
sb.append(" }").append(NEWLINE);
182184
return sb.toString();
@@ -208,7 +210,7 @@ void setProvides(List<String> provides) {
208210
}
209211

210212
void setDependsOn(List<String> dependsOn) {
211-
this.dependsOn = dependsOn;
213+
this.dependsOn = dependsOn.stream().map(Dependency::new).collect(Collectors.toList());
212214
}
213215

214216
void setMethod(String method) {

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ int processQueue() {
6767
private void detectCircularDependency(List<MetaData> remainder) {
6868
final List<DependencyLink> dependencyLinks = new ArrayList<>();
6969
for (MetaData metaData : remainder) {
70-
final List<String> dependsOn = metaData.getDependsOn();
70+
final List<Dependency> dependsOn = metaData.getDependsOn();
7171
if (dependsOn != null) {
72-
for (String dependency : dependsOn) {
72+
for (Dependency dependency : dependsOn) {
7373
final MetaData provider = findCircularDependency(remainder, dependency);
7474
if (provider != null) {
75-
dependencyLinks.add(new DependencyLink(metaData, provider, dependency));
75+
dependencyLinks.add(new DependencyLink(metaData, provider, dependency.getName()));
7676
}
7777
}
7878
}
@@ -83,13 +83,13 @@ private void detectCircularDependency(List<MetaData> remainder) {
8383
}
8484
}
8585

86-
private MetaData findCircularDependency(List<MetaData> remainder, String dependency) {
86+
private MetaData findCircularDependency(List<MetaData> remainder, Dependency dependency) {
8787
for (MetaData metaData : remainder) {
88-
if (metaData.getType().equals(dependency)) {
88+
if (metaData.getType().equals(dependency.getName())) {
8989
return metaData;
9090
}
9191
final List<String> provides = metaData.getProvides();
92-
if (provides != null && provides.contains(dependency)) {
92+
if (provides != null && provides.contains(dependency.getName())) {
9393
return metaData;
9494
}
9595
}
@@ -120,11 +120,12 @@ void missingDependencies() {
120120
}
121121

122122
private void checkMissingDependencies(MetaData metaData) {
123-
for (String dependency : metaData.getDependsOn()) {
124-
if (providers.get(dependency) == null && !scopeInfo.providedByOtherModule(dependency)) {
123+
for (Dependency dependency : metaData.getDependsOn()) {
124+
if (providers.get(dependency.getName()) == null
125+
&& !scopeInfo.providedByOtherModule(dependency.getName())) {
125126
TypeElement element = context.elementMaybe(metaData.getType());
126127
context.logError(element, "No dependency provided for " + dependency + " on " + metaData.getType());
127-
missingDependencyTypes.add(dependency);
128+
missingDependencyTypes.add(dependency.getName());
128129
}
129130
}
130131
}
@@ -170,12 +171,12 @@ private int processQueueRound() {
170171
}
171172

172173
private boolean allDependenciesWired(MetaData queuedMeta) {
173-
for (String dependency : queuedMeta.getDependsOn()) {
174-
if (!Util.isProvider(dependency)) {
174+
for (Dependency dependency : queuedMeta.getDependsOn()) {
175+
if (!Util.isProvider(dependency.getName())) {
175176
// check non-provider dependency is satisfied
176-
ProviderList providerList = providers.get(dependency);
177+
ProviderList providerList = providers.get(dependency.getName());
177178
if (providerList == null) {
178-
if (!scopeInfo.providedByOtherModule(dependency)) {
179+
if (!scopeInfo.providedByOtherModule(dependency.getName()) && !dependency.isSoftDependency()) {
179180
return false;
180181
}
181182
} else {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ boolean isGenericParam() {
342342
return isGenericType() && !isProvider();
343343
}
344344

345-
String getDependsOn() {
346-
return paramType;
345+
Dependency getDependsOn() {
346+
return new Dependency(paramType, utilType.isCollection());
347347
}
348348

349349
void addImports(Set<String> importTypes) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ boolean allowsNamedQualifier() {
3939
return type == Type.OPTIONAL || type == Type.OTHER;
4040
}
4141

42+
boolean isCollection() {
43+
return type == Type.LIST || type == Type.SET;
44+
}
45+
4246
String rawType() {
4347
switch (type) {
4448
case SET:

inject-test/src/test/java/org/example/coffee/InjectListTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.avaje.inject.BeanScope;
44
import org.example.coffee.list.CombinedSetSomei;
55
import org.example.coffee.list.CombinedSomei;
6+
import org.example.coffee.list.SomeInterface;
7+
import org.example.coffee.list.SomeInterfaceConsumer;
68
import org.junit.jupiter.api.Test;
79

810
import java.util.List;
@@ -28,4 +30,13 @@ void test_set() {
2830
assertThat(somes).containsOnly("a", "b", "a2");
2931
}
3032
}
33+
34+
@Test
35+
void testEmptyList() {
36+
try (BeanScope context = BeanScope.newBuilder().build()) {
37+
SomeInterfaceConsumer consumer = context.get(SomeInterfaceConsumer.class);
38+
assertThat(consumer.getList()).isEmpty();
39+
System.out.println("--------------------> works");
40+
}
41+
}
3142
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.example.coffee.list;
2+
3+
public interface SomeInterface {
4+
void doSomething();
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example.coffee.list;
2+
3+
import jakarta.inject.Inject;
4+
import jakarta.inject.Singleton;
5+
6+
import java.util.List;
7+
8+
@Singleton
9+
public class SomeInterfaceConsumer {
10+
private List<SomeInterface> list;
11+
12+
@Inject
13+
public SomeInterfaceConsumer(List<SomeInterface> list) {
14+
this.list = list;
15+
}
16+
17+
public List<SomeInterface> getList() {
18+
return list;
19+
}
20+
}

0 commit comments

Comments
 (0)