From 47262762d20b9346f6209e17967f09e8b44aa106 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 23 Aug 2013 20:27:18 +0400 Subject: [PATCH] extract method from lambda body: accept parameters of parent method (IDEA-112570) --- .../controlFlow/LocalsControlFlowPolicy.java | 2 +- .../extractMethod/FromLambdaBody1.java | 22 ++++++++++++++++ .../extractMethod/FromLambdaBody1_after.java | 26 +++++++++++++++++++ .../refactoring/ExtractMethodTest.java | 4 +++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1.java create mode 100644 java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1_after.java diff --git a/java/java-psi-impl/src/com/intellij/psi/controlFlow/LocalsControlFlowPolicy.java b/java/java-psi-impl/src/com/intellij/psi/controlFlow/LocalsControlFlowPolicy.java index 333ed4c45312..b5c0f8ef40a9 100644 --- a/java/java-psi-impl/src/com/intellij/psi/controlFlow/LocalsControlFlowPolicy.java +++ b/java/java-psi-impl/src/com/intellij/psi/controlFlow/LocalsControlFlowPolicy.java @@ -47,7 +47,7 @@ public class LocalsControlFlowPolicy implements ControlFlowPolicy { } if (codeFragment == null) return null; if (myCodeFragment.getContainingFile() == codeFragment.getContainingFile() && // in order for jsp includes to work - !myCodeFragment.equals(codeFragment)) { + !myCodeFragment.equals(codeFragment) && !(myCodeFragment.getParent() instanceof PsiLambdaExpression)) { return null; } return (PsiVariable)refElement; diff --git a/java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1.java b/java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1.java new file mode 100644 index 000000000000..fcb14a849657 --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1.java @@ -0,0 +1,22 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +class Test { + public void foo(int ii) { + Runnable r = () -> { + System.out.println(ii); + }; + } +} diff --git a/java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1_after.java b/java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1_after.java new file mode 100644 index 000000000000..6debfdee24db --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/FromLambdaBody1_after.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +class Test { + public void foo(int ii) { + Runnable r = () -> { + newMethod(ii); + }; + } + + private void newMethod(int ii) { + System.out.println(ii); + } +} diff --git a/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java b/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java index b56646516390..07002255014a 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java @@ -547,6 +547,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase { doTest(); } + public void testFromLambdaBody1() throws Exception { + doTest(); + } + public void testOneLineLambda() throws Exception { doTest(); }