When the code below is run:
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
class A {
private static final List<Adapter<? extends Number>> adapters = Arrays.asList(
Integer::valueOf,
s -> Long.valueOf(s),
(Adapter<Float>) s -> Float.valueOf(s),
new Adapter<Double>() {
@Override
public Double adapt(String s) {
return Double.valueOf(s);
}
}
);
private interface Adapter<T> {
T adapt(String s);
default Type type() {
try {
return getClass().getMethod("adapt", String.class).getReturnType();
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
}
public static void main(String[] args) {
for (Adapter<?> adapter : adapters) {
System.out.printf("Got adapter for type %s%n",
adapter.type().getTypeName());
}
}
}
I get:
% javac A.java
% java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
% java A
Got adapter for type java.lang.Object
Got adapter for type java.lang.Object
Got adapter for type java.lang.Object
Got adapter for type java.lang.Double
I am curious why only the last adapter is recognized as Double, and the rest are Object (as opposed to Integer, Long, and Float). At the very least, I would have expected them to be for Number.