Odd compiler behavior (re: generics)
Greetings, I am encountering a rather odd compiler generated error/warning with some code. The issue involves using an "independent" generic method of a raw-typed class. That is, the class has a variable T (call it Test<T> and has an instance method that returns ArrayList<String>. The problem: when a raw typed Test is used, theArrayList returned is also raw. Here is some code to illustrate:
import java.util.ArrayList;
class Test<T>{
ArrayList<String> getChildren(){
returnnull;
}
staticvoid test1( Test t ){
ArrayList<String> children = t.getChildren();
// WARNING: ^^^^^^^^^^^^^^^^
//Test.java:26: warning: [unchecked] unchecked conversion
//found: java.util.ArrayList
//required: java.util.ArrayList<java.lang.String>
}
staticvoid test2( Test<?> t ){
// OK, since we have the type variable set.
ArrayList<String> children = t.getChildren();
}
If the type variable is specified as anything but raw, then it works fine.
Is there some explantation for this behavior? My argument: since the return type of getChildren doesn't use the wildcard, why does the type argument of the implicit instance matter? Is this a compiler bug or just a "sharp edge" in the specification (perhaps even reasonable or necessary).
I know the solution (or workaround). I am just seeking an explanation.
Thanks,
- Tim

