@Nullable public <T> T tryGetInstance(Class<T> type) throws InjectionException { Desire d = Desires.create(null, type, true); DAGEdge<Component, Dependency> e = graph.getOutgoingEdgeWithLabel(l -> l.hasInitialDesire(d)); if (e != null) { return type.cast(instantiator.instantiate(e.getTail())); } else { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node != null) { return type.cast(instantiator.instantiate(node)); } else { return null; } } }
/** * Get an outgoing edge from this node with the specified label, if it exists. * * @param label The label. * @return An outgoing edge with the specified label, or {@code null} if no such edge exists. * If multiple edges have this label, an arbitrary one is returned. */ public DAGEdge<V,E> getOutgoingEdgeWithLabel(E label) { return getOutgoingEdgeWithLabel(Predicates.equalTo(label)); }
@Nullable public <T> T tryGetInstance(Class<T> type) throws InjectionException { Desire d = Desires.create(null, type, true); DAGEdge<Component, Dependency> e = graph.getOutgoingEdgeWithLabel(l -> l.hasInitialDesire(d)); if (e != null) { return type.cast(instantiator.instantiate(e.getTail())); } else { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node != null) { return type.cast(instantiator.instantiate(node)); } else { return null; } } }
@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()); } } }