Reputation: 2156
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.
Upvotes: 6
Views: 4039
Reputation: 1324733
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 theConfigure...
dialog selectSimplify lambda expression and method reference syntax
on theCode Style
tab.For the given code:
You get this after the clean up:
And with Eclipse 4.29 (Q3 2023), you now have:
Lambda Cleanup Improvements
A number of improvements have been made to the Java cleanups concerning converting to use lambdas and simplifying existing lambdas.
First of all, the cleanup preference: Convert functional interface instances on the Java Features tab for Java 8 has added an additional checkbox:
Simplify method reference syntax for lambda conversions
. This checkbox is selected by default and instructs the cleanup to use method reference syntax where possible when converting from anonymous classes. If the checkbox is unselected, method reference syntax will not be used by default (sometimes required to avoid a runtimeNullPointerException
) but will only be used if the user has also selected theSimplify lambda expression and method reference syntax
cleanup option found on theCode Style
tab.
By default, the Quick fix to convert an anonymous class will also by default now use the method reference syntax where possible.The second improvement to the lambda cleanups and quick fix is that the code now recognizes use of
instanceof
can be replaced with a method reference to isInstance.The following code:
with the lambda cleanup will by default change to:
Upvotes: 1
Reputation: 6483
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:
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.
Upvotes: 8