@Override protected void configure() { bindListener(Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { final ProvisionListener.ProvisionInvocation<?> prior = ContextualProvider.provisionInvocation.get(); ContextualProvider.provisionInvocation.set(provision); try { provision.provision(); } finally { if(prior != null) { ContextualProvider.provisionInvocation.set(prior); } else { ContextualProvider.provisionInvocation.remove(); } } } }); }
public void testProvisionIsNotifiedAfterContextsClear() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindListener(Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision( ProvisionInvocation<T> provision) { Object provisioned = provision.provision(); if (provisioned instanceof X) { ((X) provisioned).init(); } else if (provisioned instanceof Y) { X.createY = false; ((Y) provisioned).init(); } } }); } }); X.createY = true; X x = injector.getInstance(X.class); assertNotSame(x, x.y.x); assertFalse("x.ID: " + x.ID + ", x.y.x.iD: " + x.y.x.ID, x.ID == x.y.x.ID); }
@Override public void configure(Binder binder) { // Some debugging if (log.isTraceEnabled()) { binder.bindListener(Matchers.any(), new ProvisionListener() { @Override @SuppressWarnings("rawtypes") public void onProvision(final ProvisionInvocation provision) { log.trace(">> Guice provisioning: {}",provision.getBinding()); } }); } log.warn("[START] R01F Bootstraping ________________________________"); //binder.requireExplicitBindings(); // All the injected members MUST be defined at the guice modules binder.install(new XMLPropertiesGuiceModule()); // XMLProperties binder.install(new ConfigPropertiesGuiceModule()); // Configs binder.install(new I18NGuiceModule()); // I18N binder.install(new GUIDDispenserGuiceModule()); // GUIDDispenser binder.install(new MarsallerGuiceModule()); // Marshaller binder.install(new JSonMarshallerGuiceModule()); // Marshalling de JSON log.warn(" [END] R01F Bootstraping ________________________________"); }
/** * Creates a new {@link ProvisionListenerStackCallback} with the correct listeners for the key. */ private <T> ProvisionListenerStackCallback<T> create(Binding<T> binding) { List<ProvisionListener> listeners = null; for (ProvisionListenerBinding provisionBinding : listenerBindings) { if (provisionBinding.getBindingMatcher().matches(binding)) { if (listeners == null) { listeners = Lists.newArrayList(); } listeners.addAll(provisionBinding.getListeners()); } } if (listeners == null || listeners.isEmpty()) { // Optimization: don't bother constructing the callback if there are // no listeners. return ProvisionListenerStackCallback.emptyListener(); } return new ProvisionListenerStackCallback<T>(binding, listeners); }
/** * Creates a new {@link ProvisionListenerStackCallback} with the correct listeners * for the key. */ private <T> ProvisionListenerStackCallback<T> create(Binding<T> binding) { List<ProvisionListener> listeners = null; for (ProvisionListenerBinding provisionBinding : listenerBindings) { if (provisionBinding.getBindingMatcher().matches(binding)) { if (listeners == null) { listeners = Lists.newArrayList(); } listeners.addAll(provisionBinding.getListeners()); } } if (listeners == null || listeners.isEmpty()) { // Optimization: don't bother constructing the callback if there are // no listeners. return ProvisionListenerStackCallback.emptyListener(); } return new ProvisionListenerStackCallback<T>(binding, listeners); }
public void testProvisionIsNotifiedAfterContextsClear() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindListener(Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { Object provisioned = provision.provision(); if (provisioned instanceof X) { ((X)provisioned).init(); } else if (provisioned instanceof Y) { X.createY = false; ((Y)provisioned).init(); } } }); } }); X.createY = true; X x = injector.getInstance(X.class); assertNotSame(x, x.y.x); assertFalse("x.ID: " + x.ID + ", x.y.x.iD: " + x.y.x.ID, x.ID == x.y.x.ID); }
@Override final public T get() { final ProvisionListener.ProvisionInvocation<T> pi = (ProvisionListener.ProvisionInvocation<T>) provisionInvocation.get(); if(pi == null) { return getWithoutContext(); } else { return getFor(pi.getBinding(), pi.getDependencyChain()); } }
@Override protected void configure() { workerPoolScope = new WorkerPoolScope(); Provider<Injector> injectorProvider = getProvider(Injector.class); bindListener(new AbstractMatcher<Binding<?>>() { @Override public boolean matches(Binding<?> binding) { return binding.getKey().getTypeLiteral().getRawType() == WorkerPool.class; } }, new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { WorkerPool workerPool = (WorkerPool) provision.provision(); workerPool.injector = injectorProvider.get(); workerPool.poolScope = workerPoolScope; } }); bindScope(Worker.class, workerPoolScope); bind(Integer.class).annotatedWith(WorkerId.class).toProvider(new Provider<Integer>() { @Override public Integer get() { return workerPoolScope.currentWorkerId; } }); }
public ProvisionListenerStackCallback(Binding<T> binding, List<ProvisionListener> listeners) { this.binding = binding; if (listeners.isEmpty()) { this.listeners = EMPTY_LISTENER; } else { Set<ProvisionListener> deDuplicated = Sets.newLinkedHashSet(listeners); this.listeners = deDuplicated.toArray(new ProvisionListener[deDuplicated.size()]); } }
public void testProvisionIsNotifiedAfterContextsClear() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bindListener( Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { Object provisioned = provision.provision(); if (provisioned instanceof X) { ((X) provisioned).init(); } else if (provisioned instanceof Y) { X.createY = false; ((Y) provisioned).init(); } } }); } }); X.createY = true; X x = injector.getInstance(X.class); assertNotSame(x, x.y.x); assertFalse("x.id: " + x.id + ", x.y.x.id: " + x.y.x.id, x.id == x.y.x.id); }
public ProvisionListenerStackCallback(Binding<T> binding, List<ProvisionListener> listeners) { this.binding = binding; if (listeners.isEmpty()) { this.listeners = EMPTY_LISTENER; } else { this.listeners = listeners.toArray(new ProvisionListener[listeners.size()]); } }
/** * Adapt a {@link Consumer} to be a {@link ProvisionListener} (which is not lambda compatible) */ static ProvisionListener provisionListener(Consumer<ProvisionListener.ProvisionInvocation<?>> consumer) { return consumer::accept; }
/** * Adapt the given matcher and listener and pass them to {@link Binder#bindListener(Matcher, ProvisionListener...)} */ default void bindProvisionListener(Predicate<? super Binding<?>> matcher, Consumer<ProvisionListener.ProvisionInvocation<?>> listener) { bindListener(Matchers.predicate(matcher), provisionListener(listener)); }
default <T> void bindProvisionSubtypesOfListener(TypeLiteral<T> type, Consumer<ProvisionListener.ProvisionInvocation<T>> listener) { bindProvisionListener(bindingsForSubtypesOf(type), (Consumer) listener); }
default <T> void bindProvisionSubtypesOfListener(Class<T> type, Consumer<ProvisionListener.ProvisionInvocation<T>> listener) { bindProvisionSubtypesOfListener(TypeLiteral.get(type), listener); }
@Override protected void configure() { final InjectionLogger listener = new InjectionLogger(logger); bindListener(Matchers.any(), (TypeListener) listener); bindListener(Matchers.any(), (ProvisionListener) listener); }
@Override default void bindListener(final Matcher<? super Binding<?>> bindingMatcher, final ProvisionListener... listeners) { this.binder().bindListener(bindingMatcher, listeners); }
/** * @see Binder#bindListener(Matcher, ProvisionListener...) * @since 4.0 */ protected void bindListener(Matcher<? super TypeToken<?>> typeMatcher, ProvisionListener... listener) { binder().bindListener(typeMatcher, listener); }
private Module getBindings(LogInject<_Logger_> logInject, Class<_Logger_> loggerClass) { TypeLiteral<_Logger_> loggerType = TypeLiteral.get(loggerClass); GuiceLoggerProvider<_Logger_> provider = new GuiceLoggerProvider<>(); Predicate<Dependency<?>> matchesLogger = dependency -> loggerType.equals(dependency.getKey().getTypeLiteral()); return new AbstractModule() { @Override protected void configure() { ProvisionListener provisionListener = new ProvisionListener() { @Override public <_Target_> void onProvision(ProvisionInvocation<_Target_> provision) { Binding<_Target_> binding = provision.getBinding(); if (loggerType.equals(binding.getKey().getTypeLiteral())) { Dependency<?> loggerDependency; Stream<Dependency<?>> stream; stream = provision.getDependencyChain().stream().map(DependencyAndSource::getDependency); Iterator<Dependency<?>> dependencies = reverse(stream.collect(toList())).iterator(); if (dependencies.hasNext() && matchesLogger.test(loggerDependency = dependencies.next())) { InjectionPoint injectionPoint = loggerDependency.getInjectionPoint(); TypeLiteral<?> declaringType = injectionPoint.getDeclaringType(); TypeLiteral<?> targetType = null; if (dependencies.hasNext()) { TypeLiteral<?> typeLiteral = dependencies.next().getKey().getTypeLiteral(); if (declaringType.getRawType().isAssignableFrom(typeLiteral.getRawType())) { targetType = typeLiteral; } } Class<?> logger = (targetType != null? targetType:declaringType).getRawType(); BindingTargetVisitor<_Target_, Void> bindingTargetVisitor; bindingTargetVisitor = new DefaultBindingTargetVisitor<_Target_, Void>() { @Override public Void visit(ProviderInstanceBinding<? extends _Target_> instanceBinding) { if (provider.equals(instanceBinding.getUserSuppliedProvider())) { provider.setLogger(logInject.createLogger(logger)); } return null; } }; binding.acceptTargetVisitor(bindingTargetVisitor); } } } }; bind(loggerClass).toProvider(provider); bindListener(Matchers.any(), provisionListener); } }; }
/** * @see Binder#bindListener(Matcher, ProvisionListener...) * @since 4.0 */ protected void bindListener( Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners) { binder().bindListener(bindingMatcher, listeners); }
/** * @see Binder#bindListener(Matcher, ProvisionListener...) * @since 4.0 */ protected void bindListener( Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listener) { binder().bindListener(bindingMatcher, listener); }
@SuppressWarnings("unchecked") public void testDependencyChain() { final List<Class<?>> pList = Lists.newArrayList(); final List<Class<?>> totalList = Lists.newArrayList(); Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Instance.class).toInstance(new Instance()); bind(B.class).to(BImpl.class); bind(D.class).toProvider(DP.class); bindListener( Matchers.any(), new ProvisionListener() { @Override public <T> void onProvision(ProvisionInvocation<T> provision) { totalList.add(provision.getBinding().getKey().getRawType()); } }); // Build up a list of asserters for our dependency chains. ImmutableList.Builder<Class<?>> chain = ImmutableList.builder(); chain.add(Instance.class); bindListener(keyMatcher(Instance.class), new ChainAsserter(pList, chain.build())); chain.add(A.class); bindListener(keyMatcher(A.class), new ChainAsserter(pList, chain.build())); chain.add(B.class).add(BImpl.class); bindListener(keyMatcher(BImpl.class), new ChainAsserter(pList, chain.build())); chain.add(C.class); bindListener(keyMatcher(C.class), new ChainAsserter(pList, chain.build())); // the chain has D before DP even though DP is provisioned & notified first // because we do DP because of D, and need DP to provision D. chain.add(D.class).add(DP.class); bindListener(keyMatcher(D.class), new ChainAsserter(pList, chain.build())); bindListener(keyMatcher(DP.class), new ChainAsserter(pList, chain.build())); chain.add(E.class); bindListener(keyMatcher(E.class), new ChainAsserter(pList, chain.build())); chain.add(F.class); bindListener(keyMatcher(F.class), new ChainAsserter(pList, chain.build())); } @Provides C c(D d) { return new C() {}; } }); injector.getInstance(Instance.class); // make sure we're checking all of the chain asserters.. assertEquals( of(Instance.class, A.class, BImpl.class, C.class, DP.class, D.class, E.class, F.class), pList); // and make sure that nothing else was notified that we didn't expect. assertEquals(totalList, pList); }
/** * @see Binder#bindListener(Matcher, ProvisionListener...) */ protected void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners) { binder().bindListener(bindingMatcher, listeners); }
/** * @see Binder#bindListener(Matcher, ProvisionListener...) * @since 4.0 */ protected void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listener) { binder().bindListener(bindingMatcher, listener); }
@SuppressWarnings("unchecked") public void testDependencyChain() { final List<Class<?>> pList = Lists.newArrayList(); final List<Class<?>> totalList = Lists.newArrayList(); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Instance.class).toInstance(new Instance()); bind(B.class).to(BImpl.class); bind(D.class).toProvider(DP.class); bindListener(Matchers.any(), new ProvisionListener() { public <T> void onProvision(ProvisionInvocation<T> provision) { totalList.add(provision.getBinding().getKey().getRawType()); } }); // Build up a list of asserters for our dependency chains. ImmutableList.Builder<Class<?>> chain = ImmutableList.builder(); chain.add(Instance.class); bindListener(keyMatcher(Instance.class), new ChainAsserter(pList, chain.build())); chain.add(A.class); bindListener(keyMatcher(A.class), new ChainAsserter(pList, chain.build())); chain.add(B.class).add(BImpl.class); bindListener(keyMatcher(BImpl.class), new ChainAsserter(pList, chain.build())); chain.add(C.class); bindListener(keyMatcher(C.class), new ChainAsserter(pList, chain.build())); // the chain has D before DP even though DP is provisioned & notified first // because we do DP because of D, and need DP to provision D. chain.add(D.class).add(DP.class); bindListener(keyMatcher(D.class), new ChainAsserter(pList, chain.build())); bindListener(keyMatcher(DP.class), new ChainAsserter(pList, chain.build())); chain.add(E.class); bindListener(keyMatcher(E.class), new ChainAsserter(pList, chain.build())); chain.add(F.class); bindListener(keyMatcher(F.class), new ChainAsserter(pList, chain.build())); } @Provides C c(D d) { return new C() {}; } }); Instance instance = injector.getInstance(Instance.class); // make sure we're checking all of the chain asserters.. assertEquals( of(Instance.class, A.class, BImpl.class, C.class, DP.class, D.class, E.class, F.class), pList); // and make sure that nothing else was notified that we didn't expect. assertEquals(totalList, pList); }
/** * Registers listeners for provisioned objects. Guice will notify the * listeners just before and after the object is provisioned. Provisioned * objects that are also injectable (everything except objects provided * through Providers) can also be notified through TypeListeners registered * in {@link #bindListener}. * * @param bindingMatcher * that matches bindings of provisioned objects the listener * should be notified of * @param listeners * for provisioned objects matched by bindingMatcher * @since 4.0 */ void bindListener(Matcher<? super TypeToken<?>> bindingMatcher, ProvisionListener... listeners);
/** * Registers listeners for provisioned objects. Guice will notify the listeners just before and * after the object is provisioned. Provisioned objects that are also injectable (everything * except objects provided through Providers) can also be notified through TypeListeners * registered in {@link #bindListener}. * * @param bindingMatcher that matches bindings of provisioned objects the listener should be * notified of * @param listeners for provisioned objects matched by bindingMatcher * @since 4.0 */ void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners);
/** * Registers listeners for provisioned objects. Guice will notify the * listeners just before and after the object is provisioned. Provisioned * objects that are also injectable (everything except objects provided * through Providers) can also be notified through TypeListeners registered in * {@link #bindListener}. * * @param bindingMatcher that matches bindings of provisioned objects the listener * should be notified of * @param listeners for provisioned objects matched by bindingMatcher * @since 4.0 */ void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners);