-1

In Java, Imagine these interfaces and classes:

public interface Iface<S, T> { ... }

public class Parent<A, B> implements Iface<B, A> { ... }

public class Sub extends Parent<String, Integer> { ... }

Is there a way to make a function that accepts an Iface and obtains which are the types of its generics. So for example, if you enter a Sub.class:

public void showTypes(Class<Iface<S, T>> ifaceClass) {
    // do something...
}

//showTypes(Sub.class) --> S = Integer.class, T = String.class

EDIT: About type erasure. If you have an ArrayList<?> you can't know which generic type it is. But this case is different. If I have a class that extends ArrayList<String> I can know its generic type (String.class). Please look at this snippet:

@SuppressWarnings("serial")
public class StringList extends ArrayList<String> {
    public static void main(String... args) {
        ParameterizedType superType = 
            (ParameterizedType) StringList.class.getGenericSuperclass();
        Type[] types = superType.getActualTypeArguments();
        Class<?> clss = (Class<?>) types[0];
        System.out.println(clss);
    }
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • @GanGnaMStYleOverFlowErroR: Maybe, but I need some more help :) – sinuhepop Jan 22 '13 at 10:50
  • Because you don't like to accept answers as it seems or communicate with the responsers, for example ITT or here: http://stackoverflow.com/questions/2764842/issues-in-ajax-based-applications – czupe Jan 22 '13 at 11:15
  • yeah, but i always used to answer for every solution : ) And as i checked some of your question it was not the state for your... The same also true for this thread. I don't observe the framework you asked for, in any of your response or in the question. Overall: i think you should be more communicative/repsonsive and explicit about the question... Anyway Peace. Gl finding what you want. – czupe Jan 22 '13 at 11:26
  • and i upvated it because of the communication ( and i obviously downvote it, because of the lack of it). by. – czupe Jan 22 '13 at 11:34
  • @czupe: Anyway, peace ;) – sinuhepop Jan 22 '13 at 11:37

3 Answers3

2

Well, I found what I was looking for. TypeTools is a small library with only one class, designed to do exactly this. The pity is that is not in Maven central repository.

TypeResolver.resolveArguments(Sub.class, Iface.class) --> [java.lang.Integer, java.lang.String]

Spring has a similar method in GenericTypeResolver:

GenericTypeResolver.resolveTypeArguments(Sub.class, Iface.class);
sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • Dude you are awesome... So sick... We work on your problem, and you downvote our answers?:))) Seriously wtf?:) There is a lot of topic about this, and without hack you cant do it, because of the generics erasure.. Anyway it is nice resarch. But still dont think http://stackoverflow.com/questions/1942644/get-generic-type-of-java-util-list http://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime http://stackoverflow.com/questions/8030101/java-get-generic-type Than you edit your loose specific question and we got downvote... so sick.. Seriously i dont understand you... – czupe Jan 23 '13 at 12:07
  • @czupe: Hey, I promise I haven't downvoted any answer! As I said in my edit, there is no type erasure here. There is a little complex way of achieving what I want, but I no consider it a hack. – sinuhepop Jan 23 '13 at 15:57
  • Ok, I apologize from you sinuhepop : ( My bad (experiences... :/) It would be logical (but unfair) to get a downvote from you, because you got a "better solution" and also you are the questioner, have a nice days! Cu! – czupe Jan 24 '13 at 10:45
  • Anyway this tool as it seems from the source, also use reflection, but in high level : ) – czupe Jan 24 '13 at 11:05
0

No, this cannot be done due to the way generics are implemented in Java. During run time, there is no information left about the actual types used for generics parameters.

You may read some more details about type erasure here

Henry
  • 42,982
  • 7
  • 68
  • 84
  • 1
    You cannot find the type used for declaration (v.g., `new ArrayList`) but you can always use the `getClass` method to find the actual class. I am not sure what is the OP asking form. – SJuan76 Jan 22 '13 at 10:14
  • @SJuan76: I'm asking for the second option. Please, see my edit. – sinuhepop Jan 22 '13 at 11:27
-1
Update 01.23:

We and other responser answered for a loose specificated question, so we dont deserve the -1 vote.... Not so fair...

Original answer:

No, there is no way for that...

In generics main specification, when you use T and S it will generate this code :

public void showTypes(Class<Iface<Object, Object>> ifaceClass) {
    // do something...
}

So you will not know this information...

You can have an solution to define the class of a generic type like this:

public void showTypes(T type)
{
if (s instanceof Number) 
 System.out.println("Number")
if (s instanceof String) 
 System.out.println("String")
}

But this is not an option in your case as i see...

And it will only determine in run-time, not compile time...

czupe
  • 4,740
  • 7
  • 34
  • 52