Java reflection: Get concrete type of implemented generic interface?

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 { } static class SubBar extends Bar { } public static void main(String argv) { ParameterizedType pt = (ParameterizedType)SubBar.class.

GetGenericSuperclass(); Type t = pt. GetActualTypeArguments(); for (int i=0;iPrintln(ti); } } } Result: class java.lang.Integer.

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 { } static class SubBar extends Bar { } public static void main(String argv) { ParameterizedType pt = (ParameterizedType)SubBar.class. GetGenericSuperclass(); Type t = pt. GetActualTypeArguments(); for (int i=0;iLength;i++) { System.out.

Println(ti); } } } Result: class java.lang.Integer.

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 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) then you can retrieve that information.

ParameterizedType pt = (ParameterizedType)AtomEntryHandler.class. GetGenericInterfaces()0; Class atomEntryClass = (Class)pt. GetActualTypeArguments()0.

I read about interface in doc, but if class implements parameterized interface as in question (and does not extend any class explicitly), you will get java.lang. Object and cast exception when casting to ParameterizedType. – Alex Gitelman Oct 21 at 4:31 If you call getGenericSuperclass() and you don't explicitly extend anything or you extend non-generic classes, that's true, but if you call getGenericInterfaces() then the class heirarchy doesn't matter because it's only dealing with interfaces.

– Chris Hannon Oct 21 at 4:45.

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

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 {} class AtomEntry {} class AtomEntryHandler implements ConsumingHandler { public static void main( String args ) { Type interfaces = AtomEntryHandler.class. GetGenericInterfaces(); ParameterizedType firstInterface = (ParameterizedType) interfaces0; 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.

Yeah I had a play around with it, and it just makes the code more complicated than it needs to be. It would have just avoided some duplication if it was simple. – ekj Oct 27 at 11:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions