ekj
ekj

Reputation: 1082

Java reflection: Get concrete type of implemented generic interface

Say I have a class like the following

public class AtomEntryHandler implements ConsumingHandler<AtomEntry>
{
...
}

Is it possible to get the class object AtomEntry.class from the class object of AtomEntryHandler.class ?

I didn't think it was possible due to erasure, but a friend said it is.

Upvotes: 9

Views: 14587

Answers (4)

David Moles
David Moles

Reputation: 51093

If you happen to know ConsumingHandler is the only interface AtomEntryHandler implements, and you happen to know it takes just one type argument, you can do this:

interface ConsumingHandler<T> {}

class AtomEntry {}

class AtomEntryHandler implements ConsumingHandler<AtomEntry>
{
    public static void main( String[] args )
    {
        Type[] interfaces = AtomEntryHandler.class.getGenericInterfaces();
        ParameterizedType firstInterface = (ParameterizedType) interfaces[0];
        Class c = (Class) firstInterface.getActualTypeArguments()[0];
        System.out.println(c.getName()); // prints "AtomEntry"
    }
}

Otherwise, you can poke around in getGenericInterfaces() and their actualTypeArguments until you find something that looks like what you're looking for.

But if you find yourself needing to do this in real code, either something's probably gone badly wrong in your design, or you're writing some mad genius mock object library and you shouldn't need us to answer these questions.

Upvotes: 2

user545680
user545680

Reputation:

There is a blog post that goes over it in detail here: Reflecting Generics.

Upvotes: 0

Chris Hannon
Chris Hannon

Reputation: 4134

You can get the generic type for both interfaces and direct subclasses, but only for the concrete implementation. For example, if you have a List<T> instance, you have no way of knowing what it's been parameterized to because of type erasure. If the class definition includes parameterized types that are known at compile time (for example, class StringList extends List<String>) then you can retrieve that information.

ParameterizedType pt = (ParameterizedType)AtomEntryHandler.class.getGenericInterfaces()[0];
Class atomEntryClass = (Class)pt.getActualTypeArguments()[0];

Upvotes: 10

Alex Gitelman
Alex Gitelman

Reputation: 24722

I could not figure a way to determine base type parameter in case of interface implementation (which does not mean there is none). But this is as close as it gets to it.

import java.lang.reflect.*;
public class Foo {
  static class Bar<T> {
  }
  static class SubBar extends Bar<Integer> {
  }

  public static void main(String argv[]) {
    ParameterizedType pt = (ParameterizedType)SubBar.class.getGenericSuperclass();
    Type[] t = pt.getActualTypeArguments();
    for (int i=0;i<t.length;i++) {
       System.out.println(t[i]);
    }
  }
}

Result: class java.lang.Integer

Upvotes: 5

Related Questions