I am using Android Studio 1.1.0.
This causes no warning:
public static class A {
public Map<Integer, String> getMap() {
return null;
}
}
public static class B {
public void processA(A a) {
Map<Integer, String> map = a.getMap();
}
}
But make A
generic:
public static class A<T> {
public Map<Integer, String> getMap() {
return null;
}
}
And this line:
Map<Integer, String> map = a.getMap();
gets you a warning now: "Unchecked assignment: 'java.util.Map to java.util.Map<java.lang.Integer, java.lang.String>'
.
Even though the signature of getMap
is totally independent of T
, and the code is unambiguous regarding the types the Map
contains.
I know that I can get rid of the warning by reimplementing processA
as follows:
public <T> void processA(A<T> a) {
Map<Integer, String> map = a.getMap();
}
But why would I have to do that? What does T
matter here at all?
So, the question is - why does type erasure have to not only affect T
(which is understandable - if I'm passing an instance of A
, T
is an unknown), but also "hardcoded" generic signature like <Integer, String>
in this case?
You might not be using anything related to type of A, but hey the compiler doesn't know this fact
- well, it could get to know it quite easily, just take a look at the implementation ofprocessA
, it's not hard to verify thatT
is irrelevant ;) I'll go with<?>
as you suggest, but I'm disappointed with the compiler. – Urethroscope