0

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.

6
  • Do you want to retrieve the type parameter from a generic declaration? Commented Sep 5, 2013 at 12:21
  • No, I want to get the class object of RequestClass (sort of RequestClass.class). Commented Sep 5, 2013 at 12:23
  • @DmitriPisarenko : Please don't use backticks for non-code, see e.g. here why Commented Sep 5, 2013 at 12:24
  • request.getClass() should do the trick for you, as @ragatskynet answered below. Commented Sep 5, 2013 at 12:27
  • @Mauren See my comment to ragatskynet's answer. Commented Sep 5, 2013 at 12:43

2 Answers 2

2

Try to pass object or an interface preferably to the method, then you can get the class of it with the getClass() method or maybe instanceof.

Sign up to request clarification or add additional context in comments.

2 Comments

I need the class of RequestClass in order to create an instance of it. So I can't use your solution. Basically, this is JSON parsing - mapper takes a text and generates a request object, which represents that text. In order to do so, it needs the class of the request.
This case you should try using the Factory pattern for the creation of the class you need. Bare polymorphism is enough, no need of generics. There is a small article on this here (but you can find many other resources of course): java.dzone.com/articles/factory-design-pattern
1

So you want to access the generic types bound in AbstractService. You can do it via reflection. See the example code here:

import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; public class GenericClass extends GenericSuper<String, Integer> { public GenericClass() { ParameterizedType gen = (ParameterizedType) getClass().getGenericSuperclass(); TypeVariable<?> typeVars[] = getClass().getSuperclass().getTypeParameters(); Type [] types = gen.getActualTypeArguments(); for (int i = 0; i < typeVars.length; i++) { System.out.println("Generic parameter " + typeVars[i] + " is bound to " + types[i]); } } public static void main(String[] args) { new GenericClass(); } } class GenericSuper<X, Y> {}; 

Output is:

Generic parameter X is bound to class java.lang.String Generic parameter Y is bound to class java.lang.Integer 

You can cast types[i] to Class.

For a much more sophisticated solution, see getTypeArguments(...).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.