Lambda suggestions in Eclipse like IntelliJ does

Viewed 3264

When in IntelliJ you enter some code like this :

Runnable cronTask = new Runnable() {
   @Override
   public void run() {
      // do something
   }
};

IntelliJ will automatically suggest that this will be better when using a lambda expression. And you have the option to do an automatic conversion to

Runnable cronTask = () -> {
   // do something
};

Is something like this possible in Eclipse ? Maybe with some kind of plugin ? I want Eclipse to give me warnings of where a lambda expression might be a better solution. And if possible also suggest the correct fix.

2 Answers

Once you have "Use lambda when possible", you can now (Eclipse 2020.03) clean them up

A new clean up has been added that simplifies the lambda expression and the method reference syntax and is enabled only for Java 8 and higher.

The clean up:

  • removes parenthesis for a single untyped parameter,
  • return statement for a single expression and
  • brackets for a single statement.

It replaces a lambda expression by a creation or a method reference when possible.

To select the clean up, invoke Source > Clean Up..., use a custom profile, and on the Configure... dialog select Simplify lambda expression and method reference syntax on the Code Style tab.

For the given code:

Lambda before cleanup -- https://www.eclipse.org/eclipse/news/4.15/images/lambda-expression-enhancements-before.png

You get this after the clean up:

Lambda after cleanup -- https://www.eclipse.org/eclipse/news/4.15/images/lambda-expression-enhancements-after.png

Related