I guess it's probably best to start with the behaviour I'm looking at:
public class genericTest {
public static void main(String[] args) {
String str = "5318008";
printClass(str); // class java.lang.String
callPrintClass(str); // class java.lang.String
printClassVarargs(str); // class java.lang.String
callPrintClassVarargs(str); // class java.lang.Object
}
public static <T> void printClass(T str) {
System.out.println(str.getClass());
}
public static <T> void printClassVarargs(T ... str) {
System.out.println(str.getClass().getComponentType());
}
public static <T> void callPrintClass(T str) {
printClass(str);
}
@SuppressWarnings("unchecked")
public static <T> void callPrintClassVarargs(T str) {
printClassVarargs(str);
}
}
Looking at printClass()
and callPrintClass()
, it seems like everything is working fine. callPrintClass()
takes a generic argument and passes it along. printClass()
recognises this variable by its correct type, not caring who is sending the parameter, and then does as it's supposed to and prints java.lang.String
.
But when we try to use varargs, this stops working. I would expect printClassVarargs()
to recognise it's argument is of type String[]
, much like the method without varargs recognises the type of its argument. Notice also that this doesn't happen if I call printClassVarargs()
directly (it's perfectly happy there to output String
), but only when it's called by callPrintClassVarargs()
, where it forgets the type of its argument and presumes it's getting an Object
. I also realise I'm having to suppress a compiler warning here, which usually comes along when I'm trying to cast generics, but I'm not sure what exactly is going on there.
So my question is really two. What is the reason behind this behaviour? Is this some consequence of type erasure, or the way Java handles arrays? And secondly, is there any way around this?
This is only a simple example, of course. I'm not trying to print class names this way, but originally found the problem while writing an overloaded method to concatenate arrays.