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

Missing null check in bound method reference capture

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8
    • 8
    • tools
    • b110
    • Verified

      When a bound method reference is captured, we should do a null check on the result of the receiver:

          Sam mr = foo::bar;

      The expression "foo" is evaluated immediately, and if it is null, we should throw NPE.

      Relevant spec:

      If the expression is a method reference that begins with an ExpressionName or a Primary, this subexpression is first evaluated. [jsr335-15.28.2-30-A]
      If the subexpression evaluation completes abruptly, then the method reference evaluation completes abruptly for the same reason. [jsr335-15.28.2-30-A1]
      If the result of evaluation of the subexpression is null, then a NullPointerException is thrown. [jsr335-15.28.2-30-A2]



      Test case below:

      import java.util.function.*;

      public class Foo {
        public static void main(String[] args) {
          String s = null;
          Supplier<Boolean> ss = s::isEmpty;
        }
      }

            vromero Vicente Arturo Romero Zaldivar
            briangoetz Brian Goetz
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: