The following code works when compiled with sourceCompatibility=1.7 or 1.6, but fails after switching to 1.8:
public class Java8Wat {
interface Parcelable {
}
static class Bundle implements Parcelable {
public void put(Parcelable parcelable) {
}
public void put(Serializable serializable) {
}
public <T extends Parcelable> T getParcelable() {
return null;
}
}
static {
Bundle inBundle = new Bundle();
Bundle outBundle = new Bundle();
outBundle.put(inBundle.getParcelable());
}
}
Compilation output:
Java8Wat.java:23: error: reference to put is ambiguous
outBundle.put(inBundle.getParcelable());
^
both method put(Parcelable) in Bundle and method put(Serializable) in Bundle match
Here's the repo with failing code: https://github.com/chalup/java8-wat. Just invoke ./gradlew clean build
from project directory.
I skimmed through JLS for Java 8, but I haven't found anything relevant.
Additional observation: the code compiles if I change the getParcelable()
signature to:
public Parcelable getParcelable()
Why does java compiler considers put(Serializable)
to be potentially applicable method for outBundle.put(inBundle.getParcelable())
call and what changes should be made to Parcelable/Bundle class? Bonus question: why does this error happens only on Java 8 and not on Java 7?