Java 7 was stupid (not it wasn't) when inferring type arguments for generic methods. For example, it would use the declared type of an argument to infer the type regardless of the context of the method invocation. So in
Arrays.asList(obj1);
the type argument would be inferred as
DataWrapper<Object>
the method's return type would then be List<DataWrapper<Object>>
which is no assignable to a List<DataWrapper<?>>
.
In
Arrays.asList(obj1, objInt);
the type is inferred from both arguments. A common type is found between the two. In this case, that's ? extends Object
. The method return type becomes List<DataWrapper<? extends Object>>
which is assignable to List<DataWrapper<?>>
.
In Java 8, what you posted works out of the box. In Java 7, you can provide an explicit type argument to make it work
List<DataWrapper<?>> anyDataList = Arrays.<DataWrapper<?>>asList(obj1);