protected void bind(BindDescriptor bindDescriptor, BindType bindType) { switch (bindType) { case SIMPLE: AnnotatedBindingBuilder<?> builder = null; if (bindDescriptor.getBind() != null) { builder = bind(bindDescriptor.getBind()); if (bindDescriptor.getTo() != null) { builder.to(bindDescriptor.getTo()); } if (bindDescriptor.getIn() != null) { builder.in(bindDescriptor.getIn()); } } break; case MOCK: bindToMock(bindDescriptor); break; case SPY: bindToSpy(bindDescriptor); break; } }
@SuppressWarnings("unchecked") @Override protected void configure() { install(new CloseableModule()); install(new Jsr250Module()); install(new ParserModule()); install(new RxAccessorModule()); install(new RxBukkitSchedulerModule()); install(new ParserModule()); bind(JPl.class).toInstance(plugin); bind(Plugin.class).toInstance(plugin); ((AnnotatedBindingBuilder) bind(plugin.getClass())).toInstance(plugin); install(binder -> plugin.configure(binder::install)); }
private static List<Module> newModuleList(final Application app, final Class<? extends Application> appClass, Module... guiceModules) { List<Module> lst = new LinkedList<>(); lst.addAll(Arrays.asList(guiceModules)); lst.add(new AbstractModule() { @Override public void configure() { AnnotatedBindingBuilder<Application> builder = bind(Application.class); if (app != null) { builder.toInstance(app); } else { builder.to(appClass); } } }); return lst; }
@SuppressWarnings({"rawtypes", "unchecked"}) private Block bind(final String name, final Class type) { return unit -> { Object value = unit.get(type); AnnotatedBindingBuilder abbce = unit.mock(AnnotatedBindingBuilder.class); abbce.toInstance(value); Binder binder = unit.get(Binder.class); if (name == null) { expect(binder.bind(Key.get(type))).andReturn(abbce); } else { expect(binder.bind(Key.get(type, Names.named(name)))).andReturn(abbce); } }; }
private MockUnit.Block sessionStore(SessionStore<WebContext> ss) { return unit -> { org.pac4j.core.config.Config config = unit.get(org.pac4j.core.config.Config.class); expect(config.getSessionStore()).andReturn(ss); if (ss != null) { config.setSessionStore(ss); } AnnotatedBindingBuilder aab = unit.mock(AnnotatedBindingBuilder.class); if (ss == null) { expect(aab.to(Pac4jSessionStore.class)).andReturn(aab); expect(aab.to(Pac4jSessionStore.class)).andReturn(aab); } else { aab.toInstance(ss); aab.toInstance(ss); } Binder binder = unit.get(Binder.class); expect(binder.bind(SSTORE)).andReturn(aab); expect(binder.bind(SessionStore.class)).andReturn(aab); }; }
@SuppressWarnings("unchecked") private Block serviceKey(final String db) { return unit -> { Env env = unit.get(Env.class); ServiceKey skey = new Env.ServiceKey(); expect(env.serviceKey()).andReturn(skey).times(2); AnnotatedBindingBuilder<DataSource> binding = unit.mock(AnnotatedBindingBuilder.class); binding.toInstance(unit.get(HikariDataSource.class)); binding.toInstance(unit.get(HikariDataSource.class)); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(DataSource.class))).andReturn(binding); expect(binder.bind(Key.get(DataSource.class, Names.named(db)))).andReturn(binding); }; }
@SuppressWarnings("unchecked") private Block serviceKey(final String db, String dbtype) { return unit -> { Env env = unit.get(Env.class); expect(env.serviceKey()).andReturn(new Env.ServiceKey()); AnnotatedBindingBuilder<DataSource> binding = unit.mock(AnnotatedBindingBuilder.class); binding.toInstance(unit.get(HikariDataSource.class)); binding.toInstance(unit.get(HikariDataSource.class)); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(DataSource.class))).andReturn(binding); expect(binder.bind(Key.get(DataSource.class, Names.named(db)))).andReturn(binding); expect(env.set(Key.get(DataSource.class), unit.get(HikariDataSource.class))).andReturn(env); expect(env.set(Key.get(DataSource.class, Names.named(db)), unit.get(HikariDataSource.class))) .andReturn(env); if (dbtype != null) { expect(env.set(Key.get(String.class, Names.named(db + ".dbtype")), dbtype)).andReturn(env); } }; }
public <T> AnnotatedBindingBuilder<T> bind(final Class<T> clazz) { if (isIgnoreClass(clazz)) { return new NullAnnotatedBindingBuilder<T>(); } else { return binder.bind(clazz); } }
protected <T> TrackerProvider<T> bindTracker(java.lang.reflect.Type typeParam, Annotation annotatedWith, String extensionPoint, String beanParameter) { ParameterizedType type = Types.newParameterizedType(PluginTracker.class, typeParam); TrackerProvider<T> trackerProvider = new TrackerProvider<T>(getPluginId(), extensionPoint, beanParameter); @SuppressWarnings("unchecked") TypeLiteral<PluginTracker<T>> typeLiteral = (TypeLiteral<PluginTracker<T>>) TypeLiteral.get(type); LinkedBindingBuilder<PluginTracker<T>> bindingBuilder = bind(typeLiteral); if( annotatedWith != null ) { bindingBuilder = ((AnnotatedBindingBuilder<PluginTracker<T>>) bindingBuilder).annotatedWith(annotatedWith); } bindingBuilder.toProvider(trackerProvider).in(Scopes.SINGLETON); return trackerProvider; }
protected com.google.inject.Module createModule(final Grammar grammar) { final com.google.inject.Module _function = (Binder binder) -> { binder.<Grammar>bind(Grammar.class).toInstance(grammar); binder.<Naming>bind(Naming.class).toInstance(this.getNaming()); AnnotatedBindingBuilder<IGrammarAccess> _bind = binder.<IGrammarAccess>bind(IGrammarAccess.class); Xtend2GeneratorFragment.GenericGrammarAccess _genericGrammarAccess = new Xtend2GeneratorFragment.GenericGrammarAccess(grammar); _bind.toInstance(_genericGrammarAccess); this.addLocalBindings(binder); }; return _function; }
public void configureIEncodingProvider(final Binder binder) { AnnotatedBindingBuilder<IEncodingProvider> _bind = binder.<IEncodingProvider>bind(IEncodingProvider.class); IEncodingProvider.Runtime _runtime = new IEncodingProvider.Runtime(); final Procedure1<IEncodingProvider.Runtime> _function = (IEncodingProvider.Runtime it) -> { it.setDefaultEncoding(this.code.getEncoding()); }; IEncodingProvider.Runtime _doubleArrow = ObjectExtensions.<IEncodingProvider.Runtime>operator_doubleArrow(_runtime, _function); _bind.toInstance(_doubleArrow); }
@Override protected void bindWebSecurityManager(final AnnotatedBindingBuilder<? super WebSecurityManager> bind) { bind(NexusWebSecurityManager.class).asEagerSingleton(); // bind RealmSecurityManager and WebSecurityManager to _same_ component bind(RealmSecurityManager.class).to(NexusWebSecurityManager.class); bind.to(NexusWebSecurityManager.class); // bindings used by external modules expose(RealmSecurityManager.class); expose(WebSecurityManager.class); }
@Override protected void bindSessionManager(final AnnotatedBindingBuilder<SessionManager> bind) { // use native web session management instead of delegating to servlet container // workaround for NEXUS-5727, see NexusDefaultWebSessionManager javadoc for clues bind.to(NexusWebSessionManager.class).asEagerSingleton(); // this is a PrivateModule, so explicitly binding the NexusDefaultSessionManager class bind(NexusWebSessionManager.class); }
/** * @see com.google.inject.Binder#bind(Class) */ @Override protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) { if (Service.class.isAssignableFrom(clazz)) { Class<? extends Service> serviceClass = (Class<? extends Service>) clazz; services.register(serviceClass); } return binder().bind(clazz); }
@Override protected void bindWebSecurityManager(AnnotatedBindingBuilder<? super WebSecurityManager> bind) { try { // bind.toConstructor(PermissiveSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton(); bind.toConstructor(PermissiveSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton(); } catch (NoSuchMethodException e) { throw new ConfigurationException("This really shouldn't happen. Either something has changed in Shiro, or there's a bug in ShiroModule.", e); } }
LazyBindingBuilder( Binder binder, TypeLiteral<T> type, AnnotatedBindingBuilder<Lazy<T>> lazyBinding, PotentialAnnotation potentialAnnotation) { this.binder = binder; this.type = type; this.lazyBinding = lazyBinding; this.potentialAnnotation = potentialAnnotation; }
@SuppressWarnings("unchecked") @Override public <DE extends DomainEntity, R extends Repository<DE>> R getRepository(final Class<DE> entityClass) { if (fRepos.containsKey(entityClass)) { return (R) fRepos.get(entityClass); } Injector child = fInjector.createChildInjector(new AbstractModule() { @SuppressWarnings("rawtypes") @Override protected void configure() { bind(new TypeLiteral<Class>(){}).toInstance(entityClass); } }); final R repo = (R) child.getInstance(fRepoClass); if (BinaryEntity.class.isAssignableFrom(entityClass)) { child = child.createChildInjector(new AbstractModule() { @Override protected void configure() { ((AnnotatedBindingBuilder<Repository<DE>>) bind(Key.get(new DummyParameterizedType(Repository.class, entityClass)))).toInstance(repo); } }); R binaryRepo = (R) child.getInstance(BinaryEntityRepository.class); fRepos.put(entityClass, binaryRepo); return binaryRepo; } fRepos.put(entityClass, repo); return repo; }
private LinkedBindingBuilder<?> verifyBindingAnnotations( Field field, AnnotatedBindingBuilder<?> annotatedBinder) { LinkedBindingBuilder<?> binderRet = annotatedBinder; for (Annotation annotation : field.getAnnotations()) { Class<? extends Annotation> annotationType = annotation.annotationType(); if (Annotations.isBindingAnnotation(annotationType)) { // not returning here ensures that annotatedWith will be called multiple times if this field // has multiple BindingAnnotations, relying on the binder to throw an error in this case. binderRet = annotatedBinder.annotatedWith(annotation); } } return binderRet; }
@SuppressWarnings("unchecked") @Test public void configure() { Binder binder = createNiceMock(Binder.class); AnnotatedBindingBuilder<LangFactory> bindLangFactory = createMock(AnnotatedBindingBuilder.class); AnnotatedBindingBuilder<ILinkingDiagnosticMessageProvider.Extended> bindLinkingDMP = createMock(AnnotatedBindingBuilder.class); AnnotatedBindingBuilder<ILaunchManager> bindLaunchManager = createMock(AnnotatedBindingBuilder.class); final ILaunchManager launchManager = createMock(ILaunchManager.class); final AnnotatedBindingBuilder<Console> bindConsole = createMock(AnnotatedBindingBuilder.class); final Console console = createMock(Console.class); expect(binder.bind(LangFactory.class)).andReturn(bindLangFactory); expect(binder.bind(ILinkingDiagnosticMessageProvider.Extended.class)).andReturn(bindLinkingDMP); expect(binder.bind(ILaunchManager.class)).andReturn(bindLaunchManager); expect(binder.bind(Console.class)).andReturn(bindConsole); bindLangFactory.toInstance(LangFactory.eINSTANCE); expect(bindLinkingDMP.to(Antlr4MissingReferenceMessageProvider.class)).andReturn(null); bindLaunchManager.toInstance(launchManager); bindConsole.toInstance(console); Object[] mocks = {binder, bindLangFactory, bindLinkingDMP, launchManager, console }; replay(mocks); new Antlr4RuntimeModule() { @Override protected ILaunchManager getLaunchManager() { return launchManager; } @Override protected Console getConsole() { return console; } }.configure(binder); verify(mocks); }
private MockUnit.Block profile(Class profileClass) { return unit -> { AnnotatedBindingBuilder abb = unit.mock(AnnotatedBindingBuilder.class); expect(abb.toProvider(isA(Provider.class))).andReturn(abb); abb.in(RequestScoped.class); Binder binder = unit.get(Binder.class); expect(binder.bind(profileClass)).andReturn(abb); }; }
@SuppressWarnings({"rawtypes", "unchecked" }) @Test public void withServiceWithoutInterface() throws Exception { AmazonWebServiceClient aws = new AmazonWebServiceClient(new ClientConfiguration()) { @Override public String getServiceName() { return "s3"; } }; new MockUnit(Env.class, Config.class, Binder.class) .expect(unit -> { Config config = unit.get(Config.class); expect(config.hasPath("aws.s3.accessKey")).andReturn(false); expect(config.hasPath("aws.s3.secretKey")).andReturn(false); expect(config.hasPath("aws.s3.sessionToken")).andReturn(false); expect(config.hasPath("aws.sessionToken")).andReturn(false); expect(config.getString("aws.accessKey")).andReturn("accessKey"); expect(config.getString("aws.secretKey")).andReturn("secretKey"); }) .expect(unit -> { AnnotatedBindingBuilder abbAWSC = unit.mock(AnnotatedBindingBuilder.class); abbAWSC.toInstance(aws); Binder binder = unit.get(Binder.class); expect(binder.bind(aws.getClass())).andReturn(abbAWSC); }) .expect(unit -> { Env env = unit.get(Env.class); expect(env.onStop(isA(AwsShutdownSupport.class))).andReturn(env); }) .run(unit -> { new Aws() .with(creds -> aws) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void withAccessor() throws Exception { Object value = new Object(); new MockUnit(Env.class, Config.class, Binder.class, Cluster.class, Cluster.Builder.class, Configuration.class, Session.class) .expect(clusterBuilder) .expect(serviceKey(new Env.ServiceKey())) .expect(contactPoints("localhost")) .expect(port(9042)) .expect(codecRegistry) .expect(bind("beers", Cluster.class)) .expect(bind(null, Cluster.class)) .expect(bind("beers", Session.class)) .expect(bind(null, Session.class)) .expect(connect("beers")) .expect(mapper) .expect(bind("beers", MappingManager.class)) .expect(bind(null, MappingManager.class)) .expect(datastore) .expect(bind("beers", Datastore.class)) .expect(bind(null, Datastore.class)) .expect(routeMapper).expect(onStop) .expect(unit -> { MappingManager manager = unit.get(MappingManager.class); expect(manager.createAccessor(Object.class)).andReturn(value); AnnotatedBindingBuilder<Object> abb = unit.mock(AnnotatedBindingBuilder.class); abb.toInstance(value); Binder binder = unit.get(Binder.class); expect(binder.bind(Object.class)).andReturn(abb); }) .run(unit -> { new Cassandra("cassandra://localhost/beers") .accesor(Object.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void scanAnnotation() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, Router.class) .expect(ns("app.ns")) .expect(runtimeProcessors) .expect(scanResult("app.ns", "javax.inject", "com.google.inject.name")) .expect(routes) .expect(annotations(Path.class)) .expect(subClassesOf(Jooby.class)) .expect(appclass(ScannerTest.class.getName())) .expect(annotations(javax.inject.Named.class, NamedFoo.class.getName())) .expect(annotations(com.google.inject.name.Named.class)) .expect(unit -> { Binder binder = unit.get(Binder.class); AnnotatedBindingBuilder<NamedFoo> abb = unit.mock(AnnotatedBindingBuilder.class); abb.asEagerSingleton(); expect(binder.bind(NamedFoo.class)).andReturn(abb); Env env = unit.get(Env.class); expect(env.lifeCycle(NamedFoo.class)).andReturn(env); }) .run(unit -> { new Scanner() .scan(javax.inject.Named.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void scanSingleton() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, Router.class) .expect(ns("app.ns")) .expect(runtimeProcessors) .expect(scanResult("app.ns", "javax.inject", "com.google.inject")) .expect(routes) .expect(annotations(Path.class)) .expect(subClassesOf(Jooby.class)) .expect(appclass(ScannerTest.class.getName())) .expect(annotations(javax.inject.Singleton.class, SingletonFoo.class.getName())) .expect(annotations(com.google.inject.Singleton.class)) .expect(unit -> { Binder binder = unit.get(Binder.class); AnnotatedBindingBuilder<SingletonFoo> abb = unit.mock(AnnotatedBindingBuilder.class); abb.asEagerSingleton(); expect(binder.bind(SingletonFoo.class)).andReturn(abb); Env env = unit.get(Env.class); expect(env.lifeCycle(SingletonFoo.class)).andReturn(env); }) .run(unit -> { new Scanner() .scan(javax.inject.Singleton.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void scanImplements() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, Router.class) .expect(ns("app.ns")) .expect(runtimeProcessors) .expect(scanResult("app.ns")) .expect(routes) .expect(annotations(Path.class)) .expect(subClassesOf(Jooby.class)) .expect(appclass(ScannerTest.class.getName())) .expect(implementing(IFoo.class, FooImpl.class.getName())) .expect(unit -> { Binder binder = unit.get(Binder.class); AnnotatedBindingBuilder<FooImpl> abb = unit.mock(AnnotatedBindingBuilder.class); abb.asEagerSingleton(); expect(binder.bind(FooImpl.class)).andReturn(abb); Env env = unit.get(Env.class); expect(env.lifeCycle(FooImpl.class)).andReturn(env); }) .run(unit -> { new Scanner() .scan(IFoo.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void scanSubclass() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, Router.class) .expect(ns("app.ns")) .expect(runtimeProcessors) .expect(scanResult("app.ns")) .expect(routes) .expect(annotations(Path.class)) .expect(subClassesOf(Jooby.class)) .expect(appclass(ScannerTest.class.getName())) .expect(subClassesOf(AbsFoo.class, FooSub.class.getName())) .expect(unit -> { Binder binder = unit.get(Binder.class); AnnotatedBindingBuilder<FooSub> abb = unit.mock(AnnotatedBindingBuilder.class); abb.asEagerSingleton(); expect(binder.bind(FooSub.class)).andReturn(abb); Env env = unit.get(Env.class); expect(env.lifeCycle(FooSub.class)).andReturn(env); }) .run(unit -> { new Scanner() .scan(AbsFoo.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void defaults() throws Exception { new MockUnit(Env.class, Config.class, Binder.class) .expect(unit -> { Config ehcache = unit.get(Config.class); expect(ehcache.getConfig("ehcache")).andReturn(empty()); }) .expect(unit -> { Binder binder = unit.get(Binder.class); AnnotatedBindingBuilder<CacheManager> abbCM = unit.mock(AnnotatedBindingBuilder.class); abbCM.toInstance(isA(CacheManager.class)); expect(binder.bind(CacheManager.class)).andReturn(abbCM); }) .expect(unit -> { Env env = unit.get(Env.class); expect(env.onStop(isA(Throwing.Runnable.class))).andReturn(env); }) .run(unit -> { new Eh() .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
private MockUnit.Block consulIsBuiltAndBound(String name, Config config) { return unit -> { Config consulConfig = consulConfigWithFallback(name, config); Consul consul = unit.get(Consul.class); Consul.Builder consulBuilder = unit.mock(Consul.Builder.class); expect(consulBuilder.withUrl(consulConfig.getString("url"))).andReturn(consulBuilder); expect(consulBuilder.build()).andReturn(consul); unit.mockStatic(Consul.class); expect(Consul.builder()).andReturn(consulBuilder); Env env = unit.get(Env.class); expect(env.onStop(isA(Throwing.Runnable.class))).andReturn(env); //noinspection unchecked AnnotatedBindingBuilder<Consul> consulABB = unit.mock(AnnotatedBindingBuilder.class); consulABB.toInstance(consul); consulABB.toInstance(consul); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(Consul.class))).andReturn(consulABB); expect(binder.bind(Key.get(Consul.class, Names.named(name)))).andReturn(consulABB); }; }
protected <T extends BaseComponent> AnnotatedBindingBuilder<ComponentRenderer<? super T>> bindComponent(Class<T> componentType) { return bind(ComponentRenderers.rendererType(componentType)); }
BindAndExposeBindingBuilder(final AnnotatedBindingBuilder<T> bind, final AnnotatedElementBuilder expose) { this.bind = bind; this.expose = expose; }
@NonNull @Override public AnnotatedBindingBuilder<T> builder() { return this.bind; }
@Override default <T> AnnotatedBindingBuilder<T> bind(final TypeLiteral<T> typeLiteral) { return this.binder().bind(typeLiteral); }
@Override default <T> AnnotatedBindingBuilder<T> bind(final Class<T> type) { return this.binder().bind(type); }
@Override public <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { checkForFrameworkTypes(typeLiteral.getRawType()); return new AnnotatedBindingBuilderImpl<T>( delegate.bind(typeLiteral.getTypeToken())); }
@Override public <T> AnnotatedBindingBuilder<T> bind(Class<T> type) { checkForFrameworkTypes(type); return new AnnotatedBindingBuilderImpl<T>(delegate.bind(type)); }
/** * @see Binder#bind(TypeLiteral) */ protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { return binder().bind(typeLiteral); }
/** * @see Binder#bind(Class) */ protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) { return binder().bind(clazz); }
public AnnotatedBindingBuilderAdapter(AnnotatedBindingBuilder<T> annotatedBindingBuilder) { this.annotatedBindingBuilder = annotatedBindingBuilder; }