What type will be returned by a method called from a common class?

Is there a way to figure out which type will be returned by a method called from a generic class?

There is an example:

Generified<Integer, Double> generified = new Generified<Integer, Double>(); Method method = generified.getClass().getMethod("getDoubleValue"); Class<?> returnType = ........ 

Ok guys there is a class:

 public class Generified<T, Z> { private T tValue; private Z doubleValue; private List<T> tValueList; public Generified() { } public T gettValue() { return tValue; } public void settValue(T tValue) { this.tValue = tValue; } public Z getDoubleValue() { return doubleValue; } public void setDoubleValue(Z doubleValue) { this.doubleValue = doubleValue; } public List<T> gettValueList() { return tValueList; } public void settValueList(List<T> tValueList) { this.tValueList = tValueList; } } 

Update:

This is why I ask:

I am trying to make lib that can do simple clusters for collections. There is what I have now: github.README.md

But I have a lot of problems with return types from methods of generic classes. I need to know the actual return type in order to wrap it with CGLIB and add an interceptor for all methods for this return class in order to know the call hierarchy in the future.

But after a lot of debugging, I see that now there is a way to do what I want, and I want to cry :)

+4
source share
3 answers

The answer is obvious from the method name - getDoubleValue() should return Double . But since you did not provide code for this class, we can only guess.

But if you're talking about the general case here, thinking can go that far: Method.getReturnType () and Method.getGenericReturnType () .

UPDATE: If you check MethodSpy on this page and run it against your class, you will get:

 public Z Generified.getDoubleValue() ReturnType: class java.lang.Object GenericReturnType: Z 

I don’t think you can find what you are looking for simply by looking at the general class, it just does not contain the information you need. Hope this helps.

+2
source

You can declare Generified as an abstract class, so you must use a subclass to instantiate, and when you do, you can use getClass (). getGenericSuperclass () inside the Generated constructor and get the actual type parameters, for example:

 package com.stackoverflow.q15998449; import java.lang.reflect.ParameterizedType; import java.util.List; public abstract class Generified<T, Z> { private T tValue; private Z doubleValue; private List<T> tValueList; private final Class<Z> zType; private final Class<T> tType; @SuppressWarnings("unchecked") public Generified() { ParameterizedType a = ((ParameterizedType) getClass().getGenericSuperclass()); tType = (Class<T>) a.getActualTypeArguments()[0]; zType = (Class<Z>) a.getActualTypeArguments()[1]; } public Class<T> getTReturnType() { return tType; } public Class<Z> getZReturnType() { return zType; } public T gettValue() { return tValue; } public void settValue(T tValue) { this.tValue = tValue; } public Z getDoubleValue() { return doubleValue; } public void setDoubleValue(Z doubleValue) { this.doubleValue = doubleValue; } public List<T> gettValueList() { return tValueList; } public void settValueList(List<T> tValueList) { this.tValueList = tValueList; } 

Your class user will look like this:

 package com.stackoverflow.q15998449; import java.lang.reflect.Method; public class User { public static void main(String[] args) throws NoSuchMethodException, SecurityException { Generified<Integer, Double> generified = new Generified<Integer, Double>(){}; Method method = generified.getClass().getMethod("getDoubleValue"); Class<?> returnType = generified.getZReturnType(); } } 
+2
source

I do not understand what the problem is. You know from .getGenericReturnType() that the return type is Z And you know that Z is the second parameter. And you explicitly create an object with the specific Double parameter in the first line. So, you know exactly what it is.

0
source

All Articles