Java: allow to extract vararg arguments into method (IDEA-310454)

GitOrigin-RevId: ea061b03905f994b46b0339d9157dc693f8fbd84
This commit is contained in:
Bas Leijdekkers
2025-08-15 12:17:38 +00:00
committed by intellij-monorepo-bot
parent e713318cd5
commit a927fa7653
6 changed files with 40 additions and 8 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.extractMethod.newImpl
import com.intellij.codeInsight.Nullability
@@ -17,6 +17,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.platform.ide.progress.withBackgroundProgress
import com.intellij.psi.*
import com.intellij.psi.impl.source.PsiFileImpl
import com.intellij.psi.util.PsiEditorUtil
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.refactoring.JavaRefactoringSettings
@@ -130,7 +131,8 @@ class MethodExtractor {
val prepareStart = System.currentTimeMillis()
readAction { JavaDuplicatesFinder.linkCopiedClassMembersWithOrigin(file) }
val copiedFile = readAction { file.copy() as PsiFile }
val copiedFile = readAction { file.copy() as PsiFileImpl }
copiedFile.originalFile = file
val elementsInCopy = readAction { ExtractSelector().suggestElementsToExtract(copiedFile, range) }
val descriptorsForAllTargetPlaces = prepareDescriptorsForAllTargetPlaces(file.project, editor, elementsInCopy)
@@ -351,10 +351,14 @@ public final class IntroduceVariableUtil {
final PsiType componentType = TypeConversionUtil.erasure(psiSubstitutor.substitute(psiType.getComponentType()));
try {
int start = ArrayUtil.indexOf(args, startElement);
int end = ArrayUtil.indexOf(args, endElement);
if (start != parameters.length - 1 || end != args.length - 1) return null;
final PsiExpression expressionFromText =
elementFactory.createExpressionFromText("new " + componentType.getCanonicalText() + "[]{" + text + "}", parent);
final RangeMarker rangeMarker =
FileDocumentManager.getInstance().getDocument(containingFile.getVirtualFile()).createRangeMarker(startOffset, endOffset);
Document document = FileDocumentManager.getInstance().getDocument(containingFile.getOriginalFile().getVirtualFile());
if (document == null) return null;
final RangeMarker rangeMarker = document.createRangeMarker(startOffset, endOffset);
expressionFromText.putUserData(ElementToWorkOn.TEXT_RANGE, rangeMarker);
expressionFromText.putUserData(ElementToWorkOn.PARENT, parent);
return expressionFromText;
@@ -0,0 +1,8 @@
public class VarargsArguments {
public VarargsArguments(String ... strs) {}
public void test1(VarargsArguments o, String... foo) {}
void bar() {
test1(new VarargsArguments(<selection> "", ""</selection>));
}
}
@@ -0,0 +1,14 @@
import org.jetbrains.annotations.NotNull;
public class VarargsArguments {
public VarargsArguments(String ... strs) {}
public void test1(VarargsArguments o, String... foo) {}
void bar() {
test1(new VarargsArguments(newMethod()));
}
private String @NotNull [] newMethod() {
return new String[]{"", ""};
}
}
@@ -201,7 +201,7 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
try {
doTest();
fail("Should not work for assignment expression");
} catch (PrepareFailedException e){
} catch (PrepareFailedException ignored) {
}
}
@@ -273,7 +273,7 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
try {
doTest();
fail("Should not work for assignment expression");
} catch (PrepareFailedException e){
} catch (PrepareFailedException ignored) {
}
}
@@ -281,7 +281,7 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
try {
doTest();
fail("Should not work for assignment expression");
} catch (PrepareFailedException e){
} catch (PrepareFailedException ignored) {
}
}
@@ -818,6 +818,10 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase {
doDuplicatesTest();
}
public void testVarargsArguments() throws Exception {
doTest();
}
public void testParametersFromAnonymous() throws Exception {
doTest();
}
@@ -232,7 +232,7 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase {
public void testSubExpressionFromMagicString() { doTest("str", false, false, false, JAVA_LANG_STRING); }
public void testSubExpressionFromPrimitiveWithConversion() { doTest("i", false, false, false, "int"); }
public void testSubPrimitiveLiteral() { doTest("str", false, false, false, JAVA_LANG_STRING); }
public void testArrayFromVarargs() { doTest("strings", false, false, false, "java.lang.String[]"); }
public void testArrayFromVarargs() { doTestWithFailure("strings", "java.lang.String[]"); }
public void testArrayFromVarargs1() { doTest("strings", false, false, false, "java.lang.String[]"); }
public void testEnumArrayFromVarargs() { doTest("strings", false, false, false, "E[]"); }
public void testFromFinalFieldOnAssignment() { doTest("strings", false, false, false, JAVA_LANG_STRING); }