From b173982b88f0bb420bc59b7fda3a2f53a5cfc476 Mon Sep 17 00:00:00 2001 From: yanis_biziuk Date: Fri, 18 Jun 2021 12:33:28 +0300 Subject: [PATCH] added cyclic interface binding --- .../src/main/java/toothpick/ScopeImpl.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java b/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java index 15fa20b2..637702c3 100644 --- a/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java +++ b/toothpick-runtime/src/main/java/toothpick/ScopeImpl.java @@ -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; @@ -226,9 +228,29 @@ private void installModules(boolean isTestModule, Module... modules) { } } + private InternalProvider installInternalProviderCyclic(Set 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 bindings = module.getBindingSet(); + for (Binding binding : bindings) { if (binding == null) { throw new IllegalStateException("A module can't have a null binding : " + module); } @@ -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(