Search in sources :

Example 6 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class HighlightMethodUtil method registerMethodCallIntentions.

private static void registerMethodCallIntentions(@Nullable HighlightInfo highlightInfo, PsiMethodCallExpression methodCall, PsiExpressionList list, PsiResolveHelper resolveHelper) {
    TextRange fixRange = getFixRange(methodCall);
    final PsiExpression qualifierExpression = methodCall.getMethodExpression().getQualifierExpression();
    if (qualifierExpression instanceof PsiReferenceExpression) {
        final PsiElement resolve = ((PsiReferenceExpression) qualifierExpression).resolve();
        if (resolve instanceof PsiClass && ((PsiClass) resolve).getContainingClass() != null && !((PsiClass) resolve).hasModifierProperty(PsiModifier.STATIC)) {
            QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix((PsiClass) resolve, PsiModifier.STATIC, true, false));
        }
    }
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateMethodFromUsageFix(methodCall));
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateAbstractMethodFromUsageFix(methodCall));
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateConstructorFromSuperFix(methodCall));
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateConstructorFromThisFix(methodCall));
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreatePropertyFromUsageFix(methodCall));
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateGetterSetterPropertyFromUsageFix(methodCall));
    CandidateInfo[] methodCandidates = resolveHelper.getReferencedMethodCandidates(methodCall, false);
    CastMethodArgumentFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
    PermuteArgumentsFix.registerFix(highlightInfo, methodCall, methodCandidates, fixRange);
    AddTypeArgumentsFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
    WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
    WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
    WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
    WrapStringWithFileFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
    registerMethodAccessLevelIntentions(methodCandidates, methodCall, list, highlightInfo);
    registerChangeMethodSignatureFromUsageIntentions(methodCandidates, list, highlightInfo, fixRange);
    RemoveRedundantArgumentsFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
    ConvertDoubleToFloatFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
    WrapExpressionFix.registerWrapAction(methodCandidates, list.getExpressions(), highlightInfo);
    registerChangeParameterClassFix(methodCall, list, highlightInfo);
    if (methodCandidates.length == 0) {
        QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createStaticImportMethodFix(methodCall));
        QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.addMethodQualifierFix(methodCall));
    }
    for (IntentionAction action : QUICK_FIX_FACTORY.getVariableTypeFromCallFixes(methodCall, list)) {
        QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, action);
    }
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createReplaceAddAllArrayToCollectionFix(methodCall));
    QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createSurroundWithArrayFix(methodCall, null));
    QualifyThisArgumentFix.registerQuickFixAction(methodCandidates, methodCall, highlightInfo, fixRange);
    CandidateInfo[] candidates = resolveHelper.getReferencedMethodCandidates(methodCall, true);
    ChangeStringLiteralToCharInMethodCallFix.registerFixes(candidates, methodCall, highlightInfo);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextRange(com.intellij.openapi.util.TextRange)

Example 7 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class JavaClassReference method advancedResolveInner.

private JavaResolveResult advancedResolveInner(@NotNull PsiElement psiElement, @NotNull String qName, @NotNull PsiFile containingFile) {
    final PsiManager manager = containingFile.getManager();
    final GlobalSearchScope scope = getScope(containingFile);
    if (myIndex == myJavaClassReferenceSet.getReferences().length - 1) {
        final PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(qName, scope);
        if (aClass != null) {
            return new ClassCandidateInfo(aClass, PsiSubstitutor.EMPTY, false, psiElement);
        } else {
            if (!JavaClassReferenceProvider.ADVANCED_RESOLVE.getBooleanValue(getOptions())) {
                return JavaResolveResult.EMPTY;
            }
        }
    }
    PsiElement resolveResult = JavaPsiFacade.getInstance(manager.getProject()).findPackage(qName);
    if (resolveResult == null) {
        resolveResult = JavaPsiFacade.getInstance(manager.getProject()).findClass(qName, scope);
    }
    if (myInStaticImport && resolveResult == null) {
        resolveResult = resolveMember(qName, manager, getElement().getResolveScope());
    }
    if (resolveResult == null) {
        if (containingFile instanceof PsiJavaFile) {
            if (containingFile instanceof ServerPageFile) {
                containingFile = containingFile.getViewProvider().getPsi(JavaLanguage.INSTANCE);
                if (containingFile == null)
                    return JavaResolveResult.EMPTY;
            }
            final ClassResolverProcessor processor = new ClassResolverProcessor(getCanonicalText(), psiElement, containingFile);
            PsiClass contextClass = myJavaClassReferenceSet.getProvider().getContextClass(psiElement);
            if (contextClass != null) {
                PsiScopesUtil.treeWalkUp(processor, contextClass, null);
            } else {
                containingFile.processDeclarations(processor, ResolveState.initial(), null, psiElement);
            }
            if (processor.getResult().length == 1) {
                final JavaResolveResult javaResolveResult = processor.getResult()[0];
                if (javaResolveResult != JavaResolveResult.EMPTY && getOptions() != null) {
                    final Boolean value = JavaClassReferenceProvider.RESOLVE_QUALIFIED_CLASS_NAME.getValue(getOptions());
                    final PsiClass psiClass = (PsiClass) javaResolveResult.getElement();
                    if (value != null && value.booleanValue() && psiClass != null) {
                        final String qualifiedName = psiClass.getQualifiedName();
                        if (!qName.equals(qualifiedName)) {
                            return JavaResolveResult.EMPTY;
                        }
                    }
                }
                return javaResolveResult;
            }
        }
    }
    return resolveResult != null ? new CandidateInfo(resolveResult, PsiSubstitutor.EMPTY, false, false, psiElement) : JavaResolveResult.EMPTY;
}
Also used : ClassResolverProcessor(com.intellij.psi.impl.source.resolve.ClassResolverProcessor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassCandidateInfo(com.intellij.psi.infos.ClassCandidateInfo) CandidateInfo(com.intellij.psi.infos.CandidateInfo) ClassCandidateInfo(com.intellij.psi.infos.ClassCandidateInfo)

Example 8 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class JavaMethodsConflictResolver method checkSameSignatures.

protected void checkSameSignatures(@NotNull List<CandidateInfo> conflicts, FactoryMap<MethodCandidateInfo, PsiSubstitutor> map) {
    // candidates should go in order of class hierarchy traversal
    // in order for this to work
    Map<MethodSignature, CandidateInfo> signatures = new THashMap<>(conflicts.size());
    Set<PsiMethod> superMethods = new HashSet<>();
    for (CandidateInfo conflict : conflicts) {
        final PsiMethod method = ((MethodCandidateInfo) conflict).getElement();
        final PsiClass containingClass = method.getContainingClass();
        final boolean isInterface = containingClass != null && containingClass.isInterface();
        for (HierarchicalMethodSignature methodSignature : method.getHierarchicalMethodSignature().getSuperSignatures()) {
            final PsiMethod superMethod = methodSignature.getMethod();
            if (!isInterface) {
                superMethods.add(superMethod);
            } else {
                final PsiClass aClass = superMethod.getContainingClass();
                if (aClass != null && !CommonClassNames.JAVA_LANG_OBJECT.equals(aClass.getQualifiedName())) {
                    superMethods.add(superMethod);
                }
            }
        }
    }
    nextConflict: for (int i = 0; i < conflicts.size(); i++) {
        ProgressManager.checkCanceled();
        CandidateInfo info = conflicts.get(i);
        PsiMethod method = (PsiMethod) info.getElement();
        if (!method.hasModifierProperty(PsiModifier.STATIC) && superMethods.contains(method)) {
            conflicts.remove(i);
            i--;
            continue;
        }
        PsiClass class1 = method.getContainingClass();
        PsiSubstitutor infoSubstitutor = getSubstitutor((MethodCandidateInfo) info, map);
        MethodSignature signature = method.getSignature(infoSubstitutor);
        CandidateInfo existing = signatures.get(signature);
        if (existing == null) {
            signatures.put(signature, info);
            continue;
        }
        PsiMethod existingMethod = (PsiMethod) existing.getElement();
        PsiClass existingClass = existingMethod.getContainingClass();
        if (class1 != null && existingClass != null) {
            //prefer interface methods to methods from Object
            if (class1.isInterface() && CommonClassNames.JAVA_LANG_OBJECT.equals(existingClass.getQualifiedName())) {
                signatures.put(signature, info);
                continue;
            } else if (existingClass.isInterface() && CommonClassNames.JAVA_LANG_OBJECT.equals(class1.getQualifiedName())) {
                conflicts.remove(info);
                i--;
                continue;
            }
        }
        if (method == existingMethod) {
            PsiElement scope1 = info.getCurrentFileResolveScope();
            PsiElement scope2 = existing.getCurrentFileResolveScope();
            if (scope1 instanceof PsiClass && scope2 instanceof PsiClass && PsiTreeUtil.isAncestor(scope1, scope2, true) && !existing.isAccessible()) {
                //prefer methods from outer class to inaccessible base class methods
                signatures.put(signature, info);
                continue;
            }
        }
        // filter out methods with incorrect inferred bounds (for unrelated methods only)
        boolean existingTypeParamAgree = areTypeParametersAgree(existing);
        boolean infoTypeParamAgree = areTypeParametersAgree(info);
        if (existingTypeParamAgree && !infoTypeParamAgree && !PsiSuperMethodImplUtil.isSuperMethodSmart(method, existingMethod)) {
            conflicts.remove(i);
            i--;
            continue;
        }
        if (!existingTypeParamAgree && infoTypeParamAgree && !PsiSuperMethodImplUtil.isSuperMethodSmart(existingMethod, method)) {
            signatures.put(signature, info);
            int index = conflicts.indexOf(existing);
            conflicts.remove(index);
            i--;
            continue;
        }
        if (InheritanceUtil.isInheritorOrSelf(class1, existingClass, true) || InheritanceUtil.isInheritorOrSelf(existingClass, class1, true)) {
            PsiParameter[] parameters = method.getParameterList().getParameters();
            final PsiParameter[] existingParameters = existingMethod.getParameterList().getParameters();
            for (int i1 = 0, parametersLength = parameters.length; i1 < parametersLength; i1++) {
                if (parameters[i1].getType() instanceof PsiArrayType && !(existingParameters[i1].getType() instanceof PsiArrayType)) {
                    //prefer more specific type
                    signatures.put(signature, info);
                    continue nextConflict;
                }
            }
            PsiType returnType1 = method.getReturnType();
            PsiType returnType2 = existingMethod.getReturnType();
            if (returnType1 != null && returnType2 != null) {
                returnType1 = infoSubstitutor.substitute(returnType1);
                returnType2 = getSubstitutor((MethodCandidateInfo) existing, map).substitute(returnType2);
                if (!returnType1.equals(returnType2) && returnType1.isAssignableFrom(returnType2)) {
                    conflicts.remove(i);
                    i--;
                    continue;
                }
            }
            // prefer derived class
            signatures.put(signature, info);
        } else {
            final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(myArgumentsList, PsiMethodCallExpression.class);
            if (methodCallExpression != null) {
                final PsiReferenceExpression expression = methodCallExpression.getMethodExpression();
                final PsiExpression qualifierExpression = expression.getQualifierExpression();
                PsiClass currentClass;
                if (qualifierExpression != null) {
                    currentClass = PsiUtil.resolveClassInClassTypeOnly(qualifierExpression.getType());
                } else {
                    currentClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
                }
                if (currentClass != null && existingClass != null && class1 != null) {
                    final PsiSubstitutor eSubstitutor = TypeConversionUtil.getMaybeSuperClassSubstitutor(existingClass, currentClass, PsiSubstitutor.EMPTY, null);
                    final PsiSubstitutor cSubstitutor = TypeConversionUtil.getMaybeSuperClassSubstitutor(class1, currentClass, PsiSubstitutor.EMPTY, null);
                    if (eSubstitutor != null && cSubstitutor != null && MethodSignatureUtil.areSignaturesEqual(existingMethod.getSignature(eSubstitutor), method.getSignature(cSubstitutor))) {
                        final PsiType returnType = eSubstitutor.substitute(existingMethod.getReturnType());
                        final PsiType returnType1 = cSubstitutor.substitute(method.getReturnType());
                        if (returnType != null && returnType1 != null && !returnType1.equals(returnType)) {
                            if (TypeConversionUtil.isAssignable(returnType, returnType1, false)) {
                                if (class1.isInterface() && !existingClass.isInterface())
                                    continue;
                                conflicts.remove(existing);
                            } else {
                                if (!TypeConversionUtil.isAssignable(returnType1, returnType, false))
                                    continue;
                                conflicts.remove(i);
                            }
                            i--;
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) THashMap(gnu.trove.THashMap) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet)

Example 9 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class JavaMethodsConflictResolver method checkPrimitiveVarargs.

public void checkPrimitiveVarargs(@NotNull List<CandidateInfo> conflicts, final int argumentsCount) {
    if (JavaVersionService.getInstance().isAtLeast(myArgumentsList, JavaSdkVersion.JDK_1_7))
        return;
    CandidateInfo objectVararg = null;
    for (CandidateInfo conflict : conflicts) {
        ProgressManager.checkCanceled();
        final PsiMethod method = (PsiMethod) conflict.getElement();
        final int parametersCount = method.getParameterList().getParametersCount();
        if (method.isVarArgs() && parametersCount - 1 == argumentsCount) {
            final PsiType type = method.getParameterList().getParameters()[parametersCount - 1].getType();
            final PsiType componentType = ((PsiArrayType) type).getComponentType();
            final PsiClassType classType = PsiType.getJavaLangObject(method.getManager(), GlobalSearchScope.allScope(method.getProject()));
            if (Comparing.equal(componentType, classType)) {
                objectVararg = conflict;
            }
        }
    }
    if (objectVararg != null) {
        for (CandidateInfo conflict : conflicts) {
            ProgressManager.checkCanceled();
            PsiMethod method = (PsiMethod) conflict.getElement();
            if (method != objectVararg && method.isVarArgs()) {
                final int paramsCount = method.getParameterList().getParametersCount();
                final PsiType type = method.getParameterList().getParameters()[paramsCount - 1].getType();
                final PsiType componentType = ((PsiArrayType) type).getComponentType();
                if (argumentsCount == paramsCount - 1 && componentType instanceof PsiPrimitiveType) {
                    conflicts.remove(objectVararg);
                    break;
                }
            }
        }
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo)

Example 10 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class JavaMethodsConflictResolver method checkSpecifics.

public void checkSpecifics(@NotNull List<CandidateInfo> conflicts, @MethodCandidateInfo.ApplicabilityLevelConstant int applicabilityLevel, FactoryMap<MethodCandidateInfo, PsiSubstitutor> map, @NotNull LanguageLevel languageLevel) {
    final boolean applicable = applicabilityLevel > MethodCandidateInfo.ApplicabilityLevel.NOT_APPLICABLE;
    int conflictsCount = conflicts.size();
    // Specifics
    if (applicable) {
        final CandidateInfo[] newConflictsArray = conflicts.toArray(new CandidateInfo[conflicts.size()]);
        for (int i = 1; i < conflictsCount; i++) {
            final CandidateInfo method = newConflictsArray[i];
            for (int j = 0; j < i; j++) {
                ProgressManager.checkCanceled();
                final CandidateInfo conflict = newConflictsArray[j];
                if (nonComparable(method, conflict, applicabilityLevel == MethodCandidateInfo.ApplicabilityLevel.FIXED_ARITY))
                    continue;
                switch(isMoreSpecific((MethodCandidateInfo) method, (MethodCandidateInfo) conflict, applicabilityLevel, map, languageLevel)) {
                    case FIRST:
                        conflicts.remove(conflict);
                        break;
                    case SECOND:
                        conflicts.remove(method);
                        break;
                    case NEITHER:
                        break;
                }
            }
        }
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo)

Aggregations

CandidateInfo (com.intellij.psi.infos.CandidateInfo)60 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)19 NotNull (org.jetbrains.annotations.NotNull)13 Nullable (org.jetbrains.annotations.Nullable)6 Pair (com.intellij.openapi.util.Pair)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)4 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiClass (com.intellij.psi.PsiClass)3 PsiElement (com.intellij.psi.PsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrReferenceList (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList)3 PsiMethodMember (com.intellij.codeInsight.generation.PsiMethodMember)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 LanguageLevel (com.intellij.pom.java.LanguageLevel)2 ClassCandidateInfo (com.intellij.psi.infos.ClassCandidateInfo)2 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)2