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

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 11.2, 12.5
    • None
    • java - Hints
    • 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

            Unassigned Unassigned
            striderapache dennis lucero
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: