I have a legacy class that the class itself is not a generic but one of its methods return type uses generics:
public class Thing { public Collection<String> getStuff() { ... } }
getStuff() uses generics to return a collection of strings. Therefore I can iterate over getStuff() and there’s no need to cast the elements to a String:
Thing t = new Thing(); for (String s: t.getStuff()) // valid { ... }
However, if I change Thing itself to be a generic but keep everything else the same:
public class Thing<T> { public Collection<String> getStuff() { ... } }
and then keep using the non-generic reference to Thing, getStuff() no longer returns Collection<String> and instead returns a non-typed Collection. Thus the client code does not compile:
Thing t = new Thing(); for (String s: t.getStuff()) // compiler complains that Object can't be cast to String { ... }
Why is this? What are the workarounds?
My guess is that by using a non-generic reference to a generic class, Java turns off all generics for the entire class. This is pain, because now I’ve broken my client code by making Thing a generic.
Edit: I’m making Thing generic for another method which is not listed in the above example code. My question is educational as to why the above cannot be done.
Ok, take two, I misunderstood your question.
When you delcare
Thing(this is called a raw type) instead ofThing<?>(parameterized type) the Java compiler strips out all generic arguments, even thogh (as in your case) the generic type of the method has nothing to do with the generic type of the class.From the (excellent) Java Generics FAQ:
Can I use a raw type like any other type?
This seemingly inocuous and unobtrusive sentence describes the behaviour in question. You’re using
Thingas a raw type so the return type isCollection(notCollection<String>) since this is the type after type erasure.Confused? Not surprising. Just look at the size of that FAQ. There’s probably about three people on earth who nderstand the full implications of Java Generics. Just consider my favourite declaration from the JDK:
(Theres an explanation of that in the FAQ too).