Due to how generics are implemented in Java (type erasure), there is no Collection<MyClass>
class. Just the standard Collection
class.
Generics are effectively extra compile-time checks which only make it into the generated bytecode in a limited fashion. In particular, objects do not know with which generic parameters they were instantiated, so you can't call e.g. instanceof
at runtime to check this.
Most reflective calls (which includes use of the Class
class) overlook generics, because you wouldn't get any type safety anyway. Even if the runtime allowed you to access the Collection<MyClass>
class, its add
method would still take merely Object
since this would just be syntactic sugar for getting the raw type Collection
. Java arguably makes the correct decision here by not allowing this potentially misleading syntax, so as it make it clear that generic classes don't actually exist at runtime.