tags:

views:

49

answers:

2

Hi, I have a question about generics.

I have a function that knows about a class type of object that I want to create:

public static <T> XmlParserInterface<T> createXmlParser(Class<T> rootType, String currentTagName) {
 XmlParserInterface<T> result = null;

 if (rootType.equals(List.class)) { 
  result = new XmlParserList<T>();
 }
 // ...

    result.init(rootType, currentTagName);
    return result;
}

In that function, for this special case, I call it with rootType being List<String>.

In my init function, I pass again the rootType so that I can create an ArrayList<something>. My problem here is to find the something since I will need it to generate further objects. In this case, it would be String, but it could go even deeper with being a List<List<String>> or even a custom POJO that I check for their set functions to fill them.

I tried doing rootType.getTypeParameters()[0], but it only returns me E. I need to know the actual class of the E.

Thanks if anyone can answer that :)

+1  A: 

Alas this information isn't available at run-time, Java throws this information away through a process of erasure. Therefore, alas, what you want to do isn't possible.

Adrian Smith
+1  A: 

You need to add the type information explicitly - it's not available at runtime because generic type parameters are deleted during compilation. And since you need to specify this explicitly you cannot do what you want (as far as I can see) using just generics. Instead you will need to do some casting (and pass in some kind of structure of classes that indicates what you want to construct).

See the excellent book on Java Generics by Wadler (really, you need to buy and read that book if you are trying to write code like this).

If you always had, say List<X> then you could do this (you would still need to extend the signature, but the code would compile without any type warnings). Or even X<Y> where X is itself a variable that, say, subclasses List. What you cannot do is handle both X<Y> and X<Y<Z>> without forcing the system to do "unsafe" operations.

The X<Y> case would look something like:

public <Y, X extends List<Y>> X<Y> myMethod(Class<X<Y>> xClass, Class<Y> yClass, ...) { ...}

(that's just from memory but you can get the idea, I hope - you pass both X and Y, and you use qualified type annotations to express the relationship between them. Really, read Wadler's book!).

And if you wanted to force things by hand, you'd have to just pass in a list of Class values without types, and add a lot of casting.

andrew cooke
Thanks, I tried the `T extends List<Y>` and got to specify the Y so it only moved my problem upper as you said. I think I will add a special annotation to my properties that are Lists. I tough about typing the class name as a string, but you gave me a great idea to use a `Class[]`. Thanks again
Simon Levesque