public T getById(int id) { return entityManager.find(T.class, id); } Error: T cannot select from a type variable. Found:
ReflectionUtils.getGenericParameterClass(..) - не работает The question is how to pass a parameterized class object?
public T getById(int id) { return entityManager.find(T.class, id); } Error: T cannot select from a type variable. Found:
ReflectionUtils.getGenericParameterClass(..) - не работает The question is how to pass a parameterized class object?
Generic parameters in runtime do not exist, after compilation they are replaced with the most simple type (That is, <T> will be replaced with Object , will be replaced with Number, etc.), to get the class, it is easiest to submit it to the constructor,
public class MyClass<T extends Number> { private final Class<T> clazz; public MyClass(Class<T> clazz) { this.clazz = clazz; } Then you can create it by letting the compiler itself determine the generic new MyClass<>(Integer.class); will be equivalent to new MyClass<Integer>(Integer.class); after which the clazz variable can be used instead of T.class .
Source: https://ru.stackoverflow.com/questions/661641/
All Articles