The reason for this is the erasure type : the fact that this is an Integer list is known to the compiler, not the JVM.
After compiling the code, List<Integer> becomes a List<Object> , allowing code based on reflection without errors.
Please note that your own code has a strong hint of why this works:
a.getClass() .getMethod("add", Object.class) // <<== Here: Object.class, not Integer.class .invoke(a, new Double(0.55555));
Also note that you can achieve the same evil result through some creative use of casting, without reflection. All this is the result of a design solution for implementing Java generics with type-wiping.
dasblinkenlight Nov 30 '13 at 13:39 2013-11-30 13:39
source share