mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
change signature from usage fix: varargs processing (IDEA-58449)
This commit is contained in:
+22
-1
@@ -238,6 +238,7 @@ public class ChangeMethodSignatureFromUsageFix implements IntentionAction {
|
||||
}
|
||||
int ei = 0;
|
||||
int pi = 0;
|
||||
PsiParameter varargParam = targetMethod.isVarArgs() ? parameters[parameters.length - 1] : null;
|
||||
while (ei < expressions.length || pi < parameters.length) {
|
||||
PsiExpression expression = ei < expressions.length ? expressions[ei] : null;
|
||||
PsiParameter parameter = pi < parameters.length ? parameters[pi] : null;
|
||||
@@ -248,7 +249,16 @@ public class ChangeMethodSignatureFromUsageFix implements IntentionAction {
|
||||
pi++;
|
||||
ei++;
|
||||
}
|
||||
else if (isArgumentInVarargPosition(expressions, ei, varargParam, substitutor)) {
|
||||
if (pi == parameters.length - 1) {
|
||||
assert varargParam != null;
|
||||
result.add(new ParameterInfoImpl(pi, varargParam.getName(), varargParam.getType()));
|
||||
}
|
||||
pi++;
|
||||
ei++;
|
||||
}
|
||||
else if (expression != null) {
|
||||
if (varargParam != null && pi >= parameters.length) return null;
|
||||
PsiType exprType = RefactoringUtil.getTypeByExpression(expression);
|
||||
if (exprType == null) return null;
|
||||
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
|
||||
@@ -257,7 +267,7 @@ public class ChangeMethodSignatureFromUsageFix implements IntentionAction {
|
||||
ei++;
|
||||
}
|
||||
}
|
||||
if (result.size() != expressions.length) return null;
|
||||
if (result.size() != expressions.length && varargParam == null) return null;
|
||||
}
|
||||
else {
|
||||
//parameter type changed
|
||||
@@ -291,6 +301,17 @@ public class ChangeMethodSignatureFromUsageFix implements IntentionAction {
|
||||
return result.toArray(new ParameterInfoImpl[result.size()]);
|
||||
}
|
||||
|
||||
private static boolean isArgumentInVarargPosition(PsiExpression[] expressions, int ei, PsiParameter varargParam, PsiSubstitutor substitutor) {
|
||||
if (varargParam == null) return false;
|
||||
final PsiExpression expression = expressions[ei];
|
||||
if (expression == null || TypeConversionUtil.areTypesAssignmentCompatible(substitutor.substitute(((PsiEllipsisType)varargParam.getType()).getComponentType()), expression)) {
|
||||
final int lastExprIdx = expressions.length - 1;
|
||||
if (ei == lastExprIdx) return true;
|
||||
return expressions[lastExprIdx].getType() != PsiType.NULL;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String suggestUniqueParameterName(JavaCodeStyleManager codeStyleManager,
|
||||
PsiExpression expression,
|
||||
PsiType exprType,
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Change signature of 'f(int...)' to 'f(String, int...)'" "true"
|
||||
public class S {
|
||||
void f(String s, int... args) {
|
||||
f("",1,1);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Change signature of 'f(int...)' to 'f(String, int...)'" "true"
|
||||
public class S {
|
||||
void f(String s, int... args) {
|
||||
f("",null);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Change signature of 'f(int...)' to 'f(String, int...)'" "true"
|
||||
public class S {
|
||||
void f(int... args) {
|
||||
f("",1,1)<caret>;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Change signature of 'f(int...)' to 'f(String, int...)'" "true"
|
||||
public class S {
|
||||
void f(int... args) {
|
||||
f("",null)<caret>;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Change signature of 'f(int...)' to 'f(int..., String)'" "false"
|
||||
public class S {
|
||||
void f(int... args) {
|
||||
f(1,1, "")<caret>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user