Java Class literal constraint
Simply said; no you can't bound the Class
literal assignment as B<T>
is a generic type, i.e. you can have:
Class<? extends A<? extends Z>> clazzb = B<X>.class; // illegal statement
// OR
Class<? extends A<? extends Z>> clazzb = B<W>.class; // assuming W is a sub-type of X
as there is only one class literal (in terms of byte code (runtime) and in terms of language constraints (compile-time)): B.class
And since the compiler won't be able to know of which type the type parameter T
would be for the B
class, it won't allow such an assignment.
Legal assignment
Below assignment statement would be legal (compiles fine):
Class<? extends A> clazzb = B.class;
but with warnings and that would perfectly complies with the Generic Class and Type Parameters Java Language Specification.
I've seen many times a Java specific-pattern where developers work-around the compiler type-check with a generic utility method inferring its return type to match the assigned reference type:
<!-- language : lang-java -->
@SuppressWarnings("unchecked")
public static <T> Class<T> inferType(Class<?> clazz) {
return (Class<T>) clazz;
}
Then you would be able to perform what used to be forbidden:
Class<? extends A<? extends Z>> clazzb = generify(B.class);
Note that this should be nothing but a non-recommended workaround. Just to let you be more sure, here down a quite confusing statement that compiles just fine:
Class<C> clazzb = generify(A.class);
At first glance, that should not work, but it will as you are assigning a class literal to a class reference after all.