/** * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to * satisfy injection requests. */ @Override public Collection<Edge> visit(ConstructorBinding<?> binding) { return newDependencyEdges(NodeId.newTypeId(binding.getKey()), binding); }
@Override public Void visit(final ConstructorBinding<?> binding) { final IServiceGraphVertex<?> bindingVertex = serviceGraph.findVertex(binding.getKey()).get(); final Map<Dependency<?>, Optional<IServiceGraphVertex<?>>> dependencyVertices = binding.getConstructor().getDependencies().stream() .collect(Collectors.toMap(Function.identity(), dependency -> serviceGraph.findVertex(dependency.getKey()))); dependencyVertices.forEach((dependency, vertex) -> { // If the dependency vertex already exists in the graph, then just add an edge between the two... if(vertex.isPresent()) { serviceGraph.addEdge(vertex.get(), bindingVertex); // ...otherwise, we have a key mismatch. This happens when the dependency uses an interface // but the Guice binding uses the implementing singleton class. In that case, we'll create // a vertex ad-hoc here for the interface and provide the edge. This will be merged in the DAG. } else { final Key<Object> dependencyKey = (Key<Object>)dependency.getKey(); final Injector injector = ((BindingImpl) binding).getInjector(); final Object dependencyInstance = injector.getInstance(dependencyKey); final IServiceGraphVertex<?> dependencyVertex = createGraphVertex(dependencyKey, dependencyInstance); serviceGraph.addVertex(dependencyVertex); serviceGraph.addEdge(dependencyVertex, bindingVertex); } }); return null; }