make add cast fix available for vararg methods (IDEA-169541)

This commit is contained in:
Anna.Kozlova
2017-03-14 11:05:12 +01:00
parent 589c139797
commit 1e9f6352f2
8 changed files with 57 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.util.IncorrectOperationException;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
@@ -54,11 +55,11 @@ public abstract class ArgumentFixerActionFactory {
PsiMethod method = (PsiMethod) candidate.getElement();
PsiSubstitutor substitutor = candidate.getSubstitutor();
PsiParameter[] parameters = method.getParameterList().getParameters();
if (expressions.length != parameters.length) {
if (expressions.length != parameters.length && !method.isVarArgs()) {
methodCandidates.remove(i);
continue;
}
for (int j = 0; j < parameters.length; j++) {
for (int j = 0; j < Math.min(parameters.length, expressions.length); j++) {
PsiParameter parameter = parameters[j];
PsiExpression expression = expressions[j];
// check if we can cast to this method
@@ -84,8 +85,7 @@ public abstract class ArgumentFixerActionFactory {
for (CandidateInfo candidate : methodCandidates) {
PsiMethod method = (PsiMethod)candidate.getElement();
PsiSubstitutor substitutor = candidate.getSubstitutor();
PsiParameter[] parameters = method.getParameterList().getParameters();
PsiType originalParameterType = parameters[i].getType();
PsiType originalParameterType = PsiTypesUtil.getParameterType(method.getParameterList().getParameters(), i, true);
PsiType parameterType = substitutor.substitute(originalParameterType);
if (parameterType instanceof PsiWildcardType) continue;
if (!GenericsUtil.isFromExternalTypeLanguage(parameterType)) continue;

View File

@@ -67,7 +67,11 @@ public class CastMethodArgumentFix extends MethodArgumentFix implements HighPrio
parameterType = PsiPrimitiveType.getUnboxedType(parameterType);
if (parameterType == null) return false;
}
return parameterType.isConvertibleFrom(exprType);
if (parameterType.isConvertibleFrom(exprType)) {
return true;
}
return parameterType instanceof PsiEllipsisType && areTypesConvertible(exprType, ((PsiEllipsisType)parameterType).getComponentType(), context);
}
}

View File

@@ -0,0 +1,8 @@
// "Cast parameter to 'long'" "true"
class a {
void f(Long l, String... s) {}
void g() {
f((long) 0);
}
}

View File

@@ -0,0 +1,8 @@
// "Cast 1st parameter to 'long'" "true"
class a {
void f(Long l, String... s) {}
void g() {
f((long) 0, "", "");
}
}

View File

@@ -0,0 +1,8 @@
// "Cast 1st parameter to 'long'" "true"
class a {
void f(Long l, String... s) {}
void g() {
f((long) 0, "");
}
}

View File

@@ -0,0 +1,8 @@
// "Cast parameter to 'long'" "true"
class a {
void f(Long l, String... s) {}
void g() {
f(<caret>0);
}
}

View File

@@ -0,0 +1,8 @@
// "Cast 1st parameter to 'long'" "true"
class a {
void f(Long l, String... s) {}
void g() {
f(<caret>0, "", "");
}
}

View File

@@ -0,0 +1,8 @@
// "Cast 1st parameter to 'long'" "true"
class a {
void f(Long l, String... s) {}
void g() {
f(<caret>0, "");
}
}