I have following class:
public abstract class AbstractService<PersistenceClass extends IPersistence, RequestClass, ResponseClass> { public String processRequest(final String aRequestAsText) { [...] final RequestClass request = mapper.readValue(aRequestAsText, RequestClass); [...] } } The mapper.readValue invokation is normally implemented as
mapper.readValue(aRequestAsText, Request.class) How can I code it, if the class RequestClass is a generic?
Update 1 (06.09.2013): This code seems to work:
public class GenericTypeBindigsFinder implements IGenericTypeBindigsFinder { @SuppressWarnings("rawtypes") @Override public List<Class> getGenericTypes(final Class aClass) { final List<Class> result = new LinkedList<Class>(); final ParameterizedType gen = (ParameterizedType) aClass.getGenericSuperclass(); final Type [] types = gen.getActualTypeArguments(); for (int i = 0; i < types.length; i++) { if (types[i] instanceof Class) { final Class clazz = (Class)types[i]; result.add(clazz); } } return result; } } You can find the corresponding unit test here.
RequestClass(sort ofRequestClass.class).request.getClass()should do the trick for you, as @ragatskynet answered below.