tags:

views:

75

answers:

1

I want to know the class of a generic return type of a method, something like this:

    <T> T getEntry() { System.out.println(T.class) }

The problem is, this is not a generic class, this is just a generic method, so I can't extract the generic type from the class. What I want to achieve is knowing what concrete type the caller wants without requiring the class, like:

<T> T getEntry(Class<?> clazz);

Is that possible?

+5  A: 

Well if Kylar is right I've to resort to:

<T> T getEntry(String name, Class<T> clazz)

I wanted to avoid the extra argument :(

einundswanzig
That however depends on what you need the `Class<T>` for. If it's e.g. to create a `newInstance()`, then it's indeed mandatory. But if it's just to cast the `return` value, then you can also just do `return (T) value;` instead of `return clazz.cast(value);` and live with the `@SuppressWarnings("unchecked")` ;)
BalusC
I need to create an instance of an object with Spring SpEL, something like:parser.parseExpression(expr).getValue(clazz.getName());
einundswanzig
Yes, passing in the runtime type is then mandatory.
BalusC
Are you guys living in here? We can almost consider this a chat because of the comments rate.
einundswanzig