Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 841
ekj Avatar asked Oct 21 '11 03:10

ekj


3 Answers

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];
like image 157
Chris Hannon Avatar answered Sep 28 '22 01:09

Chris Hannon


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

like image 32
Alex Gitelman Avatar answered Sep 28 '22 01:09

Alex Gitelman


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.

like image 35
David Moles Avatar answered Sep 28 '22 00:09

David Moles