Hi,

I'm having some troubles with generics, trying to get the "Type" of my
Parameterized class???

Let's begin with a working example...

class TestClass<T> {
    Type type;

    public TestClass() {
        type = getSuperclassTypeParameter(getClass());
    }

    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return parameterized.getActualTypeArguments()[0];
  }
}

Whit this code, I can get at runtime, the Type of T (like TestClass<String>
will return a String type).

My problem is when I have a generic class extending another generic class.

class AsyncRequest<T> extends AsynTask<String, Void, AsyncResponse<T>> {
    Type type;
    public AsyncRequest() {
        type = getSuperclassTypeParameter(getClass());
    }

    static Type getSuperclassTypeParameter(Class<?> subclass);
}

In this case, with getSuperclassTypeParameter() I get the parameterized
types of AsyncTask, and not of my own Class AsyncRequest...
(parameterized.getActualTypeArguments()[0] is a String type).


Anyone has some ideia of how I can get the Type of T of my own Class???

Thanks in advance...
LĂșcio Maciel
luci...@gmail.com

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to