moduleAdapter.getBindings(addTo, loadedModule.getValue()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(
if (seedModulesOrClasses[i] instanceof Class<?>) { ModuleAdapter<?> adapter = loader.getModuleAdapter((Class<?>) seedModulesOrClasses[i]); seedAdapters.put(adapter, adapter.newModule()); } else { ModuleAdapter<?> adapter = loader.getModuleAdapter(seedModulesOrClasses[i].getClass()); result.put(dependency, dependency.newModule());
private static ObjectGraph makeGraph(ObjectGraph base, Plugin plugin, Object... modules) { Map<String, Class<?>> entryPoints = new LinkedHashMap<String, Class<?>>(); Map<Class<?>, StaticInjection> staticInjections = new LinkedHashMap<Class<?>, StaticInjection>(); // Extract bindings in the 'base' and 'overrides' set. Within each set no // duplicates are permitted. Map<String, Binding<?>> baseBindings = new UniqueMap<String, Binding<?>>(); Map<String, Binding<?>> overrideBindings = new UniqueMap<String, Binding<?>>(); for (ModuleAdapter<?> moduleAdapter : getAllModuleAdapters(plugin, modules).values()) { for (String key : moduleAdapter.entryPoints) { entryPoints.put(key, moduleAdapter.getModule().getClass()); } for (Class<?> c : moduleAdapter.staticInjections) { staticInjections.put(c, null); } Map<String, Binding<?>> addTo = moduleAdapter.overrides ? overrideBindings : baseBindings; moduleAdapter.getBindings(addTo); } // Create a linker and install all of the user's bindings Linker linker = new Linker((base != null) ? base.linker : null, plugin, new ThrowingErrorHandler()); linker.installBindings(baseBindings); linker.installBindings(overrideBindings); return new ObjectGraph(base, linker, plugin, staticInjections, entryPoints); }
/** * Returns a full set of module adapters, including module adapters for included * modules. */ public static Map<Class<?>, ModuleAdapter<?>> getAllModuleAdapters(Plugin plugin, Object[] seedModules) { // Create a module adapter for each seed module. ModuleAdapter<?>[] seedAdapters = new ModuleAdapter<?>[seedModules.length]; int s = 0; for (Object module : seedModules) { if (module instanceof Class) { seedAdapters[s++] = plugin.getModuleAdapter((Class<?>) module, null); // Plugin constructs. } else { seedAdapters[s++] = plugin.getModuleAdapter(module.getClass(), module); } } Map<Class<?>, ModuleAdapter<?>> adaptersByModuleType = new LinkedHashMap<Class<?>, ModuleAdapter<?>>(); // Add the adapters that we have module instances for. This way we won't // construct module objects when we have a user-supplied instance. for (ModuleAdapter<?> adapter : seedAdapters) { adaptersByModuleType.put(adapter.getModule().getClass(), adapter); } // Next add adapters for the modules that we need to construct. This creates // instances of modules as necessary. for (ModuleAdapter<?> adapter : seedAdapters) { collectIncludedModulesRecursively(plugin, adapter, adaptersByModuleType); } return adaptersByModuleType; }
/** * Obtains a module adapter for {@code module} from the first responding resolver. */ @Override public <T> ModuleAdapter<T> getModuleAdapter(Class<? extends T> moduleClass, T module) { for (int i = 0; i < plugins.length; i++) { try { ModuleAdapter<T> result = plugins[i].getModuleAdapter(moduleClass, module); result.module = (module != null) ? module : result.newModule(); return result; } catch (RuntimeException e) { if (i == plugins.length - 1) throw e; logNotFound("Module adapter", moduleClass.getName(), e); } } throw new AssertionError(); }