Search code examples
javalambdajava-8sonarlintfunctional-interface

SonarLint: make this anonymous inner class a lambda (non functional interface)


I'm running SonarLint 3.4 and Oracle JDK 8. SonarLint is giving me this error:

Anonymous inner classes containing only one method should become lambdas (squid:S1604)

The interface, which I don't have control over, is setup like this:

public interface Interface {
   static String staticMethodOne() {
      return "abc";
   }

   default String methodOne(String input) {
      return "one: " + input;
   }

   default String methodTwo(String input) {
      return "two: " + input;
   }
}

This is the code that generates the error:

public class Main {
   public static void main(String[] args) {
      callMethodOne(
         new Interface() {
            @Override
            public String methodOne(String input) {
               return ("override: " + input);
            }
         }
      );
   }

   private static void callMethodOne(Interface instance) {
      System.out.println(instance.methodOne("test"));
   }
}

Since "Interface" is not a functional interface I don't see a way to replace it with a lambda. Is this a bug in SonarLint or am I missing something?


Solution

  • Confirmed as a bug in SonarJava; time to wait for the next SonarLint update.

    https://jira.sonarsource.com/browse/SONARJAVA-2654