Community
Participate
Working Groups
Since a lambda expression is like a method, it should be possible to extract local variables within its body. Consider the following example and try to extract a local variable from the lines with comments, we get different error messages: @FunctionalInterface interface FI { int foo(int a); } public class TestExtractLocalVariable { FI fi1= (a) -> a + 10; // Error FI fi2= (int a) -> { int b= a + 10; // Error return b; }; void bar(FI fi) { FI fi1= (a) -> a + 10; // Error, not extracted within lambda body FI fi2= (a) -> { int b= a + 10; // Works here return b; }; } }
Created attachment 247437 [details] Patch Attached fix and tests to extract local variable in lambda expression's body. Markus, please have a look.
Looks good, thanks. Nits: * The separate message constant is not really necessary, since the addition of "lambda expression" is not wrong for earlier versions of Java. I'd favor less code and less messages over a sightly more concise message for a soon-to-be-EOL'd language version. * Better use Bindings.isVoidType(..) instead of ast.resolveWellKnownType("void").isEqualTo(..) * The double casts here are not too nice: SimpleName varName= ((VariableDeclarationFragment) ((VariableDeclarationStatement) replacement).fragments().get(0)).getName(); Better extract "createTempDeclaration(initializer)", remove the comment, and use the new local variable that already has the right type. The other cast can be avoided by extracting this local variable: List<VariableDeclarationFragment> fragments= tempDeclaration.fragments(); This works because the jdt.ui project has the "Ignore unavoidable generic type problems due to raw APIs" warning enabled.
(In reply to Markus Keller from comment #2) > This works because the jdt.ui project has the "Ignore unavoidable generic > type problems due to raw APIs" warning enabled. I meant: ... the ... *option* enabled. That option can fix "type safety" problems, but it cannot insert casts, since that would make the compiler deviate from the JLS.
Thanks Markus. Updated the patch as per review comments and released as: http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=4f588b97a34c99264ee85309e6786c822be612ed