下面列出了怎么用com.google.inject.Binder的API类实例代码及写法,或者点击链接到github查看源代码。
public void configure(Binder binder) {
binder.bind(IQualifiedNameConverter.class);
binder.bind(BuilderDeltaConverter.class);
binder.bind(DeltaConverter.class);
binder.bind(TypeURIHelper.class);
binder.bind(TypeResourceUnloader.class);
binder.bind(JavaChangeQueueFiller.class);
binder.bind(IEagerContribution.class).to(ListenerRegistrar.class);
binder.bind(TraceRegionSerializer.class);
binder.bind(ITraceForTypeRootProvider.class).to(TraceForTypeRootProvider.class);
binder.bind(FolderAwareTrace.class);
binder.bind(ZipFileAwareTrace.class);
binder.bind(OppositeFileOpenerContributor.class).to(ClassFileBasedOpenerContributor.class);
}
@Test
public void testBindProviders(@Mocked final Binder binder) {
underTest.configure(binder);
new Verifications() {{
binder.bind(MyProvider1.class).in(Scopes.SINGLETON);
binder.bind(MyProvider2.class).in(Scopes.SINGLETON);
}};
}
@Override
public void configure(Binder binder)
{
bindRowDecoder(binder, JsonKinesisRowDecoder.class);
bindFieldDecoder(binder, JsonKinesisFieldDecoder.class);
bindFieldDecoder(binder, RFC2822JsonKinesisFieldDecoder.class);
bindFieldDecoder(binder, ISO8601JsonKinesisFieldDecoder.class);
bindFieldDecoder(binder, SecondsSinceEpochJsonKinesisFieldDecoder.class);
bindFieldDecoder(binder, MillisecondsSinceEpochJsonKinesisFieldDecoder.class);
bindFieldDecoder(binder, CustomDateTimeJsonKinesisFieldDecoder.class);
}
@Override
public void configure(Binder binder)
{
//binder.bind(CommandExecutor.class).to(SimpleCommandExecutor.class).in(Scopes.SINGLETON);
binder.bind(CommandExecutor.class).to(DockerCommandExecutor.class).in(Scopes.SINGLETON);
binder.bind(SimpleCommandExecutor.class).in(Scopes.SINGLETON);
}
public void configureGeneratorRoot(GeneratorEntry entry, Binder binder) {
// TODO: replace binding with new to define JavaFlowConfiguration
binder.bind(IFlowConfiguration.class).to(DefaultFlowConfiguration.AllFeaturesDisabled.class);
binder.bind(IModelSequencer.class).to(ModelSequencer.class);
binder.bind(BehaviorMapping.class).to(org.yakindu.sct.model.sexec.transformation.ng.BehaviorMapping.class);
binder.bind(IExecutionFlowGenerator.class).to(JavaGenerator.class);
binder.bind(GeneratorEntry.class).toInstance(entry);
}
public <T> T getInstance(
Class<T> clazz,
Module[] overridableModules,
Module[] sealedModules) throws XadesProfileResolutionException
{
Module userBindingsModule = new Module()
{
@Override
public void configure(Binder b)
{
for (BindingAction ba : bindings)
{
ba.bind(b);
}
}
};
Module overridesModule = Modules.override(overridableModules).with(userBindingsModule);
// Concat sealed modules with overrides module
Module[] finalModules = Arrays.copyOf(sealedModules, sealedModules.length + 1);
finalModules[finalModules.length - 1] = overridesModule;
try
{
return Guice.createInjector(finalModules).getInstance(clazz);
}
catch (RuntimeException ex)
{
throw new XadesProfileResolutionException(ex.getMessage(), ex);
}
}
protected void bindAuditRepository(Binder binder) {
Class<? extends EntityAuditRepository> auditRepoImpl = AtlasRepositoryConfiguration.getAuditRepositoryImpl();
//Map EntityAuditRepository interface to configured implementation
binder.bind(EntityAuditRepository.class).to(auditRepoImpl).asEagerSingleton();
if(Service.class.isAssignableFrom(auditRepoImpl)) {
Class<? extends Service> auditRepoService = (Class<? extends Service>)auditRepoImpl;
//if it's a service, make sure that it gets properly closed at shutdown
Multibinder<Service> serviceBinder = Multibinder.newSetBinder(binder, Service.class);
serviceBinder.addBinding().to(auditRepoService);
}
}
public static <T> ScopedBindingBuilder bindMapToInstancesOf(Binder binder, TypeLiteral<Map<String, T>> type) {
return bindMapToInstancesOf(binder, type, new Function<T,String>() {
@Override
public String apply(T service) {
return Injectors.getServiceName(service);
}
});
}
/** Create the module for tests of the runtime libraries.
*
* @return the module.
*/
@SuppressWarnings("static-method")
protected Module createRuntimeTestModule() {
return new Module() {
@Override
public void configure(Binder binder) {
binder.bind(OnTheFlyJavaCompiler2.class).toProvider(JavaCompilerProvider.class).asEagerSingleton();
}
};
}
public void configureCodeMinding(Binder binder) {
try {
Class.forName("org.eclipse.jface.text.codemining.ICodeMiningProvider");
binder.bind(ICodeMiningProvider.class)
.to(DomainmodelCodeMiningProvider.class);
binder.bind(IReconcileStrategyFactory.class).annotatedWith(Names.named("codeMinding"))
.to(XtextCodeMiningReconcileStrategy.Factory.class);
} catch(ClassNotFoundException ignore) {
// no bindings if code mining is not available at runtime
}
}
public <T> void addMultibinding(final Class<T> from, final Class<? extends T> to)
{
if (null == from || null == to)
throw new NullPointerException();
this.bindings.add(new BindingAction()
{
@Override
public void bind(Binder b)
{
Multibinder multibinder = Multibinder.newSetBinder(b, from);
multibinder.addBinding().to(to);
}
});
}
@Before
public void createParentInjector() {
injector = Guice.createInjector(new Module() {
@Override
public void configure(Binder binder) {
binder.bind(CharSequence.class).to(String.class);
binder.bind(String.class).toInstance("fromParentInjector");
}
});
}
public void configureHighlightingLexer(Binder binder) {
binder.bind(org.eclipse.xtext.parser.antlr.Lexer.class)
.annotatedWith(Names.named(LexerIdeBindings.HIGHLIGHTING))
.to(com.hribol.bromium.dsl.parser.antlr.internal.InternalBromiumLexer.class);
}
@Override
public void configure(Binder binder) {
properties = tryBindProperties(binder, "org/eclipse/xtext/ui/tests/editor/contentassist/Bug347012TestLanguage.properties");
super.configure(binder);
}
public void configureIResourceDescriptions(Binder binder) {
binder.bind(IResourceDescriptions.class).to(ResourceSetBasedResourceDescriptions.class);
}
@Override
public void configure(Binder binder) {
properties = tryBindProperties(binder, "org/eclipse/xtext/common/types/xtext/ui/RefactoringTestLanguage1.properties");
super.configure(binder);
}
public void configureIResourceDescriptionsPersisted(Binder binder) {
binder.bind(IResourceDescriptions.class).annotatedWith(Names.named(ResourceDescriptionsProvider.PERSISTED_DESCRIPTIONS)).to(IBuilderState.class);
}
@Override
public void configure(Binder binder) {
properties = tryBindProperties(binder, "org/eclipse/xtext/ui/tests/editor/contentassist/Bug288760TestLanguage.properties");
super.configure(binder);
}
public void configureLanguageName(Binder binder) {
binder.bind(String.class).annotatedWith(Names.named(Constants.LANGUAGE_NAME)).toInstance("org.eclipse.xtext.common.types.xtext.ui.ContentAssistTestLanguage");
}
public void configureContentAssistLexer(Binder binder) {
binder.bind(Lexer.class)
.annotatedWith(Names.named(LexerIdeBindings.CONTENT_ASSIST))
.to(InternalN4JSLexer.class);
}
public void configureContentAssistLexerProvider(Binder binder) {
binder.bind(InternalContentAssistContextTestLanguageLexer.class).toProvider(LexerProvider.create(InternalContentAssistContextTestLanguageLexer.class));
}
public void configureLanguageName(Binder binder) {
binder.bind(String.class).annotatedWith(Names.named(Constants.LANGUAGE_NAME)).toInstance("org.eclipse.xtext.xtext.ui.ecore2xtext.Ecore2XtextTest");
}
protected void addLocalBindings(Binder binder) {
}
@Override
public void configure(Binder binder) {
properties = tryBindProperties(binder, "org/eclipse/xtext/grammarinheritance/ConcreteTestLanguage.properties");
super.configure(binder);
}
@Override
public void configure(Binder binder) {
properties = tryBindProperties(binder, "org/eclipse/xtext/xbase/testlanguages/XImportSectionTestLang.properties");
super.configure(binder);
}
/** Avoid races in internal serializer caches */
public void configureSerializerPatches(Binder binder) {
new SerializerPatchModule().configure(binder);
}
public void configureLanguageName(Binder binder) {
binder.bind(String.class).annotatedWith(Names.named(Constants.LANGUAGE_NAME)).toInstance("org.eclipse.xtext.ui.tests.quickfix.QuickfixCrossrefTestLanguage");
}
@Override
public void configure(Binder binder) {
super.configure(binder);
// bind additional implementations
}
public void configureCompareViewerTitle(Binder binder) {
binder.bind(String.class).annotatedWith(Names.named(UIBindings.COMPARE_VIEWER_TITLE)).toInstance("N4JS Compare");
}
public void configureContentAssistLexer(Binder binder) {
binder.bind(Lexer.class)
.annotatedWith(Names.named(LexerIdeBindings.CONTENT_ASSIST))
.to(InternalEnumRulesUiTestLanguageLexer.class);
}