Re: Wildcards...

2009-06-13 Thread Johannes Schneider

Thanks for your suggestion - I have discovered the provides methods
(annotated with @Provides) that do the same thing.
So for all you out there - just use that annotation - will save a lot of
work and the code gets a lot clearer.


Btw: Guice is great
Btw2: Plz upload to maven central


Regards,

Johannes

je...@swank.ca wrote:
 
 
 On Jun 6, 8:48 am, Johannes Schneider maili...@cedarsoft.com wrote:
 bind( ( TypeLiteralList? extends B ) TypeLiteral.get(
 Types.listOf( Types.subtypeOf( new TypeLiteralB?() {
 }.getType() ) ) ) ).toProvider( BProvider.class );
 
 Not much I can really say - creating models for complex types code-
 intensive. TypeLiteral works for some wildcards, and for the ones it
 doesn't you can either use the Types factory class, or you can do
 something reflective.
   private final List? extends B? unused = null;
   private final TypeLiteralList? extends B? type = fieldToType
 (MyClass.class, unused);
 
   private static Type fieldToType(Class? definedIn, String name) {
 return definedIn.getDeclaredField(name).getGenericType();
   }
  

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Wildcards...

2009-06-07 Thread je...@swank.ca



On Jun 6, 8:48 am, Johannes Schneider maili...@cedarsoft.com wrote:
         bind( ( TypeLiteralList? extends B ) TypeLiteral.get(
 Types.listOf( Types.subtypeOf( new TypeLiteralB?() {
         }.getType() ) ) ) ).toProvider( BProvider.class );

Not much I can really say - creating models for complex types code-
intensive. TypeLiteral works for some wildcards, and for the ones it
doesn't you can either use the Types factory class, or you can do
something reflective.
  private final List? extends B? unused = null;
  private final TypeLiteralList? extends B? type = fieldToType
(MyClass.class, unused);

  private static Type fieldToType(Class? definedIn, String name) {
return definedIn.getDeclaredField(name).getGenericType();
  }
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Wildcards...

2009-06-06 Thread Johannes Schneider

one more time:

public class GuiceListWildcardTest {
  @Test
  public void testIt() {
Injector injector = Guice.createInjector( new AbstractModule() {
  @Override
  protected void configure() {
bind( ( TypeLiteralList? extends B? ) TypeLiteral.get(
Types.listOf( Types.subtypeOf( new TypeLiteralB?() {
}.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 BT {
  }

  public static class BProvider implements ProviderList? 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( ( TypeLiteralList? extends B ) TypeLiteral.get(
 Types.listOf( Types.subtypeOf( new TypeLiteralB?() {
 }.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 BT {
   }
 
   public static class BProvider implements ProviderList? 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( ( TypeLiteralList? extends B ) TypeLiteral.get(
 Types.listOf( Types.subtypeOf( new TypeLiteralB?() {
 }.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 BT {
   }

   public static class BProvider implements ProviderList? 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
-~--~~~~--~~--~--~---