Why is Collection<String>.class Illegal?
- by Peter
I am puzzled by generics. You can declare a field like:
Class<Collection<String>> clazz = ...
It seems logical that you could assign this field with:
Class<Collection<String>> clazz = Collection<String>.class;
However, this generates an error:
Syntax error on token ">", void expected after this token
So it looks like the .class operator does not work with generics. So I tried:
class A<S> {}
class B extends A<String> {}
Class<A<String>> c = B.class;
Also does not work, generates:
Type mismatch: cannot convert from Class<Test.StringCollection>
to Class<Collection<String>>
Now, I really fail to see why this should not work. I know generic types are not reified but in both cases it seems to be fully type safe without having access to runtime generic types. Anybody an idea?
Peter Kriens