DefaultImpl(LifecycleManager mgr) { container = InjectionContainer.create(CachePolicy.MEMOIZE, mgr); }
@Override public Object instantiate(DAGNode<Component, Dependency> node) throws InjectionException { return container.makeInstantiator(node).instantiate(); } }
/** * Create a new instantiator. * @param dft The default cache policy. * @return The instantiator. */ public static InjectionContainer create(CachePolicy dft) { return new InjectionContainer(dft); }
if (cached == null) { logger.debug("Node has not been memoized, instantiating: {}", node.getLabel()); Map<Desire, Instantiator> depMap = makeDependencyMap(node, backEdges);
@Override public Object instantiate(DAGNode<Component, Dependency> node) throws InjectionException { return container.makeInstantiator(node).instantiate(); } }
/** * Create a new instantiator with a default policy of {@code MEMOIZE}. * @return The instantiator. */ public static InjectionContainer create() { return create(CachePolicy.MEMOIZE); }
/** * Get a provider that, when invoked, will return an instance of the component represented * by a graph. * * * @param node The graph. * @return A provider to instantiate {@code graph}. * @see #makeInstantiator(DAGNode, SetMultimap) */ public Instantiator makeInstantiator(DAGNode<Component, Dependency> node) { return makeInstantiator(node, ImmutableSetMultimap.<DAGNode<Component, Dependency>, DAGEdge<Component, Dependency>>of()); }
DefaultImpl(LifecycleManager mgr) { container = InjectionContainer.create(CachePolicy.MEMOIZE, mgr); }
@Nullable @Override public Instantiator apply(@Nullable Desire input) { for (DAGEdge<Component,Dependency> edge: edges) { if (edge.getLabel().getInitialDesire().equals(input)) { return makeInstantiator(edge.getTail(), backEdges); } } return null; } }
/** * <p> * Create a new DefaultInjector. <tt>maxDepth</tt> represents the maximum * depth of the dependency hierarchy before it is assume that there is a * cycle. Bindings with a NO_PREFERENCE cache policy will use * <tt>defaultPolicy</tt>. * <p> * This constructor can be used to increase this depth in the event that * configuration requires it, although for most purposes the default 100 * should be sufficient. * * @param defaultPolicy The CachePolicy used in place of NO_PREFERENCE * @param maxDepth The maximum depth of the dependency hierarchy * @param functions The BindingFunctions to use, ordered with highest * priority functions first * @throws IllegalArgumentException if maxDepth is less than 1, or if * defaultPolicy is NO_PREFERENCE * @throws NullPointerException if spi or functions are null */ public DefaultInjector(CachePolicy defaultPolicy, int maxDepth, BindingFunction... functions) { if (defaultPolicy.equals(CachePolicy.NO_PREFERENCE)) { throw new IllegalArgumentException("Default CachePolicy cannot be NO_PREFERENCE"); } solver = DependencySolver.newBuilder() .addBindingFunctions(functions) .setMaxDepth(maxDepth) .build(); instantiator = InjectionContainer.create(defaultPolicy); }
@Override @SuppressWarnings("unchecked") public <T> T getInstance(Annotation qualifier, Class<T> type) throws InjectionException { // All Provider cache access, graph resolution, etc. occur // within this exclusive lock so we know everything is thread safe // albeit in a non-optimal way. synchronized(this) { Desire desire = Desires.create(qualifier, type, false); Predicate<Dependency> pred = Dependency.hasInitialDesire(desire); // check if the desire is already in the graph DAGEdge<Component, Dependency> resolved = solver.getGraph().getOutgoingEdgeWithLabel(pred); // The edge is only non-null if instantiate() has been called before, // it may be present in the graph at a deeper node. If that's the case // it will be properly merged after regenerating the graph at the root context. if (resolved == null) { logger.info("Must resolve desire: {}", desire); solver.resolve(desire); resolved = solver.getGraph().getOutgoingEdgeWithLabel(pred); } // Check if the provider for the resolved node is in our cache DAGNode<Component, Dependency> resolvedNode = resolved.getTail(); return type.cast(instantiator.makeInstantiator(resolvedNode, solver.getBackEdges()).instantiate()); } } }