Scenario: a reference to a method with a type variable return type that is instantiated to a box type (Integer, Long, Double, etc.), used to implement a method with a primitive return type.
If heap pollution allows a Number of some other type to be returned at run time, conversion to the primitive type happens without error. No cast check is performed. A narrowing primitive conversion may occur.
List<Integer> li = new ArrayList<>();
List lraw = li;
lraw.add(23.8); // unchecked warning
IntUnaryOperator f1 = li::get;
f1.applyAsInt(0); // result: 23
// for comparison
Function<Integer,Integer> f2 = li::get;
f2.apply(0); // CCE: can't convert Double to Integer
Per JLS 15.13.3, "the body returns the result of the method invocation or object creation, after any necessary assignment conversions (§5.2)." The conversion being applied here is Integer-->int, and we'd expect a CCE to occur if the object to be converted is not an Integer.
If heap pollution allows a Number of some other type to be returned at run time, conversion to the primitive type happens without error. No cast check is performed. A narrowing primitive conversion may occur.
List<Integer> li = new ArrayList<>();
List lraw = li;
lraw.add(23.8); // unchecked warning
IntUnaryOperator f1 = li::get;
f1.applyAsInt(0); // result: 23
// for comparison
Function<Integer,Integer> f2 = li::get;
f2.apply(0); // CCE: can't convert Double to Integer
Per JLS 15.13.3, "the body returns the result of the method invocation or object creation, after any necessary assignment conversions (§5.2)." The conversion being applied here is Integer-->int, and we'd expect a CCE to occur if the object to be converted is not an Integer.
- relates to
-
JDK-8174983 LambdaMetafactory: standardize supported type conversions
- In Progress