Search code examples
javagenericstype-parameter

Java type parameterized Class argument


JPA has a method of the following signature:

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

How do I invoke this method when my resultClass type is, itself, type parameterized?

For example:

public class ResultDto<T> {
...
}

I tried the following but it didn't work:

createQuery(ResultDto<String>.class);

Solution

  • There is no Class instance for any specific instantiation of a generic type (e.g. ResultDto<String>, ResultDto<Integer>, ResultDto<MyObject>, ...).

    No matter whether the class is generic or not there is always only one Class instance which represents raw version of the class. In your case, it is ResultDto.class.