Generics: com.google.inject.Guice#createInjector

89 views
Skip to first unread message

Johannes Schneider

unread,
Aug 22, 2007, 6:01:08 AM8/22/07
to google...@googlegroups.com
Hi,

com.google.inject.Guice#createInjector has the following signature:

public static Injector createInjector(Iterable<Module> modules)


Shouldn't that be changed to

public static Injector createInjector(Iterable<? extends Module> modules)

I have a list of my modules as static reference (usefull for testing
purposes).

Collections.unmodifiableList( Arrays.asList( new ApplicationModule(),
new SecurityModule(), new ScannerModule(), new InvoiceModule() ) )

But this line returns a List of AbstractModules (or List<? extends
Module>)....

Regards,

Johannes Schneider

--
Johannes Schneider
Im Lindenwasen 15
72810 Gomaringen

Fon +49 7072 9229972
Fax +49 7072 509999
Mobil +49 178 1364488

joha...@familieschneider.info
http://www.johannes-schneider.info

Dhanji R. Prasanna

unread,
Aug 22, 2007, 6:15:36 AM8/22/07
to google...@googlegroups.com
You are correct, the signature should be changed.

However you can take advantage of Java's (IMO broken) array type system to fix this issue:

Collections.unmodifiableList( Arrays.asList( new Module[] { new ApplicationModule(),
new SecurityModule(), new ScannerModule(), new InvoiceModule() } ) )

This will return a List<Module>.

Dhanji =)

Laura Werner

unread,
Aug 22, 2007, 3:04:13 PM8/22/07
to google-guice
> Collections.unmodifiableList( Arrays.asList( new ApplicationModule(),
> new SecurityModule(), new ScannerModule(), new InvoiceModule() ) )
>
> But this line returns a List of AbstractModules (or List<? extends
> Module>)....

You can fix this with:

Collections.unmodifiableList( Arrays.<Module>asList( new ... );

Yes, the syntax is very strange, as is almost everything at the
intersection of arrays and generics. :-/

-- Laura

Johannes Schneider

unread,
Aug 22, 2007, 5:35:31 PM8/22/07
to google...@googlegroups.com
Thanks for your suggestion. But I think it is still a "bug" and should
be changed.


Johannes Schneider

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

Robbie Vanbrabant

unread,
Aug 22, 2007, 5:52:12 PM8/22/07
to google...@googlegroups.com
I've quickly created the issue for you: http://code.google.com/p/google-guice/issues/detail?id=143
 
On 8/22/07, Johannes Schneider <joha...@familieschneider.info> wrote:
Thanks for your suggestion. But I think it is still a "bug" and should
be changed.


Johannes Schneider

Laura Werner wrote:
>> Collections.unmodifiableList( Arrays.asList( new ApplicationModule(),
>> new SecurityModule(), new ScannerModule(), new InvoiceModule() ) )
>>
>> But this line returns a List of AbstractModules (or List<? extends
>> Module>)....
>
> You can fix this with:
>
> Collections.unmodifiableList( Arrays.<Module>asList( new ... );
>
> Yes, the syntax is very strange, as is almost everything at the
> intersection of arrays and generics. :-/
>
> -- Laura
>
>
>

Dhanji R. Prasanna

unread,
Aug 22, 2007, 7:01:47 PM8/22/07
to google...@googlegroups.com
On 8/23/07, Laura Werner <lwe...@gmail.com> wrote:


Collections.unmodifiableList( Arrays.<Module>asList( new ... );

Yes, the syntax is very strange, as is almost everything at the
intersection of arrays and generics. :-/

I was more referring to the idiocy in Module[] being a supertype of AbstractModule[].


Johannes Schneider

unread,
Aug 23, 2007, 4:40:31 AM8/23/07
to google...@googlegroups.com
Thanks,

I have created a patch that fixes this problem and the same problem at
two or three other locations.


Johannes Schneider

Robbie Vanbrabant wrote:
> I've quickly created the issue for you:
> http://code.google.com/p/google-guice/issues/detail?id=143
>

> On 8/22/07, *Johannes Schneider* <joha...@familieschneider.info

> <mailto:joha...@familieschneider.info>> wrote:
>
> Thanks for your suggestion. But I think it is still a "bug" and should
> be changed.
>
>
> Johannes Schneider
>
> Laura Werner wrote:
> >> Collections.unmodifiableList( Arrays.asList( new
> ApplicationModule(),
> >> new SecurityModule(), new ScannerModule(), new InvoiceModule() ) )
> >>
> >> But this line returns a List of AbstractModules (or List<? extends
> >> Module>)....
> >
> > You can fix this with:
> >
> > Collections.unmodifiableList( Arrays.<Module>asList( new ... );
> >
> > Yes, the syntax is very strange, as is almost everything at the
> > intersection of arrays and generics. :-/
> >
> > -- Laura
> >
> >
> >
> Fax +49 7072 509999
> Mobil +49 178 1364488
>

> joha...@familieschneider.info <mailto:joha...@familieschneider.info>
> http://www.johannes-schneider.info <http://www.johannes-schneider.info>


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

--
Johannes Schneider
Im Lindenwasen 15
72810 Gomaringen

Fon +49 7072 9229972

Bob Lee

unread,
Aug 23, 2007, 11:05:10 AM8/23/07
to google...@googlegroups.com
Cool! Thanks.

Bob

Reply all
Reply to author
Forward
0 new messages