'Java get generic type of static method at runtime
So I was reading through the source code of Collections.java
, there is a generic method copy()
.
I simplified it to this:
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
System.out.println(dest.getClass());
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
It makes sense syntactically, since dest
is consumer and src
is producer, but I really want to know what is T
here.
For example:
public static void main(String[] args) {
List<Number> dest = new ArrayList<>(5) {
{
for (int i = 0; i < 5; i++)
add(null);
}
};
List<Integer> src = new ArrayList<>(5) {
{
add(1);
add(2);
add(3);
add(4);
add(5);
}
};
copy(dest, src);
System.out.println(dest);
}
What does T
stand for in this scenario? Is there any why to get the T
at runtime?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|