mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: inline method parameter for vararg parameters (IDEA-243062)
GitOrigin-RevId: 776d18524b6ec5c4eeec7f7bd71c01b768370aca
This commit is contained in:
committed by
intellij-monorepo-bot
parent
42e77f6682
commit
d1654ab5dc
+3
-2
@@ -96,7 +96,7 @@ public class InlineParameterExpressionProcessor extends BaseRefactoringProcessor
|
||||
final List<UsageInfo> result = new ArrayList<>();
|
||||
myInitializer.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitReferenceExpression(final @NotNull PsiReferenceExpression expression) {
|
||||
public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
|
||||
super.visitReferenceExpression(expression);
|
||||
final PsiElement element = expression.resolve();
|
||||
if (element instanceof PsiLocalVariable localVariable) {
|
||||
@@ -264,7 +264,8 @@ public class InlineParameterExpressionProcessor extends BaseRefactoringProcessor
|
||||
}
|
||||
else {
|
||||
for (PsiJavaCodeReferenceElement paramRef : paramRefsToInline) {
|
||||
InlineUtil.inlineVariable(myParameter, myInitializer, paramRef);
|
||||
PsiExpression expression = InlineUtil.inlineVariable(myParameter, myInitializer, paramRef);
|
||||
CommonJavaRefactoringUtil.tryToInlineArrayCreationForVarargs(expression);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-6
@@ -27,6 +27,7 @@ import com.intellij.refactoring.listeners.RefactoringEventListener;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.InlineUtil;
|
||||
import com.intellij.refactoring.util.RefactoringMessageDialog;
|
||||
import com.intellij.util.CommonJavaRefactoringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -52,7 +53,7 @@ public final class InlineParameterHandler extends JavaInlineActionHandler {
|
||||
}
|
||||
final int index = parameterList.getParameterIndex(psiParameter);
|
||||
|
||||
String errorMessage = getCannotInlineMessage(psiParameter, method);
|
||||
String errorMessage = getCannotInlineMessage(method);
|
||||
if (errorMessage != null) {
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, errorMessage, JavaRefactoringBundle.message("inline.parameter.refactoring"), null);
|
||||
return;
|
||||
@@ -72,7 +73,7 @@ public final class InlineParameterHandler extends JavaInlineActionHandler {
|
||||
if (parent instanceof PsiCallExpression methodCall) {
|
||||
occurrences.add(psiReference);
|
||||
containingFiles.add(element.getContainingFile());
|
||||
final PsiExpression[] expressions = methodCall.getArgumentList().getExpressions();
|
||||
final PsiExpression[] expressions = CommonJavaRefactoringUtil.getNonVarargArguments(methodCall);
|
||||
if (expressions.length <= index) return false;
|
||||
PsiExpression argument = expressions[index];
|
||||
if (!refInitializer.isNull()) {
|
||||
@@ -251,10 +252,7 @@ public final class InlineParameterHandler extends JavaInlineActionHandler {
|
||||
return value1 != null && value1.equals(value2);
|
||||
}
|
||||
|
||||
private static @Nullable @NlsContexts.DialogMessage String getCannotInlineMessage(PsiParameter psiParameter, PsiMethod method) {
|
||||
if (psiParameter.isVarArgs()) {
|
||||
return JavaRefactoringBundle.message("inline.parameter.error.varargs");
|
||||
}
|
||||
private static @Nullable @NlsContexts.DialogMessage String getCannotInlineMessage(PsiMethod method) {
|
||||
if (method.findSuperMethods().length > 0 || OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY).length > 0) {
|
||||
return JavaRefactoringBundle.message("inline.parameter.error.hierarchy");
|
||||
}
|
||||
|
||||
@@ -166,6 +166,9 @@ public final class InlineUtil implements CommonJavaInlineUtil {
|
||||
else if (typeElement.isInferredType()) {
|
||||
return expr;
|
||||
}
|
||||
if (typeElement.getType() instanceof PsiEllipsisType type) {
|
||||
typeElement = factory.createTypeElement(type.toArrayType());
|
||||
}
|
||||
castTypeElement.replace(typeElement);
|
||||
final PsiExpression operand = cast.getOperand();
|
||||
assert operand != null;
|
||||
|
||||
@@ -997,8 +997,7 @@ public final class CommonJavaRefactoringUtil {
|
||||
}
|
||||
final PsiType substitutedLastParamType = substitutor.substitute(((PsiEllipsisType)lastParamType).toArrayType());
|
||||
final PsiType lastArgType = lastArg.getType();
|
||||
if (lastArgType == null || !lastArgType.equals(substitutedLastParamType) &&
|
||||
!lastArgType.equals(TypeConversionUtil.erasure(substitutedLastParamType))) {
|
||||
if (lastArgType == null || !substitutedLastParamType.isAssignableFrom(lastArgType)) {
|
||||
return null;
|
||||
}
|
||||
PsiExpression[] initializers = getInitializers(newExpression);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class ValidSubtype {
|
||||
|
||||
public static void main(String[] args) {
|
||||
x(<warning descr="Redundant array creation for calling varargs method">new String[]</warning>{"firstly", "secondly", "finally"});
|
||||
}
|
||||
|
||||
static void x(Object... os) {}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(use(new Object[]{"1", "2", "3"}));
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(use());
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
public enum InlineVararg {
|
||||
A, B;
|
||||
|
||||
private static void toInline(InlineVararg... args<caret>) {
|
||||
System.out.println(Arrays.asList(args));
|
||||
}
|
||||
|
||||
public static void call() {
|
||||
toInline(InlineVararg.values());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
public enum InlineVararg {
|
||||
A, B;
|
||||
|
||||
private static void toInline() {
|
||||
System.out.println(Arrays.asList(InlineVararg.values()));
|
||||
}
|
||||
|
||||
public static void call() {
|
||||
toInline();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(use("1", "2", "3"));
|
||||
}
|
||||
|
||||
public static String use(String... os<caret>) {
|
||||
x(os);
|
||||
return Arrays.toString(os);
|
||||
}
|
||||
|
||||
static void x(Object... os) {
|
||||
for (Object o : os) {
|
||||
System.out.println(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(use());
|
||||
}
|
||||
|
||||
public static String use() {
|
||||
x("1", "2", "3");
|
||||
return Arrays.toString(new String[]{"1", "2", "3"});
|
||||
}
|
||||
|
||||
static void x(Object... os) {
|
||||
for (Object o : os) {
|
||||
System.out.println(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-15
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2017 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.
|
||||
*/
|
||||
// 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.java.codeInspection;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
@@ -40,4 +26,5 @@ public class RedundantArrayForVarargsCallInspectionTest extends LightJavaInspect
|
||||
public void testGeneric() { doTest(); }
|
||||
public void testRawArray() { doTest(); }
|
||||
public void testPolymorphicSignature() { doTest(); }
|
||||
public void testValidSubtype() { doTest(); }
|
||||
}
|
||||
|
||||
+4
-4
@@ -337,10 +337,10 @@ public class InlineParameterTest extends LightRefactoringTestCase {
|
||||
assertEquals("Method <b><code>doTest()</code></b> is already defined in class <b><code>Test</code></b>", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testArrayInitializer() {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
public void testInlineVararg() { doTest(false); }
|
||||
public void testVarargs() { doTest(false); }
|
||||
public void testArrayInitializer() { doTest(false); }
|
||||
|
||||
private void doTest(boolean createLocal) {
|
||||
getProject().putUserData(InlineParameterExpressionProcessor.CREATE_LOCAL_FOR_TESTS, createLocal);
|
||||
|
||||
Reference in New Issue
Block a user