From 67178dfdf8cf050d3f68777f66ba8f7b40f36f7e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 20 Oct 2009 16:37:05 +0400 Subject: [PATCH] IDEA-25162: Groovy extract method can fail to propose parameters and create wrong code --- .../lang/psi/controlFlow/impl/ControlFlowBuilder.java | 11 ++++++++--- .../refactoring/extractMethod/ExtractMethodTest.java | 1 + .../groovy/refactoring/extractMethod/forIn.test | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 plugins/groovy/testdata/groovy/refactoring/extractMethod/forIn.test diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java index ea9b878059..099a7a3672 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/controlFlow/impl/ControlFlowBuilder.java @@ -230,7 +230,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { assertion.accept(this); final InstructionImpl assertInstruction = startNode(assertStatement); final PsiType type = JavaPsiFacade.getInstance(assertStatement.getProject()).getElementFactory() - .createTypeByFQClassName("java.lang.AssertionError", assertStatement.getResolveScope()); + .createTypeByFQClassName("java.lang.AssertionError", assertStatement.getResolveScope()); ExceptionInfo info = findCatch(type); if (info != null) { info.myThrowers.add(assertInstruction); @@ -294,7 +294,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { if (expression.getOperationToken() != GroovyElementTypes.mASSIGN) { if (lValue instanceof GrReferenceExpression) { ReadWriteVariableInstructionImpl instruction = - new ReadWriteVariableInstructionImpl((GrReferenceExpression)lValue, myInstructionNumber++, false); + new ReadWriteVariableInstructionImpl((GrReferenceExpression)lValue, myInstructionNumber++, false); addNode(instruction); checkPending(instruction); } @@ -339,7 +339,7 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { } else { final ReadWriteVariableInstructionImpl i = - new ReadWriteVariableInstructionImpl(referenceExpression, myInstructionNumber++, PsiUtil.isLValue(referenceExpression)); + new ReadWriteVariableInstructionImpl(referenceExpression, myInstructionNumber++, PsiUtil.isLValue(referenceExpression)); addNode(i); checkPending(i); } @@ -398,6 +398,11 @@ public class ControlFlowBuilder extends GroovyRecursiveElementVisitor { if (expression != null) { expression.accept(this); } + for (GrVariable variable : clause.getDeclaredVariables()) { + ReadWriteVariableInstructionImpl writeInsn = new ReadWriteVariableInstructionImpl(variable, myInstructionNumber++); + checkPending(writeInsn); + addNode(writeInsn); + } } InstructionImpl instruction = startNode(forStatement); diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/extractMethod/ExtractMethodTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/extractMethod/ExtractMethodTest.java index f084319739..16a582c612 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/extractMethod/ExtractMethodTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/extractMethod/ExtractMethodTest.java @@ -86,5 +86,6 @@ public class ExtractMethodTest extends LightGroovyTestCase { public void testVen1() throws Throwable { doTest(); } public void testVen2() throws Throwable { doTest(); } public void testVen3() throws Throwable { doTest(); } + public void testForIn() throws Throwable { doTest(); } } \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/refactoring/extractMethod/forIn.test b/plugins/groovy/testdata/groovy/refactoring/extractMethod/forIn.test new file mode 100644 index 0000000000..f431de7ea6 --- /dev/null +++ b/plugins/groovy/testdata/groovy/refactoring/extractMethod/forIn.test @@ -0,0 +1,19 @@ +void aaa(Map map) { + for (Map.Entry versionEntry in map.entrySet()) { + String name = versionEntry.getKey(); + System.out.println(name); + } +} +----- +import java.util.Map.Entry + +void aaa(Map map) { + for (Map.Entry versionEntry in map.entrySet()) { + testMethod(versionEntry); + } +} + +private def testMethod(Entry versionEntry) { + String name = versionEntry.getKey(); + System.out.println(name) +} -- 2.11.4.GIT