Skip to content

added fix and test for #179 #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ BeanReader read() {
return this;
}

List<String> getDependsOn() {
List<String> list = new ArrayList<>();
List<Dependency> getDependsOn() {
List<Dependency> list = new ArrayList<>();
if (constructor != null) {
for (MethodReader.MethodParam param : constructor.getParams()) {
list.add(param.getDependsOn());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.avaje.inject.generator;

public class Dependency {
private final String name;
private final boolean softDependency;

public Dependency(String name) {
this(name, false);
}

public Dependency(String name, boolean softDependency) {
this.name = name;
this.softDependency = softDependency;
}

public String getName() {
return name;
}

public boolean isSoftDependency() {
return softDependency;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Holds the data as per <code>@DependencyMeta</code>
Expand All @@ -28,15 +30,15 @@ class MetaData {
/**
* The list of dependencies with optional and named.
*/
private List<String> dependsOn;
private List<Dependency> dependsOn;

MetaData(DependencyMeta meta) {
this.type = meta.type();
this.name = trimName(meta.name());
this.shortType = Util.shortName(type);
this.method = meta.method();
this.provides = asList(meta.provides());
this.dependsOn = asList(meta.dependsOn());
this.dependsOn = Stream.of(meta.dependsOn()).map(Dependency::new).collect(Collectors.toList());
}

MetaData(String type, String name) {
Expand Down Expand Up @@ -112,7 +114,7 @@ List<String> getProvides() {
return provides;
}

List<String> getDependsOn() {
List<Dependency> getDependsOn() {
return dependsOn;
}

Expand Down Expand Up @@ -157,7 +159,7 @@ String buildMethod(MetaDataOrdering ordering) {
appendProvides(sb, "provides", provides);
}
if (!dependsOn.isEmpty()) {
appendProvides(sb, "dependsOn", dependsOn);
appendProvides(sb, "dependsOn", dependsOn.stream().map(Dependency::getName).collect(Collectors.toList()));
}
sb.append(")").append(NEWLINE);
sb.append(" protected void build_").append(getBuildName()).append("() {").append(NEWLINE);
Expand All @@ -167,7 +169,7 @@ String buildMethod(MetaDataOrdering ordering) {
sb.append(" ").append(shortType).append(Constants.DI).append(".build(builder");
}

for (String depend : dependsOn) {
dependsOn.stream().map(Dependency::getName).forEach(depend-> {
if (GenericType.isGeneric(depend) && !Util.isProvider(depend)) {
// provide implementation of generic interface as a parameter to the build method
final MetaData providerMeta = findProviderOf(depend, ordering);
Expand All @@ -176,7 +178,7 @@ String buildMethod(MetaDataOrdering ordering) {
sb.append(", ").append(providerMeta.shortType).append("$DI.provider").append(type.shortName()).append("(builder)");
}
}
}
});
sb.append(");").append(NEWLINE);
sb.append(" }").append(NEWLINE);
return sb.toString();
Expand Down Expand Up @@ -208,7 +210,7 @@ void setProvides(List<String> provides) {
}

void setDependsOn(List<String> dependsOn) {
this.dependsOn = dependsOn;
this.dependsOn = dependsOn.stream().map(Dependency::new).collect(Collectors.toList());
}

void setMethod(String method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ int processQueue() {
private void detectCircularDependency(List<MetaData> remainder) {
final List<DependencyLink> dependencyLinks = new ArrayList<>();
for (MetaData metaData : remainder) {
final List<String> dependsOn = metaData.getDependsOn();
final List<Dependency> dependsOn = metaData.getDependsOn();
if (dependsOn != null) {
for (String dependency : dependsOn) {
for (Dependency dependency : dependsOn) {
final MetaData provider = findCircularDependency(remainder, dependency);
if (provider != null) {
dependencyLinks.add(new DependencyLink(metaData, provider, dependency));
dependencyLinks.add(new DependencyLink(metaData, provider, dependency.getName()));
}
}
}
Expand All @@ -83,13 +83,13 @@ private void detectCircularDependency(List<MetaData> remainder) {
}
}

private MetaData findCircularDependency(List<MetaData> remainder, String dependency) {
private MetaData findCircularDependency(List<MetaData> remainder, Dependency dependency) {
for (MetaData metaData : remainder) {
if (metaData.getType().equals(dependency)) {
if (metaData.getType().equals(dependency.getName())) {
return metaData;
}
final List<String> provides = metaData.getProvides();
if (provides != null && provides.contains(dependency)) {
if (provides != null && provides.contains(dependency.getName())) {
return metaData;
}
}
Expand Down Expand Up @@ -120,11 +120,12 @@ void missingDependencies() {
}

private void checkMissingDependencies(MetaData metaData) {
for (String dependency : metaData.getDependsOn()) {
if (providers.get(dependency) == null && !scopeInfo.providedByOtherModule(dependency)) {
for (Dependency dependency : metaData.getDependsOn()) {
if (providers.get(dependency.getName()) == null
&& !scopeInfo.providedByOtherModule(dependency.getName())) {
TypeElement element = context.elementMaybe(metaData.getType());
context.logError(element, "No dependency provided for " + dependency + " on " + metaData.getType());
missingDependencyTypes.add(dependency);
missingDependencyTypes.add(dependency.getName());
}
}
}
Expand Down Expand Up @@ -170,12 +171,12 @@ private int processQueueRound() {
}

private boolean allDependenciesWired(MetaData queuedMeta) {
for (String dependency : queuedMeta.getDependsOn()) {
if (!Util.isProvider(dependency)) {
for (Dependency dependency : queuedMeta.getDependsOn()) {
if (!Util.isProvider(dependency.getName())) {
// check non-provider dependency is satisfied
ProviderList providerList = providers.get(dependency);
ProviderList providerList = providers.get(dependency.getName());
if (providerList == null) {
if (!scopeInfo.providedByOtherModule(dependency)) {
if (!scopeInfo.providedByOtherModule(dependency.getName()) && !dependency.isSoftDependency()) {
return false;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ boolean isGenericParam() {
return isGenericType() && !isProvider();
}

String getDependsOn() {
return paramType;
Dependency getDependsOn() {
return new Dependency(paramType, utilType.isCollection());
}

void addImports(Set<String> importTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ boolean allowsNamedQualifier() {
return type == Type.OPTIONAL || type == Type.OTHER;
}

boolean isCollection() {
return type == Type.LIST || type == Type.SET;
}

String rawType() {
switch (type) {
case SET:
Expand Down
11 changes: 11 additions & 0 deletions inject-test/src/test/java/org/example/coffee/InjectListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.avaje.inject.BeanScope;
import org.example.coffee.list.CombinedSetSomei;
import org.example.coffee.list.CombinedSomei;
import org.example.coffee.list.SomeInterface;
import org.example.coffee.list.SomeInterfaceConsumer;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand All @@ -28,4 +30,13 @@ void test_set() {
assertThat(somes).containsOnly("a", "b", "a2");
}
}

@Test
void testEmptyList() {
try (BeanScope context = BeanScope.newBuilder().build()) {
SomeInterfaceConsumer consumer = context.get(SomeInterfaceConsumer.class);
assertThat(consumer.getList()).isEmpty();
System.out.println("--------------------> works");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.example.coffee.list;

public interface SomeInterface {
void doSomething();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.example.coffee.list;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import java.util.List;

@Singleton
public class SomeInterfaceConsumer {
private List<SomeInterface> list;

@Inject
public SomeInterfaceConsumer(List<SomeInterface> list) {
this.list = list;
}

public List<SomeInterface> getList() {
return list;
}
}