Re: requireBinding for a BindingAnotation possible?

2016-09-19 Thread Steve Storck
Thank you, Thomas.  That worked well and immediately.

On Monday, September 19, 2016 at 9:29:50 AM UTC-4, Thomas Broyer wrote:
>
> You need to require the Map<String,String>, not the raw Map class.
>
>  requireBinding(Key.get(new TypeLiteral<Map<String,String>>(){}, MyMap))
>
> On Monday, September 19, 2016 at 3:25:03 PM UTC+2, Steve Storck wrote:
>>
>> Some of my modules require a binding from another module.  I want to set 
>> up a requireBinding in the dependent modules' configure() method, but it 
>> doesn't seem to be working for me.  Here is an example of what I am doing:
>>
>> @BindingAnnotation
>> @Target([FIELD, PARAMETER, METHOD])
>> @Retention(RUNTIME)
>> @interface MyMap {
>> }
>>
>> class ModuleA extends AbstractModule {
>> protected void configure() {
>> }
>>
>> @Provides
>> @Singleton
>> @MyMap
>> static def Map<String, String> myMap() {
>> ImmutableMap.copyOf(
>> [A: 'val1',
>>  B: 'val2']
>> )
>> }
>> }
>>
>> class ModuleB extends AbstractModule {
>> protected void configure() {
>> requireBinding(Key.get(Map, MyMap))
>> bind Foo
>> }
>> }
>>
>> class ModuleIT extends Specification {
>> def 'Test module configuration'() {
>> setup:
>> def injector = Guice.createInjector new ModuleA(), new ModuleB()
>>
>> when:
>> def result = injector.getInstance Foo
>>
>> then:
>> result instanceof Foo
>> result.someMap instanceof Map<String, String>
>> }
>> }
>>
>> This results in an error when createInjector is called:
>> com.google.inject.CreationException: Unable to create injector, see the 
>> following errors:
>> 1) No implementation for java.util.Map annotated with interface 
>> a.b.c.MyMap was bound.
>>
>> On the other hand, it works fine if I don't specify requreBinding.  
>> However, since this dependent module has no notion of the map that is 
>> bound, I want to ensure that the binding is present when the dependent 
>> module is created.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/8a9ec818-e437-4d90-baaa-0154b1e08ead%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


requireBinding for a BindingAnotation possible?

2016-09-19 Thread Steve Storck
Some of my modules require a binding from another module.  I want to set up 
a requireBinding in the dependent modules' configure() method, but it 
doesn't seem to be working for me.  Here is an example of what I am doing:

@BindingAnnotation
@Target([FIELD, PARAMETER, METHOD])
@Retention(RUNTIME)
@interface MyMap {
}

class ModuleA extends AbstractModule {
protected void configure() {
}

@Provides
@Singleton
@MyMap
static def Map myMap() {
ImmutableMap.copyOf(
[A: 'val1',
 B: 'val2']
)
}
}

class ModuleB extends AbstractModule {
protected void configure() {
requireBinding(Key.get(Map, MyMap))
bind Foo
}
}

class ModuleIT extends Specification {
def 'Test module configuration'() {
setup:
def injector = Guice.createInjector new ModuleA(), new ModuleB()

when:
def result = injector.getInstance Foo

then:
result instanceof Foo
result.someMap instanceof Map
}
}

This results in an error when createInjector is called:
com.google.inject.CreationException: Unable to create injector, see the 
following errors:
1) No implementation for java.util.Map annotated with interface a.b.c.MyMap 
was bound.

On the other hand, it works fine if I don't specify requreBinding.  
However, since this dependent module has no notion of the map that is 
bound, I want to ensure that the binding is present when the dependent 
module is created.

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/111073cb-fa1e-4c0a-b122-5e698cc6f4ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.