Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic replacing all anonymous inner class to lambda in Intellij Idea

I'm working on a project which is implemented in java 7 and now I want to migrate to java 8. I use IntelliJ Idea and want to find all "new inner class occurrence" whit regex finding("new .(.)\s*{[.|\s]*") to replace with their anonymous versions, But those are very numerous! How to enforce Intellij to replace all these inner classes?

like image 325
mahdi_12167 Avatar asked Apr 20 '16 10:04

mahdi_12167


People also ask

How do you replace an anonymous class with a lambda expression?

Since the most common use of Anonymous class is to provide a throwaway, stateless implementation of abstract class and interface with a single function, those can be replaced by lambda expressions, but when you have a state field or implementing more than one interface, you cannot use lambdas to replace the anonymous ...

Which is better lambda expression or anonymous inner class?

Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code. An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.

Does it make sense to replace lambda expression with method references?

If you have been coding in Java 8 then you may know that using method reference in place of lambda expression makes your code more readable, hence it is advised to replace lambda expression with method reference wherever possible.

How do I change lambda in IntelliJ?

Then, with IntelliJ IDEA, PyCharm, WebStorm, or JetBrains Rider already running, do one of the following: Open AWS Explorer, if it isn't already open. If you need to switch to a different AWS Region to create the function in, do that now. Then right-click Lambda, and choose Create new AWS Lambda.


1 Answers

In the Analyze menu, select "Run Inspection by Name...". In the search box, type "Anonymous" and select the one that says "Anonymous class may be replaced by lambda" or something to that effect. Select your scope and start the analysis.

In the results, you can inspect them individually and click the "Replace with lambda" link on the right to make the change, or, if you are brave, you can right click on the node at the top of the results and select the "apply fix" to update all of them.

like image 93
Hank D Avatar answered Sep 23 '22 15:09

Hank D