one more time:

public class GuiceListWildcardTest {
  @Test
  public void testIt() {
    Injector injector = Guice.createInjector( new AbstractModule() {
      @Override
      protected void configure() {
        bind( ( TypeLiteral<List<? extends B<?>>> ) TypeLiteral.get(
Types.listOf( Types.subtypeOf( new TypeLiteral<B<?>>() {
        }.getType() ) ) ) ).toProvider( BProvider.class );
      }
    } );

    A a = injector.getInstance( A.class );
    assertEquals( a.bs.size(), 2 );
  }

  public static class A {
    private final List<? extends B<?>> bs;

    @Inject
    public A( @NotNull List<? extends B<?>> bs ) {
      this.bs = bs;
    }
  }

  public static class B<T> {
  }

  public static class BProvider implements Provider<List<? extends B<?>>> {
    @Override
    public List<? extends B<?>> get() {
      return Arrays.<B<?>>asList( new B(), new B() );
    }
  }
}




Johannes Schneider wrote:
> Improved a little (forgot wildcard at provider):
> 
> public class GuiceListWildcardTest {
>   @Test
>   public void testIt() {
>     Injector injector = Guice.createInjector( new AbstractModule() {
>       @Override
>       protected void configure() {
>         bind( ( TypeLiteral<List<? extends B>> ) TypeLiteral.get(
> Types.listOf( Types.subtypeOf( new TypeLiteral<B<?>>() {
>         }.getType() ) ) ) ).toProvider( BProvider.class );
>       }
>     } );
> 
>     A a = injector.getInstance( A.class );
>     assertEquals( a.bs.size(), 2 );
>   }
> 
>   public static class A {
>     private final List<? extends B<?>> bs;
> 
>     @Inject
>     public A( @NotNull List<? extends B<?>> bs ) {
>       this.bs = bs;
>     }
>   }
> 
>   public static class B<T> {
>   }
> 
>   public static class BProvider implements Provider<List<? extends B<?>>> {
>     @Override
>     public List<? extends B<?>> get() {
>       return Arrays.<B<?>>asList( new B(), new B() );
>     }
>   }
> }
> 
> 
> 
> 
> 
> Johannes Schneider wrote:
>> Hi,
>>
>> I think someone at Google really understands Generics ;-).
>> At the moment I try to understand how Guice manges those things.
>>
>> Therefore I have created a small sample that works. But I would like to
>> have any feedback how that should/could be solved better:
>>
>>
>> Regards,
>>
>> Johannes
>>
>>
>> public class GuiceListWildcardTest {
>>   @Test
>>   public void testIt() {
>>     Injector injector = Guice.createInjector( new AbstractModule() {
>>       @Override
>>       protected void configure() {
>>         bind( ( TypeLiteral<List<? extends B>> ) TypeLiteral.get(
>> Types.listOf( Types.subtypeOf( new TypeLiteral<B<?>>() {
>>         }.getType() ) ) ) ).toProvider( BProvider.class );
>>       }
>>     } );
>>
>>     A a = injector.getInstance( A.class );
>>     assertEquals( a.bs.size(), 2 );
>>   }
>>
>>   public static class A {
>>     private final List<? extends B<?>> bs;
>>
>>     @Inject
>>     public A( @NotNull List<? extends B<?>> bs ) {
>>       this.bs = bs;
>>     }
>>   }
>>
>>   public static class B<T> {
>>   }
>>
>>   public static class BProvider implements Provider<List<? extends B>> {
>>     @Override
>>     public List<? extends B> get() {
>>       return Arrays.asList( new B(), new B() );
>>     }
>>   }
>> }
>>
>>
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to