Open GoogleCodeExporter opened 9 years ago
It turned out not to be so easy, but this seems to do it, assuming an
implementation
of TypeWithArgument:
public static <T> TypeLiteral<List<T>> listOf(Class<T> parameterType) {
TypeWithArgument type = new TypeWithArgument(List.class, parameterType);
return (TypeLiteral<List<T>>) TypeLiteral.get(type);
}
Original comment by bslesinsky
on 29 Jun 2007 at 7:35
Great idea; also take a look at the FactoryMethods class I made for issue #18.
Perhaps we should put 'em there. Like: typeForListOf(String.class);
Original comment by robbie.v...@gmail.com
on 10 Aug 2007 at 11:03
Something like this?
Original comment by earwin@gmail.com
on 1 Nov 2007 at 8:26
Attachments:
Thanks for the code.
I think this is a good idea, and a good example of the kind of stuff that
should go
in the new com.google.inject.util package.
Original comment by kevin...@gmail.com
on 1 Nov 2007 at 10:44
Original comment by bslesinsky
on 18 Nov 2007 at 3:38
Let's do this. The methods I think we should make public:
newParameterizedType(Class rawType, Class... args)
newGenericArrayType(Class componentType)
What should we call the factory class? com.google.inject.util.Types?
I don't think we want to make these ones public:
toString(Type type)
equals(Type a, Type b);
hashCode(Type type);
canonicalize(Type type);
wrapPrimitives(Type type);
getRawType(Type type);
Original comment by limpbizkit
on 5 Jun 2008 at 7:39
My suggestions:
Types.newParameterizedType(Class rawType, Class...)
Types.arrayOf(Class componentType)
// convenience
Types.setOf(Class componentType)
Types.listOf(Class componentType)
Original comment by bslesinsky
on 5 Jun 2008 at 4:00
I second #7 for convenience methods. Why not supply them for all jdk collection
interfaces, as in my code above?
Also, I want this so be somehow possible:
qwe = listOf(setOf(SomeClass.class)); // yields TypeLiteral of
List<Set<SomeClass>>
Original comment by earwin@gmail.com
on 5 Jun 2008 at 4:10
Checked in the fix. The methods I decided to go with (as inspired by bslesinsky
and earwin):
- newParameterizedType
- newParameterizedTypeWithOwner
- arrayOf
- listOf
- setOf
- mapOf
- providerOf
Original comment by limpbizkit
on 10 Jun 2008 at 6:39
Can you please also add collectionOf()?
I ran into a situation where I want to bind a class under the base Collection
interface. Sometimes you want to expose only the minimal possible information
about a
collection and hide the fact that it is ordered. Also this will make the collections
support more compelte.
Also "iterableOf()" might not be a bad idea.
Original comment by Rinsvind@gmail.com
on 1 Jan 2009 at 4:46
Any types we don't support out-of-the-box you can create yourself with
Types.newParameterizedType().
For example,
Type collectionOfString = Types.newParameterizedType(Collection.class, String.class);
Type iterableOfShort = Types.newParameterizedType(Iterable.class, Short.class);
Original comment by limpbizkit
on 1 Jan 2009 at 5:39
Original issue reported on code.google.com by
bslesinsky
on 29 Jun 2007 at 5:34