@Override public Object instantiate(DAGNode<Component, Dependency> node) throws InjectionException { return container.makeInstantiator(node).instantiate(); } }
@Override public Object instantiate() throws ConstructionException { if (!instantiated) { synchronized (this) { if (!instantiated) { try { instance = delegate.instantiate(); } catch (Throwable th) { error = th; } instantiated = true; } } } if (error != null) { Throwables.propagateIfPossible(error, ConstructionException.class); // shouldn't happen, but hey. throw new RuntimeException("Unexpected instantiation exception", error); } else { return instance; } }
@Override public Object instantiate(DAGNode<Component, Dependency> node) throws InjectionException { return container.makeInstantiator(node).instantiate(); } }
@Override public Object get() { try { logger.trace("invoking instantiator {}", instantiator); return getProvidedType().cast(instantiator.instantiate()); } catch (ConstructionException ex) { throw new RuntimeException(ex); } } }
@Override public void inject(InjectionPoint ip, Object instance, Instantiator provider, Map<Method, ClassInstantiator.InjectionArgs> settersAndArguments) throws ConstructionException { Field field; Object value; FieldInjectionPoint fd = (FieldInjectionPoint)ip; try { value = ClassInstantiator.checkNull(fd, provider.instantiate()); field = fd.getMember(); logger.trace("Setting field {} with arguments {}", field, value); field.setAccessible(true); field.set(instance, value); } catch (IllegalAccessException e) { throw new ConstructionException(fd, e); } } },
@Override public Object instantiate() throws ConstructionException { Provider<?> provider = (Provider) providerInstantiator.instantiate(); LogContext mdcContextProvider = LogContext.create(); logger.trace("invoking provider {}",provider); try { mdcContextProvider.put("org.grouplens.grapht.currentProvider", provider.toString()); return provider.get(); } catch (Throwable th) { throw new ConstructionException(getType(), "Error invoking provider " + providerInstantiator, th); } finally { mdcContextProvider.finish(); } } @SuppressWarnings("unchecked")
settersAndArguments.put(setter, args); args.set(st.getParameterIndex(), ClassInstantiator.checkNull(st, provider.instantiate()));
@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()); } } }
ipContext.finish(); ctorArgs[cd.getParameterIndex()] = checkNull(cd, provider.instantiate());