Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8220612

Method reference allows unboxing without cast

    Details

    • Type: Bug
    • Status: In Progress
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 9, 10, 11, 12
    • Fix Version/s: tbd
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • Understanding:
      Fix Understood

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dlsmith Dan Smith
                Reporter:
                dlsmith Dan Smith
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: