6
public Interface Foo<T extends Colors>{...}

Is there a way to retrieve which T was given for an implementation of Foo?

For example,

public Class FooImpl implements Foo<Green>{..}

Would return Green.

1
  • From where are you attempting to retrieve it? Commented Nov 3, 2009 at 20:50

6 Answers 6

15

Contrary to other answers, you can obtain the type of a generic parameter. For example, adding this to a method inside a generic class will obtain the first generic parameter of the class (T in your case):

ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
type.getActualTypeArguments()[0]

I use this technique in a generic Hibernate DAO I wrote so I can obtain the actual class being persisted because it is needed by Hibernate. It works!

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

4 Comments

This will give the declared parameter type -- but it looks like that is what is wanted anyway.
+1, always fun to learn knew things, even when you have to be wrong to do so.
Thanks for following up on this. I was thinking the answer was what @JaredPar said before i even asked the question, so i guess i was a bit too quick in accepting it!
@JaredPar - that's why this is a great site - so many fringe questions asked that there is always something new to learn about things we feel we already know :-)
9

EDIT

Turns out for this case it is possible to get the generic information. Singleshot posted an answer which does just that. His should be the accepted answer. Re-qualifying mine.

In general though, there are many cases where you are unable to get type information you might expect to be there. Java uses a technique called type erasure which removes the types from the generic at compile time. This prevents you from getting information about their actual binding at runtime in many scenarios.

Nice FAQ on the subject:

3 Comments

This is wrong. You can call Class.getGenericInterfaces() and obtain type arguments from ParameterizedType
I agree. See my answer, which is code I actually use in a generic Hibernate DAO I wrote.
@SingleShot, @ChssPly76, nice to know this is possible. I updated my answer to essentially point to @SingleShot's answer.
1

There's. Look at [Javadoc for java.lang.Class#getGenericInterfaces()](http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getGenericInterfaces()).

Like this:

public class Test1 {

    interface GenericOne<T> {
    }

    public class Impl implements GenericOne<Long> {
    }

    public static void main(String[] argv) {
        Class c = (Class) ((ParameterizedType) Impl.class.getGenericInterfaces()[0]).getActualTypeArguments()[0];
        System.out.println(c);
    }
}

Comments

0

One way to do this is to explicitly pass in a Class object with the type. Something like the following:

public class FooImpl<T extends Colors> {
  private Class<T> colorClass;
  public FooImpl(T colorClass) {
    this.colorClass = colorClass;
  }
  public Class<T> getColorClass() {
    return colorClass;
  }
}

Comments

0

Depends on what you mean exactly. Just T might be what you want, for example:

public Interface Foo<T extends Colors>{ public T returnType() {...} ...}

Comments

-2

[edit] Ok, apparently partially possible. Good explanation of how to do it, (including an improvement upon the method posted by SingleShot): http://www.artima.com/weblogs/viewpost.jsp?thread=208860

1 Comment

that should probably read Class<T> getTypeParameter() and Class<Green> getTypeParameter..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.