Missing null check in bound method reference capture

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P3
    • 8
    • Affects Version/s: 8
    • Component/s: 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;
        }
      }

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

              Created:
              Updated:
              Resolved: