create enum constant: support constant creation when static import is used, process varargs method candidates as vararg/basic separately (IDEA-121287)

This commit is contained in:
Anna Kozlova
2014-03-28 20:31:52 +01:00
parent 1cf32d2b3d
commit 123ba34507
8 changed files with 137 additions and 4 deletions
@@ -529,7 +529,7 @@ public class ExpectedTypesProvider {
PsiResolveHelper helper = JavaPsiFacade.getInstance(list.getProject()).getResolveHelper();
if (list.getParent() instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodCall = (PsiMethodCallExpression)list.getParent();
CandidateInfo[] candidates = helper.getReferencedMethodCandidates(methodCall, false);
CandidateInfo[] candidates = helper.getReferencedMethodCandidates(methodCall, false, true);
Collections.addAll(myResult, getExpectedArgumentTypesForMethodCall(candidates, list, myExpr, myForCompletion));
}
else if (list.getParent() instanceof PsiEnumConstant) {
@@ -28,9 +28,11 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class CreateEnumConstantFromUsageFix extends CreateVarFromUsageFix implements HighPriorityAction{
@@ -87,6 +89,34 @@ public class CreateEnumConstantFromUsageFix extends CreateVarFromUsageFix implem
}
}
@NotNull
@Override
protected List<PsiClass> getTargetClasses(PsiElement element) {
final List<PsiClass> classes = super.getTargetClasses(element);
PsiClass enumClass = null;
for (PsiClass aClass : classes) {
if (aClass.isEnum()) {
if (enumClass == null) {
enumClass = aClass;
} else {
enumClass = null;
break;
}
}
}
if (enumClass != null) {
return Collections.singletonList(enumClass);
}
ExpectedTypeInfo[] typeInfos = CreateFromUsageUtils.guessExpectedTypes(myReferenceExpression, false);
for (final ExpectedTypeInfo typeInfo : typeInfos) {
final PsiClass psiClass = PsiUtil.resolveClassInClassTypeOnly(typeInfo.getType());
if (psiClass != null && psiClass.isEnum()) {
return Collections.singletonList(psiClass);
}
}
return Collections.emptyList();
}
@Override
protected boolean isAvailableImpl(int offset) {
@@ -80,6 +80,18 @@ public interface PsiResolveHelper {
@NotNull
CandidateInfo[] getReferencedMethodCandidates(@NotNull PsiCallExpression call, boolean dummyImplicitConstructor);
/**
* Resolves a call expression and returns an array of possible resolve results.
*
* @param call the call expression to resolve.
* @param dummyImplicitConstructor if true, implicit empty constructor which does not actually exist
* can be returned as a candidate for the resolve.
* @param checkVarargs true if varargs method should lead to 2 candidates in the result array
* @return the array of resolve results.
*/
@NotNull
CandidateInfo[] getReferencedMethodCandidates(@NotNull PsiCallExpression call, boolean dummyImplicitConstructor, boolean checkVarargs);
/**
* Resolves a reference to a class, given the text of the reference and the context
* in which it was encountered.
@@ -127,11 +127,18 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
return JavaResolveUtil.isAccessible(member, containingClass, modifierList, place, accessObjectClass, currentFileResolveScope);
}
@Override
@NotNull
public CandidateInfo[] getReferencedMethodCandidates(@NotNull PsiCallExpression expr, boolean dummyImplicitConstructor) {
@Override
public CandidateInfo[] getReferencedMethodCandidates(@NotNull PsiCallExpression expr,
boolean dummyImplicitConstructor,
final boolean checkVarargs) {
PsiFile containingFile = expr.getContainingFile();
final MethodCandidatesProcessor processor = new MethodCandidatesProcessor(expr, containingFile);
final MethodCandidatesProcessor processor = new MethodCandidatesProcessor(expr, containingFile) {
@Override
protected boolean acceptVarargs() {
return checkVarargs;
}
};
try {
PsiScopesUtil.setupAndRunProcessor(processor, expr, dummyImplicitConstructor);
}
@@ -141,6 +148,12 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
return processor.getCandidates();
}
@NotNull
@Override
public CandidateInfo[] getReferencedMethodCandidates(@NotNull PsiCallExpression call, boolean dummyImplicitConstructor) {
return getReferencedMethodCandidates(call, dummyImplicitConstructor, false);
}
@Override
public PsiType inferTypeForMethodTypeParameter(@NotNull PsiTypeParameter typeParameter,
@NotNull PsiParameter[] parameters,
@@ -0,0 +1,21 @@
// "Create Enum Constant 'CRIME'" "true"
import java.util.Arrays;
import java.util.List;
import static AutomaticTypeInference.E.*;
class AutomaticTypeInference {
AutomaticTypeInference(List<E> gs) {
}
public static void main(String[] args) {
new AutomaticTypeInference(Arrays.asList(ACTION, CRIME));
}
enum E {
ACTION, CRIME;
}
}
@@ -0,0 +1,18 @@
// "Create Enum Constant 'CRIME'" "true"
import java.util.*;
class AutomaticTypeInference {
AutomaticTypeInference(List<E> gs) {
}
public static void main(String[] args) {
new AutomaticTypeInference(Arrays.asList(E.ACTION, E.CRIME));
}
enum E {
ACTION, CRIME;
}
}
@@ -0,0 +1,21 @@
// "Create Enum Constant 'CRIME'" "true"
import java.util.Arrays;
import java.util.List;
import static AutomaticTypeInference.E.*;
class AutomaticTypeInference {
AutomaticTypeInference(List<E> gs) {
}
public static void main(String[] args) {
new AutomaticTypeInference(Arrays.asList(ACTION, CRI<caret>ME));
}
enum E {
ACTION;
}
}
@@ -0,0 +1,18 @@
// "Create Enum Constant 'CRIME'" "true"
import java.util.*;
class AutomaticTypeInference {
AutomaticTypeInference(List<E> gs) {
}
public static void main(String[] args) {
new AutomaticTypeInference(Arrays.asList(E.ACTION, E.CRI<caret>ME));
}
enum E {
ACTION;
}
}