public DAGNode<Component,Dependency> buildGraph() throws ResolutionException { DependencySolver solver = buildDependencySolver(); for (Class<?> root: roots) { solver.resolve(Desires.create(null, root, true)); } return solver.getGraph(); }
/** * Create a new desire. * @param qualifier The qualifier applied to the type. * @param type The desired type. * @param nullable Whether this injection is nullable. * @return The desire. */ public static Desire create(@Nullable Annotation qualifier, Class<?> type, boolean nullable) { return new ReflectionDesire(createInjectionPoint(qualifier, type, nullable)); }
public Object readResolve() throws ObjectStreamException { try { return Desires.createInjectionPoint(qualifier, type.resolve(), nullable); } catch (ClassNotFoundException e) { InvalidObjectException ex = new InvalidObjectException("cannot resolve class " + type.getClassName()); ex.initCause(e); throw ex; } } }
@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; } } }
public DAGNode<Component,Dependency> buildGraph() throws ResolutionException { DependencySolver solver = buildDependencySolver(); for (Class<?> root: roots) { solver.resolve(Desires.create(null, root, true)); } return solver.getGraph(); }
@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()); } } }
Desire providedDesire = Desires.create(desire.getInjectionPoint().getQualifier(), providedType, desire.getInjectionPoint().isNullable());