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?
Asked
Active
Viewed 8,737 times
32
-
2Not all anonymous classes can be represented as lambdas. – bcsb1001 Apr 20 '16 at 10:20
-
Ok. How about some of them which can be represented? – mahdi_12167 Apr 20 '16 at 10:30
-
I don't think there is such a feature out of the box. Maybe some plugin – AdrianS Apr 20 '16 at 11:57
-
https://www.jetbrains.com/help/idea/2016.1/tutorial-migrating-to-java-8.html?origin=old_help#d359489e196 – Holger Apr 20 '16 at 12:33
1 Answers
63
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.

Hank D
- 6,271
- 2
- 26
- 35
-
5in my case `Anonymous class may be replaced by lambda` is not found, but instead `Anonymous type can be replaced with lambda` – HendraWD Sep 12 '17 at 06:59
-
This can also be run in the opposite direction (converting lambdas to anonymous classes) by searching for `Lambda can be replaced with anonymous class`. – jcady Sep 07 '18 at 20:41