Issue is, your constructor expects a Class<T>
, and T
in your code is inferred as Argument<AnyOtherClassDescendant>
.
So, you should pass a Class<Argument<AnyOtherClassDescendant>>
, and you're passing Class<Argument>
. But you can't pass that Class
instance directly, as you cannot do Argument<AnyOtherClassDescendant>.class
.
You can however, solve the issue by typecasting the class to required instance:
public ProblematicClass() {
super((Class<Argument<AnyOtherClassDescendant>>)(Class<?>)Argument.class);
}
Note, how you've to typecast Class<Argument>
first to Class<?>
, and then the resultant type to Class<Argument<AnyOtherClassDescendant>>
. There is no simple way to achieve that.
The reason behind this is, there is only a single Class
instance for all parameterized instantiation of a generic type, that is associated with the class itself. A single compilation unit of a generic type, compiles to just a single class file. I guess this is different in how C++ implements templates. There you get different machine codes for different instantiation.
So, if you execute the below code, you'll get true
as output:
List<String> strList = new ArrayList<String>();
List<Integer> intList = new ArrayList<Integer>();
boolean isSameClassInstance = strList.getClass() == intList.getClass();
System.out.println(isSameClassInstance);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…