[java-intentions] DefineParamsDefaultValueAction: add cast by default when ambiguous overloads are present

GitOrigin-RevId: 10d5c9f66fa5bef5ab5e5be71987d14757fd07e2
This commit is contained in:
Tagir Valeev
2024-04-30 15:48:37 +02:00
committed by intellij-monorepo-bot
parent c7ecd0b078
commit 860bc0e5e7
7 changed files with 45 additions and 8 deletions

View File

@@ -302,7 +302,7 @@ public final class TypeUtils {
* @return the textual representation of default value
*/
@NonNls
public static String getDefaultValue(PsiType type) {
public static @NotNull String getDefaultValue(PsiType type) {
if (PsiTypes.intType().equals(type)) {
return "0";
}

View File

@@ -6,6 +6,7 @@ import com.intellij.codeInsight.generation.RecordConstructorMember;
import com.intellij.codeInsight.intention.PriorityAction;
import com.intellij.codeInsight.intention.impl.ParameterClassMember;
import com.intellij.codeInsight.template.impl.TextExpression;
import com.intellij.codeInspection.redundantCast.RemoveRedundantCastUtil;
import com.intellij.icons.AllIcons;
import com.intellij.java.JavaBundle;
import com.intellij.lang.java.JavaLanguage;
@@ -16,10 +17,7 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.JavaFeature;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.JavaElementKind;
import com.intellij.psi.util.JavaPsiRecordUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.CommonJavaRefactoringUtil;
import com.intellij.util.containers.ContainerUtil;
@@ -109,7 +107,11 @@ public final class DefineParamsDefaultValueAction extends PsiBasedModCommandActi
PsiCodeBlock body = prototype.getBody();
final String callArgs =
"(" + StringUtil.join(parameterList.getParameters(), psiParameter -> {
if (ArrayUtil.find(parameters, psiParameter) > -1) return TypeUtils.getDefaultValue(psiParameter.getType());
if (ArrayUtil.find(parameters, psiParameter) > -1) {
PsiType type = psiParameter.getType();
String defaultValue = TypeUtils.getDefaultValue(type);
return defaultValue.equals(PsiKeyword.NULL) ? "(" + type.getCanonicalText() + ")null" : defaultValue;
}
return psiParameter.getName();
}, ",") + ");";
final String methodCall;
@@ -141,7 +143,10 @@ public final class DefineParamsDefaultValueAction extends PsiBasedModCommandActi
PsiExpression[] toDefaults =
ContainerUtil.map2Array(parameters, PsiExpression.class, (parameter -> args[parameterList.getParameterIndex(parameter)]));
ModTemplateBuilder builder = updater.templateBuilder();
for (final PsiExpression exprToBeDefault : toDefaults) {
for (PsiExpression exprToBeDefault : toDefaults) {
if (exprToBeDefault instanceof PsiTypeCastExpression cast && RedundantCastUtil.isCastRedundant(cast)) {
exprToBeDefault = RemoveRedundantCastUtil.removeCast(cast);
}
builder.field(exprToBeDefault, new TextExpression(exprToBeDefault.getText()));
}
}

View File

@@ -0,0 +1,10 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
void method() {
method((String) null);
}
void method(String s) {}
void method(Integer i) {}
}

View File

@@ -0,0 +1,10 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
void method() {
method(null, (Integer) null);
}
void method(String s, Integer i) {}
void method(Integer i, Double d) {}
}

View File

@@ -1,7 +1,7 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
<T> int foo(boolean... args) {
return foo(<selection>null<caret></selection>, args);
return foo(<selection>(T) null<caret></selection>, args);
}
<T> int foo(T ii, boolean... args){

View File

@@ -0,0 +1,6 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
void m<caret>ethod(String s) {}
void method(Integer i) {}
}

View File

@@ -0,0 +1,6 @@
// "Generate overloaded method with default parameter values" "true"
class Test {
void m<caret>ethod(String s, Integer i) {}
void method(Integer i, Double d) {}
}