linker.installBindings(overrideBindings); return new DaggerObjectGraph( base, linker, plugin, staticInjections, injectableTypes, baseBindings.setBindings);
@Override public <T> T get(Class<T> type) { String key = Keys.get(type); String injectableTypeKey = type.isInterface() ? key : Keys.getMembersKey(type); ClassLoader classLoader = type.getClassLoader(); @SuppressWarnings("unchecked") // The linker matches keys to bindings by their type. Binding<T> binding = (Binding<T>) getInjectableTypeBinding(classLoader, injectableTypeKey, key); return binding.get(); }
@Override public void validate() { Map<String, Binding<?>> allBindings = linkEverything(); new ProblemDetector().detectProblems(allBindings.values()); }
/** * Links all bindings, injectable types and static injections. */ private Map<String, Binding<?>> linkEverything() { Map<String, Binding<?>> bindings = linker.fullyLinkedBindings(); if (bindings != null) { return bindings; } synchronized (linker) { if ((bindings = linker.fullyLinkedBindings()) != null) { return bindings; } linkStaticInjections(); linkInjectableTypes(); return linker.linkAll(); // Linker.linkAll() implicitly does Linker.linkRequested(). } }
/** * Returns a new dependency graph using the {@literal @}{@link * Module}-annotated modules. * * <p>This <strong>does not</strong> inject any members. Most applications * should call {@link #injectStatics} to inject static members and {@link * #inject} or get {@link #get(Class)} to inject instance members when this * method has returned. * * <p>This <strong>does not</strong> validate the graph. Rely on build time * tools for graph validation, or call {@link #validate} to find problems in * the graph at runtime. */ public static ObjectGraph create(Object... modules) { return DaggerObjectGraph.makeGraph(null, new FailoverLoader(), modules); }
@Override public void injectStatics() { // We call linkStaticInjections() twice on purpose. The first time through // we request all of the bindings we need. The linker returns null for // bindings it doesn't have. Then we ask the linker to link all of those // requested bindings. Finally we call linkStaticInjections() again: this // time the linker won't return null because everything has been linked. synchronized (linker) { linkStaticInjections(); linker.linkRequested(); linkStaticInjections(); } for (Map.Entry<Class<?>, StaticInjection> entry : staticInjections.entrySet()) { entry.getValue().inject(); } }
static ObjectGraph createWith(Loader loader, Object... modules) { return DaggerObjectGraph.makeGraph(null, loader, modules); }
DaggerObjectGraph(DaggerObjectGraph base, Linker linker, Loader plugin, Map<Class<?>, StaticInjection> staticInjections, Map<String, Class<?>> injectableTypes, List<SetBinding<?>> setBindings) { this.base = base; this.linker = checkNotNull(linker, "linker"); this.plugin = checkNotNull(plugin, "plugin"); this.staticInjections = checkNotNull(staticInjections, "staticInjections"); this.injectableTypes = checkNotNull(injectableTypes, "injectableTypes"); this.setBindings = checkNotNull(setBindings, "setBindings"); }
@Override public ObjectGraph plus(Object... modules) { linkEverything(); return makeGraph(this, plugin, modules); }
@Override public <T> T inject(T instance) { String membersKey = Keys.getMembersKey(instance.getClass()); ClassLoader classLoader = instance.getClass().getClassLoader(); @SuppressWarnings("unchecked") // The linker matches keys to bindings by their type. Binding<T> binding = (Binding<T>) getInjectableTypeBinding(classLoader, membersKey, membersKey); binding.injectMembers(instance); return instance; }