@Override protected void configure() { registerConfigFiles(this.configFiles); //init system runtime eagerly bind(SystemRuntime.IRuntime.class).to(BaseRuntime.class).asEagerSingleton(); bind(IResourceClientLibrary.class).to(ResourceClientLibrary.class).in(Scopes.SINGLETON); bind(IBotStoreClientLibrary.class).to(BotStoreClientLibrary.class).in(Scopes.SINGLETON); bind(IPackageStoreClientLibrary.class).to(PackageStoreClientLibrary.class).in(Scopes.SINGLETON); bind(IPackageStoreService.class).to(PackageStoreService.class).in(Scopes.SINGLETON); bind(IBotStoreService.class).to(BotStoreService.class).in(Scopes.SINGLETON); bind(IBotFactory.class).to(BotFactory.class).in(Scopes.SINGLETON); bind(IPackageFactory.class).to(PackageFactory.class).in(Scopes.SINGLETON); bind(IAutoBotDeployment.class).to(AutoBotDeployment.class).in(Scopes.SINGLETON); //call init method of system runtime after creation bindListener(HasInitMethod.INSTANCE, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register(InitInvoker.INSTANCE); } }); }
@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); } } }); } }); }
/** * {@inheritDoc} */ @Override public final <I> void hear( final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) { Preconditions.checkNotNull(typeLiteral); Preconditions.checkNotNull(typeEncounter); // Go through the class and its ancestors to find fields like this: // @InjectLogger private Logger logger; Class<?> clazz = typeLiteral.getRawType(); while (clazz != null) { for (final Field field : clazz.getDeclaredFields()) { if (field.getType() == ILogger.class && field.isAnnotationPresent(InjectLogger.class)) { typeEncounter.register(new MetaborgLoggerMembersInjector<>(field)); } } clazz = clazz.getSuperclass(); } }
/** * {@inheritDoc} */ @Override public final <I> void hear( final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) { Preconditions.checkNotNull(typeLiteral); Preconditions.checkNotNull(typeEncounter); // Go through the class and its ancestors to find fields like this: // @InjectLogger private Logger logger; Class<?> clazz = typeLiteral.getRawType(); while (clazz != null) { for (final Field field : clazz.getDeclaredFields()) { if (field.getType() == Logger.class && field.isAnnotationPresent(InjectLogger.class)) { typeEncounter.register(new Slf4JLoggerMembersInjector<>(field)); } } clazz = clazz.getSuperclass(); } }
@Override public <T> void hear(final TypeLiteral<T> typeLiteral, final TypeEncounter<T> typeEncounter) { for (Field field : typeLiteral.getRawType().getDeclaredFields()) { if (!field.isAnnotationPresent(InjectLogger.class)) continue; if (field.getType() == org.apache.log4j.Logger.class) typeEncounter.register(new Log4JMembersInjector<T>(field)); else if (field.getType() == org.slf4j.Logger.class) typeEncounter.register(new SLF4JMembersInjector<T>(field)); else if (field.getType() == java.util.logging.Logger.class) typeEncounter.register(new JULMembersInjector<T>(field)); else LOG.warn("@" + InjectLogger.class.getSimpleName() + " annotated unknown logger type " + field.getType()); // TODO add/inject other logger type implementations } }
@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); } }); } }); }
public void testToConstructorSpiData() throws NoSuchMethodException { final Set<TypeLiteral<?>> heardTypes = Sets.newHashSet(); final Constructor<D> constructor = D.class.getConstructor(Stage.class); final TypeListener listener = new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { if (!heardTypes.add(type)) { fail("Heard " + type + " multiple times!"); } } }; Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Object.class).toConstructor(constructor); bind(D.class).toConstructor(constructor); bindListener(Matchers.any(), listener); } }); assertEquals(ImmutableSet.of(TypeLiteral.get(D.class)), heardTypes); }
public void testTypeListenersAreFired() { final AtomicInteger firedCount = new AtomicInteger(); final TypeListener typeListener = new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { assertEquals(new TypeLiteral<A>() {}, type); firedCount.incrementAndGet(); } }; Guice.createInjector( new AbstractModule() { @Override protected void configure() { bindListener(onlyAbcd, typeListener); bind(A.class); } }); assertEquals(1, firedCount.get()); }
public void testLookupsPostCreate() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bindListener( only(TypeLiteral.get(C.class)), new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { assertNotNull(encounter.getProvider(B.class).get()); A a = new A(); encounter.getMembersInjector(A.class).injectMembers(a); assertNotNull(a.injector); } }); } }); injector.getInstance(C.class); }
/** * We had a bug where we weren't notifying of types encountered for member injection when those * types had no members to be injected. Constructed types are always injected because they always * have at least one injection point: the class constructor. */ public void testTypesWithNoInjectableMembersAreNotified() { final AtomicInteger notificationCount = new AtomicInteger(); Guice.createInjector( new AbstractModule() { @Override protected void configure() { bindListener( onlyAbcd, new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { notificationCount.incrementAndGet(); } }); bind(C.class).toInstance(new C()); } }); assertEquals(1, notificationCount.get()); }
public void testToConstructorSpiData() throws NoSuchMethodException { final Set<TypeLiteral<?>> heardTypes = Sets.newHashSet(); final Constructor<D> constructor = D.class.getConstructor(Stage.class); final TypeListener listener = new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { if (!heardTypes.add(type)) { fail("Heard " + type + " multiple times!"); } } }; Guice.createInjector(new AbstractModule() { protected void configure() { bind(Object.class).toConstructor(constructor); bind(D.class).toConstructor(constructor); bindListener(Matchers.any(), listener); } }); assertEquals(ImmutableSet.of(TypeLiteral.get(D.class)), heardTypes); }
public void testTypeListenersAreFired() { final AtomicInteger firedCount = new AtomicInteger(); final TypeListener typeListener = new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { assertEquals(new TypeLiteral<A>() {}, type); firedCount.incrementAndGet(); } }; Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindListener(onlyAbcd, typeListener); bind(A.class); } }); assertEquals(1, firedCount.get()); }
public void testAddingInterceptors() throws NoSuchMethodException { final Matcher<Object> buzz = only(C.class.getMethod("buzz")); Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindInterceptor(any(), buzz, prefixInterceptor("ka")); bindInterceptor(any(), any(), prefixInterceptor("fe")); bindListener(onlyAbcd, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.bindInterceptor(any(), prefixInterceptor("li")); encounter.bindInterceptor(buzz, prefixInterceptor("no")); } }); } }); // interceptors must be invoked in the order they're bound. C c = injector.getInstance(C.class); assertEquals("kafelinobuzz", c.buzz()); assertEquals("felibeep", c.beep()); }
public void testLookupsPostCreate() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindListener(only(TypeLiteral.get(C.class)), new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { assertNotNull(encounter.getProvider(B.class).get()); A a = new A(); encounter.getMembersInjector(A.class).injectMembers(a); assertNotNull(a.injector); } }); } }); injector.getInstance(C.class); }
/** * We had a bug where we weren't notifying of types encountered for member injection when those * types had no members to be injected. Constructed types are always injected because they always * have at least one injection point: the class constructor. */ public void testTypesWithNoInjectableMembersAreNotified() { final AtomicInteger notificationCount = new AtomicInteger(); Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindListener(onlyAbcd, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { notificationCount.incrementAndGet(); } }); bind(C.class).toInstance(new C()); } }); assertEquals(1, notificationCount.get()); }
public void testAddErrors() { try { Guice.createInjector(new AbstractModule() { @Override protected void configure() { requestInjection(new Object()); bindListener(Matchers.any(), new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.addError("There was an error on %s", type); encounter.addError(new IllegalArgumentException("whoops!")); encounter.addError(new Message("And another problem")); encounter.addError(new IllegalStateException()); } }); } }); fail(); } catch (CreationException expected) { assertContains(expected.getMessage(), "1) There was an error on java.lang.Object", "2) An exception was caught and reported. Message: whoops!", "3) And another problem", "4) An exception was caught and reported. Message: null", "4 errors"); } }
@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); } } }); } }
public void testTypeListenersAreFired() throws NoSuchMethodException { final AtomicInteger firedCount = new AtomicInteger(); final TypeListener typeListener = new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { assertEquals(new TypeLiteral<A>() {}, type); firedCount.incrementAndGet(); } }; Guice.createInjector(new AbstractModule() { protected void configure() { bindListener(onlyAbcd, typeListener); bind(A.class); } }); assertEquals(1, firedCount.get()); }
public void testAddingInterceptors() throws NoSuchMethodException { final Matcher<Object> buzz = only(C.class.getMethod("buzz")); Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { bindInterceptor(any(), buzz, prefixInterceptor("ka")); bindInterceptor(any(), any(), prefixInterceptor("fe")); bindListener(onlyAbcd, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.bindInterceptor(any(), prefixInterceptor("li")); encounter.bindInterceptor(buzz, prefixInterceptor("no")); } }); } }); // interceptors must be invoked in the order they're bound. C c = injector.getInstance(C.class); assertEquals("kafelinobuzz", c.buzz()); assertEquals("felibeep", c.beep()); }
public void testLookupsPostCreate() { Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { bindListener(only(TypeLiteral.get(C.class)), new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { assertNotNull(encounter.getProvider(B.class).get()); A a = new A(); encounter.getMembersInjector(A.class).injectMembers(a); assertNotNull(a.injector); } }); } }); injector.getInstance(C.class); }
/** * We had a bug where we weren't notifying of types encountered for member injection when those * types had no members to be injected. Constructed types are always injected because they always * have at least one injection point: the class constructor. */ public void testTypesWithNoInjectableMembersAreNotified() { final AtomicInteger notificationCount = new AtomicInteger(); Guice.createInjector(new AbstractModule() { protected void configure() { bindListener(onlyAbcd, new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { notificationCount.incrementAndGet(); } }); bind(C.class).toInstance(new C()); } }); assertEquals(1, notificationCount.get()); }
public void testAddErrors() { try { Guice.createInjector(new AbstractModule() { protected void configure() { requestInjection(new Object()); bindListener(Matchers.any(), new TypeListener() { public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.addError("There was an error on %s", type); encounter.addError(new IllegalArgumentException("whoops!")); encounter.addError(new Message("And another problem")); encounter.addError(new IllegalStateException()); } }); } }); fail(); } catch (CreationException expected) { assertContains(expected.getMessage(), "1) There was an error on java.lang.Object", "2) An exception was caught and reported. Message: whoops!", "3) And another problem", "4) An exception was caught and reported. Message: null", "4 errors"); } }
@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); } }); } } }); }
private void addPostConstructInterceptor(GuiceModuleConfiguration guiceModuleConfiguration) { HasPostConstructMethod hasPostConstruct = new HasPostConstructMethod(guiceModuleConfiguration.getClassWithDisabledPostConstruct()); final PostConstructInvoker postConstructInvoker = new PostConstructInvoker(); bindListener(hasPostConstruct, new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { encounter.register(postConstructInvoker); } }); }
/** * TypeListener implements * * @param type * @param encounter * @param <I> */ @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { Class clazz = type.getRawType(); if (clazz == null) return; for (Field field : clazz.getDeclaredFields()) { if (field.isAnnotationPresent(JbootrpcService.class)) { encounter.register(new JbootrpcMembersInjector(field)); } } }
@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 protected void configure() { bindListener(Matchers.any(), new TypeListener() { @Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { PluginResourceHandler.inst().getForClass(type.getRawType()); } }); }
@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 <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { final Class<?> cls = type.getRawType(); if (isEndpoint(cls)) { addEndpoint(cls); } }
@Override public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) { Class<?> clazz = typeLiteral.getRawType(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { if (field.getType() == Log.class && field.isAnnotationPresent(Logger.class)) { typeEncounter.register(new LoggerMembersInjector<T>(field)); } } clazz = clazz.getSuperclass(); } }
/** * Automatically called by Guice during injection of any object. Checks if the injected object's type is * annotated as PageObject. If yes, it will register PageObjectInjectorListener in the TypeEncounter, so * that PageObjectInjectorListener will be able to perform custom injections. */ @Override public <I> void hear(TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) { if (typeLiteral.getRawType().isAnnotationPresent(PageObject.class)) { typeEncounter.register(new PageObjectInjectorListener(typeEncounter)); } }
/** * Constructor of the listener. Initializes all the fields. * * @param typeEncounter Type encounter instance, provided by Guice. */ public PageObjectInjectorListener(TypeEncounter<?> typeEncounter) { this.webDriverProvider = typeEncounter.getProvider(WebDriver.class); this.locatorStackProvider = typeEncounter.getProvider(ContextStack.class); this.registry = typeEncounter.getProvider(FieldProviderRegistry.class); this.frameMap = typeEncounter.getProvider(FrameMap.class); this.bobcatWebElementFactoryProvider = typeEncounter.getProvider(BobcatWebElementFactory.class); }
@Override public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { Class<? super I> rawType = type.getRawType(); if (isApplicable(rawType)) { encounter.register(new TestClassInjectionListener(encounter)); } }
/** * @see com.google.inject.spi.TypeListener#hear(com.google.inject.TypeLiteral, * com.google.inject.spi.TypeEncounter) */ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { Class<?> clazz = type.getRawType(); for (Method method : clazz.getMethods()) { QuartzSchedule quartzSchedule = method.getAnnotation(QuartzSchedule.class); if (quartzSchedule != null) { logger.debug("Scheduling methods in class {}.", type.getRawType().getName()); encounter.register(new QuartzScheduleInjectionListener<I>(scheduleHelper)); } } }
@Override public <I> void hear(TypeLiteral<I> literal, TypeEncounter<I> encounter) { Class<? super I> type = literal.getRawType(); for (Method method : type.getMethods()) { ParameterizedTimed annotation = method.getAnnotation(ParameterizedTimed.class); if (annotation == null) { continue; } String metricType = annotation.type(); if(metricType == null || metricType.isEmpty()) { metricType = type.getSimpleName().replaceAll("\\$$", ""); } String metricName = annotation.name(); if(metricName == null || metricName.isEmpty()) { metricName = method.getName(); } String metric = MetricRegistry.name(_group, metricType, metricName); final Timer timer = _metricRegistry.timer(metric); encounter.bindInterceptor(Matchers.only(method), new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { Timer.Context time = timer.time(); try { return invocation.proceed(); } finally { time.stop(); } } }); } }
/** * Returns a new encounter provider for the given key */ public static <T> EncounterProvider<T> encounterProvider( final Key<? extends T> key) { return new EncounterProvider<T>() { public Provider<? extends T> get(TypeEncounter<?> encounter) { return encounter.getProvider(key); } }; }
/** * Returns a new encounter provider for the given type */ public static <T> EncounterProvider<T> encounterProvider( final Class<? extends T> type) { return new EncounterProvider<T>() { public Provider<? extends T> get(TypeEncounter<?> encounter) { return encounter.getProvider(type); } }; }