Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda suggestions in Eclipse like IntelliJ does

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.

like image 541
Knarf Avatar asked Jul 13 '17 13:07

Knarf


People also ask

What is Lambda How does it improve help in implementation?

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

What is lambda in Java and for what it is used?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

Can Lambda Access outer variable in Java?

Lambda expressions can use variables defined in an outer scope. We refer to these lambdas as capturing lambdas. They can capture static variables, instance variables, and local variables, but only local variables must be final or effectively final.


2 Answers

To the best of my knowledge, there is no way to make Eclipse show compiler warnings where lambda expressions are a better alternative. Nevertheless, there is a nice feature that takes care of the automatic conversion you requested.

Right-click on any project or Java file, and select Source -> Clean Up... In the window that appears, select Use custom profile, and click Configure... In the Code Style tab, enable Convert functional interface instances, and make sure Use lambda where possible is selected, as shown in the screenshot below:

Lambda conversion

Validate and run the clean up. This feature alongside the manual Quick assist suggested by Timothy Truckle in the comments will hopefully cover most of your needs.

like image 122
Pyves Avatar answered Oct 12 '22 10:10

Pyves


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

like image 25
VonC Avatar answered Oct 12 '22 11:10

VonC