Skip to content
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

cyclic interface binding #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 24 additions & 3 deletions toothpick-runtime/src/main/java/toothpick/ScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.inject.Provider;
import toothpick.config.Binding;
import toothpick.config.Module;
Expand Down Expand Up @@ -226,9 +228,29 @@ private void installModules(boolean isTestModule, Module... modules) {
}
}

private InternalProvider installInternalProviderCyclic(Set<Binding> allBindings, Binding bindingToFind, boolean isTestModule) {
InternalProvider provider = getInternalProvider(bindingToFind.getImplementationClass(), bindingToFind.getName(), true);
if (provider == null) {
for (Binding binding : allBindings) {
if (binding.getKey() == bindingToFind.getImplementationClass()) {
InternalProvider foundProvider = installInternalProviderCyclic(allBindings, binding, isTestModule);
installScopedProvider(bindingToFind.getKey(), bindingToFind.getName(), foundProvider, isTestModule);
return foundProvider;
}
}
InternalProvider createdProvider = toProvider(bindingToFind);
installScopedProvider(bindingToFind.getKey(), bindingToFind.getName(), createdProvider, isTestModule);
return createdProvider;
} else {
installScopedProvider(bindingToFind.getKey(), bindingToFind.getName(), provider, isTestModule);
return provider;
}
}

@SuppressWarnings("unchecked")
private void installModule(boolean isTestModule, Module module) {
for (Binding binding : module.getBindingSet()) {
Set<Binding> bindings = module.getBindingSet();
for (Binding binding : bindings) {
if (binding == null) {
throw new IllegalStateException("A module can't have a null binding : " + module);
}
Expand All @@ -237,8 +259,7 @@ private void installModule(boolean isTestModule, Module module) {
String bindingName = binding.getName();
try {
if (isTestModule || getScopedProvider(clazz, bindingName) == null) {
InternalProvider provider = toProvider(binding);
installScopedProvider(clazz, bindingName, provider, isTestModule);
installInternalProviderCyclic(bindings, binding, isTestModule);
}
} catch (Exception e) {
throw new IllegalBindingException(
Expand Down