Uploaded image for project: 'NetBeans'
  1. NetBeans
  2. NETBEANS-3802

Hint suggests invalid replacement with lambda for implementations of interfaces with only default methods

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 11.2
    • Fix Version/s: None
    • Component/s: java - Hints
    • Labels:
    • Environment:
      JDK 13

      Description

          interface Dog {
              default void run() {
              }
              default void bark() {
              }
          }
      
          public static void foo() {
              final Dog dog = new Dog() {
                  @Override
                  public void bark() {
                      System.out.println("Woof");
                  }
              };
          }
      

      For this code NetBeans has a hint stating

      This anonymous inner class creation can be turned into a lambda expression.

      in the line with new Dog(). Replacing the code using the hint results in

      final Dog dog = () -> {
          System.out.println("Woof");
      };
      

      which is invalid code

      incompatible types: Dog is not a functional interface
      no abstract method found in interface Dog

      (Real world example: http://jdbi.org/apidocs/org/jdbi/v3/core/statement/SqlLogger.html This style of interfaces would also be a nice alternative to Swing’s *Listener/*Adapter pairs.)

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              striderapache dennis lucero
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: