Arrays in TypeLiterals

6 views
Skip to first unread message

limpb...@gmail.com

unread,
May 26, 2008, 3:57:12 PM5/26/08
to google-guice-dev
Hey guys --

To my great disappointment, this test fails:
assertEquals(TypeLiteral.get(String[].class), new
TypeLiteral<String[]>() {});

The cause is that there's two different ways to
represent a String[] using Java's Types API:
- as a Class instance whose isArray() returns true
- as a GenericArrayType

The two are functionally equal but aren't equal
according to their respective .equals() methods.
I suspect this problem may have caused some of
our users frustration, especially for code like this:
bind(String[].class).to(...)

To resolve this, I'm going to change TypeLiteral
to prefer GenericArrayType. If ever it's constructed
with the other form, it will substitute in the equivalent
GenericArrayType value.

The additional benefit is that the GenericArrayType
has a prettier toString() value: [LString vs. String[]

If you see any potential problems with this (or places
where similar problems exist), please let me know.

Cheers,
Jesse

Reply all
Reply to author
Forward
0 new messages