@Override protected void configure() { final Destroyer destroyer = new Destroyer(errorHandler); bind(Destroyer.class).toInstance(destroyer); bindListener( Matchers.any(), new TypeListener() { @Override public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) { encounter.register( new InjectionListener<T>() { @Override public void afterInjection(T injectee) { final Method[] methods = get(injectee.getClass(), annotationType); if (methods.length > 0) { // copy array when pass it outside final Method[] copy = new Method[methods.length]; System.arraycopy(methods, 0, copy, 0, methods.length); destroyer.add(injectee, copy); } } }); } }); }
@Override protected void configure() { bind(ActionRepository.class).to(CassandraActionRepository.class); bind(PlayerRepository.class).to(CassandraPlayerRepository.class); bind(BadgeRepository.class).to(CassandraBadgeRepository.class); bind(EventRepository.class).to(CassandraEventRepository.class); bind(EventTypeRepository.class).to(CassandraEventTypeRepository.class); this.eventBus = new AsyncEventBus(java.util.concurrent.Executors.newCachedThreadPool()); bind(EventBus.class).toInstance(eventBus); bindListener(Matchers.any(), new TypeListener() { public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { typeEncounter.register(new InjectionListener<I>() { public void afterInjection(I i) { eventBus.register(i); } }); } }); }
void notifyListeners(T instance) throws InternalProvisionException { ImmutableList<InjectionListener<? super T>> localInjectionListeners = injectionListeners; if (localInjectionListeners == null) { // no listeners return; } // optimization: use manual for/each to save allocating an iterator here for (int i = 0; i < localInjectionListeners.size(); i++) { InjectionListener<? super T> injectionListener = localInjectionListeners.get(i); try { injectionListener.afterInjection(instance); } catch (RuntimeException e) { throw InternalProvisionException.errorNotifyingInjectionListener( injectionListener, typeLiteral, e); } } }
@Override @SuppressWarnings("unchecked") public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final Class<? super I> actualType = type.getRawType(); if (!Utils.isPackageValid(actualType)) { return; } if (typeClass.isAssignableFrom(actualType)) { encounter.register(new InjectionListener<I>() { @Override public void afterInjection(final I injectee) { try { postProcessor.process((T) injectee); } catch (Exception ex) { throw new IllegalStateException( String.format("Failed to process type %s of class %s", typeClass.getSimpleName(), injectee.getClass().getSimpleName()), ex); } } }); } }
@Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { if (Initializable.class.isAssignableFrom(type.getRawType())) { encounter.register(new InjectionListener<I>() { @Override public void afterInjection(I injectee) { if (injectee instanceof Initializable) { Initializable initializable = (Initializable) injectee; initializable.init(); } } }); } else { encounter.addError("Unsupported type: %s", type); } }
private void mapUtil() { final EventBus eventBus = new EventBus(); bind(EventBus.class).toInstance(eventBus); bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(@SuppressWarnings("unused") final TypeLiteral<I> type, final TypeEncounter<I> encounter) { encounter.register(new InjectionListener<I>() { @Override public void afterInjection(final I injectee) { eventBus.register(injectee); } }); } }); requestStaticInjection(TextUtil.class); }
protected void bindMBeanListener() { final MBeanAnnotationScanner scanner = new MBeanAnnotationScanner(); this.bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final MBeanMetadata metadata = scanner.scan(type.getRawType()); if (metadata != null) { encounter.register((InjectionListener<I>) injectee -> { try { final ResourceDynamicMBean dynamicMBean = new ResourceDynamicMBean(injectee, metadata); mBeanRegistry.register(dynamicMBean, metadata.getObjectName()); } catch (Exception e) { LOG.error("Could not register MBean {}", metadata.getObjectName(), e); } }); } } }); }
/** * Invokes all registered InjectionListeners. * * @param injectee The object that has been injected. */ @Override public void afterInjection(Object injectee) { for (InjectionListener<Object> listener : listeners) { // Only invoke singletons once, if oncePerSingleton is true if (oncePerSingleton && injectee.getClass().isAnnotationPresent(Singleton.class)) { if (vistedClasses.contains(injectee.getClass())) { continue; } else { vistedClasses.add(injectee.getClass()); } } listener.afterInjection(injectee); } }
@Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { logger.info("Encountered type " + type); encounter.register((InjectionListener<I>) injectee -> { logger.info("Injected " + injectee); }); }
@Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register(new InjectionListener<I>() { @Override public void afterInjection(I injectee) { if (!(injectee instanceof Ignite)) { Injection.postCreate(injectee, ignite); extractLifecycle(injectee, lifecycleBeans); } } }); }
@Override public void configure(Binder binder) { binder.disableCircularProxies(); binder.bindListener(any(), new TypeListener() { @Override public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) { encounter.register(new InjectionListener<T>() { @Override public void afterInjection(T obj) { if (isLifeCycleClass(obj.getClass())) { LifeCycleManager lifeCycleManager = lifeCycleManagerRef.get(); if (lifeCycleManager != null) { try { lifeCycleManager.addInstance(obj); } catch (Exception e) { throw new Error(e); } } else { injectedInstances.add(obj); } } } }); } }); }
@Override public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { encounter.register(// AFTER the type is injected it MUST be registered at the EventBus new InjectionListener<I>() { @Override public void afterInjection(final I injecteeEventListener) { log.warn("\tRegistering {} event listener at event bus {}", injecteeEventListener.getClass(), _eventBusProvider.get()); _eventBusProvider.get() .register(injecteeEventListener); // register the indexer (the indexer is an event listener) } }); }
@Override public void configure(Binder binder) { binder.disableCircularProxies(); binder.bindListener(Matchers.any(), new TypeListener() { @Override public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) { encounter.register(new InjectionListener<T>() { @Override public void afterInjection(T obj) { ServerLifeCycleManager initialized = lifeCycleManagerRef.get(); if (initialized == null) { earlyInjected.add(obj); } else { try { initialized.manageInstance(obj); } catch (Exception e) { // really nothing we can do here throw new Error(e); } } } }); } }); }
private TypeListener createTypeListener() { return new TypeListener() { @Override public <I> void hear(final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) { typeEncounter.register((InjectionListener<I>) LoggerInjector::inject); } }; }
private TypeListener createTypeListener(final ObjectConverterRegistry registry) { return new TypeListener() { @Override public <I> void hear(final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) { typeEncounter.register(new InjectionListener<I>() { @Override public void afterInjection(final I injectee) { registry.registerConverters(injectee); } }); } }; }
@Override public <I> void hear(final TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { Class<? super I> clz = typeLiteral.getRawType(); if (packagePrefix != null && !clz.getName().startsWith(packagePrefix)) return; final Method method = getPostConstructMethod(clz); if (method != null) { typeEncounter.register(new InjectionListener<I>() { @Override public void afterInjection(Object i) { try { // call the @PostConstruct annotated method after all dependencies have been injected method.invoke(i); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } }); } }
@Override public void register(InjectionListener<? super T> injectionListener) { checkState(valid, "Encounters may not be used after hear() returns."); if (injectionListeners == null) { injectionListeners = Lists.newArrayList(); } injectionListeners.add(injectionListener); }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { ++count; MembersInjector membersInjector = new CountingMembersInjector(); encounter.register(membersInjector); encounter.register(membersInjector); InjectionListener injectionListener = new CountingInjectionListener(); encounter.register(injectionListener); encounter.register(injectionListener); }
private void messageBusRegisterRule() { // This rule registers all objects created by Guice with the message bus. // As a result, all instances created by Guice can receive messages without explicit // subscription. bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { typeEncounter.register(new InjectionListener<I>() { public void afterInjection(I i) { messageBus.subscribe(i); } }); } }); }
@Override protected void configure() { bind(SampleApplication.class); bind(MessageConverter.class).to(JsonMessageConverter.class).in(Singleton.class); bind(ObjectConverter.class).to(JsonObjectConverter.class).in(Singleton.class); bind(SubscriptionAdapter.class).to(MapSubscriptionAdapter.class).in(Singleton.class); bind(DDPMessageEndpoint.class).to(DDPMessageEndpointImpl.class).in(Singleton.class); bind(SubscriptionEventDispatcher.class).asEagerSingleton(); bind(MeteorCollectionRepository.class).to(MeteorCollectionRepositoryImpl.class).in(Singleton.class); bind(RPCClient.class).to(RPCClientImpl.class).in(Singleton.class); bind(EventBus.class).in(Singleton.class); bindListener( new AbstractMatcher<TypeLiteral<?>>() { @Override public boolean matches(TypeLiteral<?> typeLiteral) { return typeLiteral.getRawType().isAnnotationPresent(Presents.class); } }, new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { final Presents presents = type.getRawType().getAnnotation(Presents.class); encounter.register((InjectionListener<I>) injectee -> { final FXMLLoader loader = new FXMLLoader(injectee.getClass().getResource(presents.value())); loader.setController(injectee); try { loader.load(); } catch (IOException e) { throw new RuntimeException(e); } }); } } ); }
public void register(InjectionListener<? super T> injectionListener) { checkState(valid, "Encounters may not be used after hear() returns."); if (injectionListeners == null) { injectionListeners = Lists.newArrayList(); } injectionListeners.add(injectionListener); }
void notifyListeners(T instance, Errors errors) throws ErrorsException { int numErrorsBefore = errors.size(); for (InjectionListener<? super T> injectionListener : injectionListeners) { try { injectionListener.afterInjection(instance); } catch (RuntimeException e) { errors.errorNotifyingInjectionListener(injectionListener, typeLiteral, e); } } errors.throwIfNewErrors(numErrorsBefore); }
public void testInstallingInjectionListener() { final List<Object> injectees = Lists.newArrayList(); final InjectionListener<Object> injectionListener = new InjectionListener<Object>() { public void afterInjection(Object injectee) { injectees.add(injectee); } }; Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindListener(onlyAbcd, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register(injectionListener); } }); bind(A.class); } }); assertEquals(ImmutableList.of(), injectees); Object a1 = injector.getInstance(A.class); assertEquals(ImmutableList.of(a1), injectees); Object a2 = injector.getInstance(A.class); assertEquals(ImmutableList.of(a1, a2), injectees); Object b1 = injector.getInstance(B.class); assertEquals(ImmutableList.of(a1, a2, b1), injectees); Provider<A> aProvider = injector.getProvider(A.class); assertEquals(ImmutableList.of(a1, a2, b1), injectees); A a3 = aProvider.get(); A a4 = aProvider.get(); assertEquals(ImmutableList.of(a1, a2, b1, a3, a4), injectees); }
@SuppressWarnings({"rawtypes", "unchecked"}) public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { ++count; MembersInjector membersInjector = new CountingMembersInjector(); encounter.register(membersInjector); encounter.register(membersInjector); InjectionListener injectionListener = new CountingInjectionListener(); encounter.register(injectionListener); encounter.register(injectionListener); }
@Override @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final Class<? super I> actualType = type.getRawType(); if (!Utils.isPackageValid(actualType)) { return; } Class<? super I> investigatingType = actualType; while (investigatingType != null && !investigatingType.equals(Object.class)) { for (final Field field : investigatingType.getDeclaredFields()) { if (field.isAnnotationPresent(annotationClass)) { encounter.register(new InjectionListener<I>() { @Override public void afterInjection(final I injectee) { try { field.setAccessible(true); postProcessor.process(field.getAnnotation(annotationClass), field, injectee); } catch (Exception ex) { throw new IllegalStateException( String.format("Failed to process annotation %s on field %s of class %s", annotationClass.getSimpleName(), field.getName(), injectee.getClass().getSimpleName()), ex); } } }); } } investigatingType = investigatingType.getSuperclass(); } }
@Override @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final Class<? super I> actualType = type.getRawType(); if (!Utils.isPackageValid(actualType)) { return; } Class<? super I> investigatingType = actualType; while (investigatingType != null && !investigatingType.equals(Object.class)) { for (final Method method : investigatingType.getDeclaredMethods()) { if (method.isAnnotationPresent(annotationClass)) { encounter.register(new InjectionListener<I>() { @Override public void afterInjection(final I injectee) { try { method.setAccessible(true); postProcessor.process(method.getAnnotation(annotationClass), method, injectee); } catch (Exception ex) { throw new IllegalStateException( String.format("Failed to process annotation %s on method %s of class %s", annotationClass.getSimpleName(), method.getName(), injectee.getClass().getSimpleName()), ex); } } }); } } investigatingType = investigatingType.getSuperclass(); } }
public void testInstallingInjectionListener() { final List<Object> injectees = Lists.newArrayList(); final InjectionListener<Object> injectionListener = new InjectionListener<Object>() { public void afterInjection(Object injectee) { injectees.add(injectee); } }; Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { bindListener(onlyAbcd, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register(injectionListener); } }); bind(A.class); } }); assertEquals(ImmutableList.of(), injectees); Object a1 = injector.getInstance(A.class); assertEquals(ImmutableList.of(a1), injectees); Object a2 = injector.getInstance(A.class); assertEquals(ImmutableList.of(a1, a2), injectees); Object b1 = injector.getInstance(B.class); assertEquals(ImmutableList.of(a1, a2, b1), injectees); Provider<A> aProvider = injector.getProvider(A.class); assertEquals(ImmutableList.of(a1, a2, b1), injectees); A a3 = aProvider.get(); A a4 = aProvider.get(); assertEquals(ImmutableList.of(a1, a2, b1, a3, a4), injectees); }
@Override protected void configure() { LOG.info("Loading PaasModule"); bind(EventBus.class).toInstance(eventBus); bindListener(Matchers.any(), new TypeListener() { public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { typeEncounter.register(new InjectionListener<I>() { public void afterInjection(I i) { eventBus.register(i); } }); } }); bind(TaskManager.class).to(InlineTaskManager.class); // Constants bind(String.class).annotatedWith(Names.named("namespace")).toInstance("com.netflix.pass."); bind(String.class).annotatedWith(Names.named("appname" )).toInstance("paas"); bind(AbstractConfiguration.class).toInstance(ConfigurationManager.getConfigInstance()); // Configuration bind(PaasConfiguration.class).to(ArchaeusPaasConfiguration.class).in(Scopes.SINGLETON); // Stuff bind(ScheduledExecutorService.class).annotatedWith(Names.named("tasks")).toInstance(Executors.newScheduledThreadPool(10)); bind(DaoProvider.class).in(Scopes.SINGLETON); // Rest resources bind(DataResource.class).in(Scopes.SINGLETON); bind(SchemaAdminResource.class).to(JerseySchemaAdminResourceImpl.class).in(Scopes.SINGLETON); bind(SchemaService.class).to(DaoSchemaService.class).in(Scopes.SINGLETON); }
@Override protected void configure() { bindListener(ClassToTypeLiteralMatcherAdapter.adapt(Matchers.annotatedWith(JmxBean.class)), new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { final Provider<JmxExporter> exporter = encounter.getProvider(JmxExporter.class); encounter.register(new InjectionListener<I>() { @Override public void afterInjection(I injectee) { exporter.get().register(injectee); } }); } }); }
private static Injector createInject() { Settings settings = Settings.build(); Environment environment = new Environment(settings); ArrayList<Module> modules = Lists.newArrayList(); modules.add(new AbstractModule() { @Override protected void configure() { bind(Settings.class).toInstance(settings); bind(Environment.class).toInstance(environment); bind(MynlpResourceFactory.class).toInstance(environment.getMynlpResourceFactory()); //process initialize interface bindListener(new AbstractMatcher<TypeLiteral<?>>() { @Override public boolean matches(TypeLiteral<?> t) { return InitInterface.class.isAssignableFrom(t.getRawType()); } }, new TypeListener() { @SuppressWarnings({"unchecked", "rawtypes"}) @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register((InjectionListener) injectee -> { InitInterface initInterface = (InitInterface) injectee; try { initInterface.init(); } catch (Exception e) { e.printStackTrace(); } }); } }); } }); //加载模块,在配置文件中声明的 modules.addAll(loadModules(environment)); Injector injector = Guice.createInjector(modules); return injector; }
@Override protected void configure() { bind(GripMode.class).toInstance(GripMode.GUI); bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(final TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) { typeEncounter.register((InjectionListener<I>) i -> { if (!i.getClass().isAnnotationPresent(ParametrizedController.class)) { return; } try { FXMLLoader.load(i.getClass().getResource( i.getClass().getAnnotation(ParametrizedController.class).url()), null, null, c -> i ); } catch (IOException e) { throw new IllegalStateException("Failed to load FXML", e); } }); } }); install(new FactoryModuleBuilder() .implement(StepController.class, StepController.class) .build(StepController.Factory.class)); // Source Factories install(new FactoryModuleBuilder().build(new TypeLiteral<SourceController .BaseSourceControllerFactory<Source>>() { })); install(new FactoryModuleBuilder().build(MultiImageFileSourceController.Factory.class)); install(new FactoryModuleBuilder().build(CameraSourceController.Factory.class)); install(new FactoryModuleBuilder().build(HttpSourceController.Factory.class)); install(new FactoryModuleBuilder().build(ClassifierSourceController.Factory.class)); install(new FactoryModuleBuilder().build(VideoFileSourceController.Factory.class)); // END Source Factories // Components install(new FactoryModuleBuilder().build(StartStoppableButton.Factory.class)); install(new FactoryModuleBuilder().build(ExceptionWitnessResponderButton.Factory.class)); // End Components // Controllers install(new FactoryModuleBuilder().build(OperationController.Factory.class)); install(new FactoryModuleBuilder().build(SocketHandleView.Factory.class)); install(new FactoryModuleBuilder().build(OutputSocketController.Factory.class)); // End arbitrary controllers // InputSocketController Factories install(new FactoryModuleBuilder().build(new TypeLiteral<InputSocketController .BaseInputSocketControllerFactory<Object>>() { })); install(new FactoryModuleBuilder().build(CheckboxInputSocketController.Factory.class)); install(new FactoryModuleBuilder().build(ListSpinnerInputSocketController.Factory.class)); install(new FactoryModuleBuilder().build(RangeInputSocketController.Factory.class)); install(new FactoryModuleBuilder().build(new TypeLiteral<SelectInputSocketController .Factory<Object>>() { })); install(new FactoryModuleBuilder().build(NumberSpinnerInputSocketController.Factory.class)); install(new FactoryModuleBuilder().build(SliderInputSocketController.Factory.class)); install(new FactoryModuleBuilder().build(TextFieldInputSocketController.Factory.class)); // END Input Socket Controller Factories }
@Override protected void configure() { bind(GripMode.class).toInstance(GripMode.HEADLESS); // Register any injected object on the event bus bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register((InjectionListener<I>) eventBus::register); } }); bind(EventBus.class).toInstance(eventBus); bind(EventLogger.class).asEagerSingleton(); // Allow for just injecting the settings provider, instead of the whole pipeline bind(SettingsProvider.class).to(Pipeline.class); install(new FactoryModuleBuilder().build(new TypeLiteral<Connection.Factory<Object>>() { })); bind(StepIndexer.class).to(Pipeline.class); bind(ConnectionValidator.class).to(Pipeline.class); bind(Source.SourceFactory.class).to(Source.SourceFactoryImpl.class); bind(InputSocket.Factory.class).to(InputSocketImpl.FactoryImpl.class); bind(OutputSocket.Factory.class).to(OutputSocketImpl.FactoryImpl.class); install(new FactoryModuleBuilder() .implement(CameraSource.class, CameraSource.class) .build(CameraSource.Factory.class)); install(new FactoryModuleBuilder() .implement(ImageFileSource.class, ImageFileSource.class) .build(ImageFileSource.Factory.class)); install(new FactoryModuleBuilder() .implement(MultiImageFileSource.class, MultiImageFileSource.class) .build(MultiImageFileSource.Factory.class)); install(new FactoryModuleBuilder() .implement(HttpSource.class, HttpSource.class) .build(HttpSource.Factory.class)); install(new FactoryModuleBuilder() .implement(NetworkTableEntrySource.class, NetworkTableEntrySource.class) .build(NetworkTableEntrySource.Factory.class)); install(new FactoryModuleBuilder() .implement(ClassifierSource.class, ClassifierSource.class) .build(ClassifierSource.Factory.class)); install(new FactoryModuleBuilder() .implement(VideoFileSource.class, VideoFileSource.class) .build(VideoFileSource.Factory.class)); install(new FactoryModuleBuilder().build(ExceptionWitness.Factory.class)); install(new FactoryModuleBuilder().build(Timer.Factory.class)); bind(BenchmarkRunner.class).asEagerSingleton(); bind(Cleaner.class).asEagerSingleton(); }
public static InternalProvisionException errorNotifyingInjectionListener( InjectionListener<?> listener, TypeLiteral<?> type, RuntimeException cause) { return errorInUserCode( cause, "Error notifying InjectionListener %s of %s.%n Reason: %s", listener, type, cause); }
ImmutableSet<InjectionListener<? super T>> getInjectionListeners() { return injectionListeners == null ? ImmutableSet.<InjectionListener<? super T>>of() : ImmutableSet.copyOf(injectionListeners); }
public void testInstallingInjectionListener() { final List<Object> injectees = Lists.newArrayList(); final InjectionListener<Object> injectionListener = new InjectionListener<Object>() { @Override public void afterInjection(Object injectee) { injectees.add(injectee); } }; Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bindListener( onlyAbcd, new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register(injectionListener); } }); bind(A.class); } }); assertEquals(ImmutableList.of(), injectees); Object a1 = injector.getInstance(A.class); assertEquals(ImmutableList.of(a1), injectees); Object a2 = injector.getInstance(A.class); assertEquals(ImmutableList.of(a1, a2), injectees); Object b1 = injector.getInstance(B.class); assertEquals(ImmutableList.of(a1, a2, b1), injectees); Provider<A> aProvider = injector.getProvider(A.class); assertEquals(ImmutableList.of(a1, a2, b1), injectees); A a3 = aProvider.get(); A a4 = aProvider.get(); assertEquals(ImmutableList.of(a1, a2, b1, a3, a4), injectees); }
public Errors errorNotifyingInjectionListener( InjectionListener<?> listener, TypeLiteral<?> type, RuntimeException cause) { return errorInUserCode(cause, "Error notifying InjectionListener %s of %s.%n" + " Reason: %s", listener, type, cause); }
@Override public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final Class<?> discoveredType = type.getRawType(); int reconfigurables = 0; // Process @Inject constructor for (Constructor<?> constructor : discoveredType.getDeclaredConstructors()) { if (constructor.isAnnotationPresent(Inject.class)) { reconfigurables += processMethod(discoveredType, constructor); } } // Walk up the hierarchy and look for the declared fields for (Class<?> clazz = discoveredType; clazz != null; clazz = clazz.getSuperclass()) { // Process @Inject @Named fields for (Field field : clazz.getDeclaredFields()) { if (field.isAnnotationPresent(Inject.class) && field.isAnnotationPresent(Named.class)) { final Named named = field.getAnnotation(Named.class); registry.register(discoveredType, injectorRef, named.value(), field.getType(), field); reconfigurables++; } } // Process @Inject methods for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(Inject.class)) { reconfigurables += processMethod(discoveredType, method); } } } // If there were config properties in this type, register an InjectionListener to grab instances for runtime reconfiguration // TODO should all the props be @Reconfigurable for this? if (reconfigurables > 0) encounter.register(new InjectionListener<I>() { @Override public void afterInjection(final I injectee) { registry.addInstance(discoveredType, injectee); } }); }