From 97f0a30f9364c0a8cf04fe2c18109732ce87858b Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Tue, 7 Aug 2012 17:40:35 +0400 Subject: [PATCH] IDEA-89475 Inline method with varargs loses varargs --- .../refactoring/GroovyRefactoringUtil.java | 3 +- .../GrChangeSignatureUsageProcessor.java | 3 + ...ethodTest.java => InlineMethodTest.groovy} | 94 +++++++++---------- .../inlineMethod/inlineVarargs.test | 18 ++++ 4 files changed, 64 insertions(+), 54 deletions(-) rename plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/{InlineMethodTest.java => InlineMethodTest.groovy} (74%) create mode 100644 plugins/groovy/testdata/groovy/refactoring/inlineMethod/inlineVarargs.test diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java index ade42083d029..43c6e3dfb94f 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/GroovyRefactoringUtil.java @@ -531,7 +531,6 @@ public abstract class GroovyRefactoringUtil { argText.append("["); for (PsiElement argument : arguments) { argText.append(argument.getText()).append(", "); - argument.delete(); } if (arguments.size() > 0) { argText.delete(argText.length() - 2, argText.length()); @@ -547,7 +546,7 @@ public abstract class GroovyRefactoringUtil { } return GroovyPsiElementFactory.getInstance(project).createExpressionFromText(argText.toString()); } - + public static boolean hasSideEffect(@NotNull GroovyPsiElement statement) { final Ref hasSideEffect = new Ref(false); statement.accept(new GroovyRecursiveElementVisitor() { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java index fb1a2d0f04c7..45c03785f339 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/changeSignature/GrChangeSignatureUsageProcessor.java @@ -516,6 +516,9 @@ public class GrChangeSignatureUsageProcessor implements ChangeSignatureUsageProc (i != parameters.length - 1 || !parameter.isVarargType())) { final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager()); final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject()); + for (PsiElement argument : arguments) { + argument.delete(); + } anchor = argumentList.addAfter(arg, anchor); GrReferenceAdjuster.shortenReferences(anchor); } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.groovy similarity index 74% rename from plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java rename to plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.groovy index dd062fd96550..a2a29ecb32a7 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.groovy @@ -13,39 +13,33 @@ * limitations under the License. */ -package org.jetbrains.plugins.groovy.refactoring.inline; - -import com.intellij.lang.ASTNode; -import com.intellij.lang.refactoring.InlineHandler; -import com.intellij.openapi.editor.Editor; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.impl.source.codeStyle.CodeEditUtil; -import com.intellij.psi.impl.source.tree.TreeElement; -import com.intellij.refactoring.inline.GenericInlineHandler; -import com.intellij.refactoring.util.CommonRefactoringUtil; -import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; -import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; -import junit.framework.Assert; -import org.jetbrains.plugins.groovy.GroovyFileType; -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; -import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil; -import org.jetbrains.plugins.groovy.util.TestUtils; - -import java.util.List; +package org.jetbrains.plugins.groovy.refactoring.inline +import com.intellij.lang.ASTNode +import com.intellij.lang.refactoring.InlineHandler +import com.intellij.openapi.editor.Editor +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.impl.source.codeStyle.CodeEditUtil +import com.intellij.psi.impl.source.tree.TreeElement +import com.intellij.refactoring.inline.GenericInlineHandler +import com.intellij.refactoring.util.CommonRefactoringUtil +import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import junit.framework.Assert +import org.jetbrains.plugins.groovy.GroovyFileType +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression +import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil +import org.jetbrains.plugins.groovy.util.TestUtils /** * @author ilyas */ public class InlineMethodTest extends LightCodeInsightFixtureTestCase { - @Override - protected String getBasePath() { - return TestUtils.getTestDataPath() + "groovy/refactoring/inlineMethod/"; - } + final String basePath = TestUtils.testDataPath + "groovy/refactoring/inlineMethod/"; public void testAbstr1() throws Throwable { doTest(); } public void testBlock1() throws Throwable { doTest(); } @@ -105,31 +99,27 @@ public class InlineMethodTest extends LightCodeInsightFixtureTestCase { public void testVarargs() {doTest();} - public void testInlineAll() throws Throwable { + public void testInlineAll() { doTest(new GroovyInlineHandler() { @Override - public Settings prepareInlineElement(PsiElement element, Editor editor, boolean invokedOnReference) { - return new Settings() { + public InlineHandler.Settings prepareInlineElement(PsiElement element, Editor editor, boolean invokedOnReference) { + return new InlineHandler.Settings() { @Override - public boolean isOnlyOneReferenceToInline() { - return false; - } - }; + boolean isOnlyOneReferenceToInline() {false} + } } - }); + }) } - public void testInlineNamedArgs() { - doTest(); - } + public void testInlineNamedArgs() {doTest(); } + public void testInlineVarargs() {doTest()} protected void doTest() { doTest(new GroovyInlineHandler()); - } protected void doTest(InlineHandler handler) { - doInlineTest(myFixture, getTestDataPath() + getTestName(true) + ".test", handler); + doInlineTest(myFixture, testDataPath + getTestName(true) + ".test", handler); } public static void doInlineTest(final JavaCodeInsightTestFixture fixture, @@ -140,32 +130,32 @@ public class InlineMethodTest extends LightCodeInsightFixtureTestCase { fixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, fileText); - final PsiFile file = fixture.getFile(); - final Editor editor = fixture.getEditor(); - setIndentationToNode(file.getNode()); - int startOffset = editor.getSelectionModel().getSelectionStart(); - int endOffset = editor.getSelectionModel().getSelectionEnd(); - editor.getCaretModel().moveToOffset(endOffset); + final PsiFile file = fixture.file; + final Editor editor = fixture.editor; + indentationToNode = file.node; + int startOffset = editor.selectionModel.selectionStart; + int endOffset = editor.selectionModel.selectionEnd; + editor.caretModel.moveToOffset(endOffset); GroovyPsiElement selectedArea = GroovyRefactoringUtil.findElementInRange(file, startOffset, endOffset, GrReferenceExpression.class); if (selectedArea == null) { PsiElement identifier = GroovyRefactoringUtil.findElementInRange(file, startOffset, endOffset, PsiElement.class); if (identifier != null){ - Assert.assertTrue("Selected area doesn't point to method", identifier.getParent() instanceof GrVariable); - selectedArea = (GroovyPsiElement)identifier.getParent(); + Assert.assertTrue("Selected area doesn't point to method", identifier.parent instanceof GrVariable); + selectedArea = (GroovyPsiElement)identifier.parent; } } Assert.assertNotNull("Selected area reference points to nothing", selectedArea); - PsiElement element = selectedArea instanceof GrExpression ? selectedArea.getReference().resolve() : selectedArea; + PsiElement element = selectedArea instanceof GrExpression ? selectedArea.reference.resolve() : selectedArea; Assert.assertNotNull("Cannot resolve selected reference expression", element); try { GenericInlineHandler.invoke(element, editor, inlineHandler); - editor.getSelectionModel().removeSelection(); + editor.selectionModel.removeSelection(); fixture.checkResult(data.get(1), true); } catch (CommonRefactoringUtil.RefactoringErrorHintException e) { - assertEquals(data.get(1), "FAIL: " + e.getMessage()); + assertEquals(data.get(1), "FAIL: " + e.message); } } @@ -174,7 +164,7 @@ public class InlineMethodTest extends LightCodeInsightFixtureTestCase { CodeEditUtil.setOldIndentation(((TreeElement) element), 0); } for (ASTNode node : element.getChildren(null)) { - setIndentationToNode(node); + indentationToNode = node; } } diff --git a/plugins/groovy/testdata/groovy/refactoring/inlineMethod/inlineVarargs.test b/plugins/groovy/testdata/groovy/refactoring/inlineMethod/inlineVarargs.test new file mode 100644 index 000000000000..109044728d2d --- /dev/null +++ b/plugins/groovy/testdata/groovy/refactoring/inlineMethod/inlineVarargs.test @@ -0,0 +1,18 @@ +void foo(String... s) { + println 'hello' + bar(s) +} + +void bar(String... s) {} + +foo("foo", "bar") +----- +void foo(String... s) { + println 'hello' + bar(s) +} + +void bar(String... s) {} + +println 'hello' +bar(["foo", "bar"] as java.lang.String[]) \ No newline at end of file