Search in sources :

Example 21 with CandidateInfo

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

the class MethodParameterInfoHandler method updateUI.

@Override
public void updateUI(final Object p, @NotNull final ParameterInfoUIContext context) {
    if (p instanceof CandidateInfo) {
        CandidateInfo info = (CandidateInfo) p;
        PsiMethod method = (PsiMethod) info.getElement();
        if (!method.isValid()) {
            context.setUIComponentEnabled(false);
            return;
        }
        updateMethodPresentation(method, getCandidateInfoSubstitutor(info), context);
    } else {
        updateMethodPresentation((PsiMethod) p, null, context);
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo)

Example 22 with CandidateInfo

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

the class MethodParameterInfoHandler method getMethods.

private static CandidateInfo[] getMethods(PsiExpressionList argList) {
    final PsiCall call = getCall(argList);
    PsiResolveHelper helper = JavaPsiFacade.getInstance(argList.getProject()).getResolveHelper();
    if (call instanceof PsiCallExpression) {
        CandidateInfo[] candidates = getCandidates((PsiCallExpression) call);
        ArrayList<CandidateInfo> result = new ArrayList<>();
        if (!(argList.getParent() instanceof PsiAnonymousClass)) {
            cand: for (CandidateInfo candidate : candidates) {
                PsiMethod methodCandidate = (PsiMethod) candidate.getElement();
                for (CandidateInfo info : result) {
                    if (MethodSignatureUtil.isSuperMethod(methodCandidate, (PsiMethod) info.getElement())) {
                        continue cand;
                    }
                }
                if (candidate.isStaticsScopeCorrect()) {
                    boolean accessible = candidate.isAccessible();
                    if (!accessible && methodCandidate.getModifierList().hasModifierProperty(PsiModifier.PRIVATE)) {
                        // privates are accessible within one file
                        accessible = JavaPsiFacade.getInstance(methodCandidate.getProject()).getResolveHelper().isAccessible(methodCandidate, methodCandidate.getModifierList(), call, null, null);
                    }
                    if (accessible)
                        result.add(candidate);
                }
            }
        } else {
            PsiClass aClass = (PsiClass) argList.getParent();
            for (CandidateInfo candidate : candidates) {
                if (candidate.isStaticsScopeCorrect() && helper.isAccessible((PsiMethod) candidate.getElement(), argList, aClass)) {
                    result.add(candidate);
                }
            }
        }
        return result.isEmpty() ? candidates : result.toArray(new CandidateInfo[result.size()]);
    } else {
        assert call instanceof PsiEnumConstant;
        //We are inside our own enum, no isAccessible check needed
        PsiMethod[] constructors = ((PsiEnumConstant) call).getContainingClass().getConstructors();
        CandidateInfo[] result = new CandidateInfo[constructors.length];
        for (int i = 0; i < constructors.length; i++) {
            result[i] = new CandidateInfo(constructors[i], PsiSubstitutor.EMPTY);
        }
        return result;
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) ArrayList(java.util.ArrayList)

Example 23 with CandidateInfo

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

the class PsiDiamondTypeImpl method getStaticFactoryCandidateInfo.

private static JavaResolveResult getStaticFactoryCandidateInfo(final PsiNewExpression newExpression, final PsiElement context) {
    return ourDiamondGuard.doPreventingRecursion(context, false, () -> {
        final PsiExpressionList argumentList = newExpression.getArgumentList();
        if (argumentList == null) {
            //token expected diagnostic is provided by parser
            return null;
        }
        final JavaMethodsConflictResolver resolver = new JavaMethodsConflictResolver(argumentList, PsiUtil.getLanguageLevel(newExpression));
        final JavaResolveResult[] result = collectStaticFactories(newExpression, resolver);
        final PsiMethod staticFactory = result != null && result.length == 1 ? (PsiMethod) result[0].getElement() : null;
        if (staticFactory == null) {
            //additional diagnostics: inference fails due to unresolved constructor
            return JavaResolveResult.EMPTY;
        }
        final MethodCandidateInfo staticFactoryCandidateInfo = createMethodCandidate(staticFactory, context, false, argumentList);
        if (!staticFactory.isVarArgs()) {
            return staticFactoryCandidateInfo;
        }
        final ArrayList<CandidateInfo> conflicts = new ArrayList<>();
        conflicts.add(staticFactoryCandidateInfo);
        conflicts.add(createMethodCandidate(staticFactory, context, true, argumentList));
        return resolver.resolveConflict(conflicts);
    });
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) JavaMethodsConflictResolver(com.intellij.psi.scope.conflictResolvers.JavaMethodsConflictResolver)

Example 24 with CandidateInfo

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

the class OverrideImplementExploreUtil method getMapToOverrideImplement.

@NotNull
public static Map<MethodSignature, CandidateInfo> getMapToOverrideImplement(PsiClass aClass, boolean toImplement, boolean skipImplemented) {
    Map<MethodSignature, PsiMethod> abstracts = new LinkedHashMap<>();
    Map<MethodSignature, PsiMethod> finals = new LinkedHashMap<>();
    Map<MethodSignature, PsiMethod> concretes = new LinkedHashMap<>();
    PsiUtilCore.ensureValid(aClass);
    Collection<HierarchicalMethodSignature> allMethodSigs = aClass.getVisibleSignatures();
    PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
    for (HierarchicalMethodSignature signature : allMethodSigs) {
        PsiMethod method = signature.getMethod();
        PsiUtilCore.ensureValid(method);
        if (method.hasModifierProperty(PsiModifier.STATIC) || !resolveHelper.isAccessible(method, aClass, aClass))
            continue;
        for (HierarchicalMethodSignature superMethodSignature : signature.getSuperSignatures()) {
            final PsiMethod superMethod = superMethodSignature.getMethod();
            if (PsiUtil.getAccessLevel(superMethod.getModifierList()) > PsiUtil.getAccessLevel(method.getModifierList())) {
                method = superMethod;
                break;
            }
        }
        PsiClass hisClass = method.getContainingClass();
        if (hisClass == null)
            continue;
        // filter non-immediate super constructors
        if (method.isConstructor() && (!aClass.isInheritor(hisClass, false) || aClass instanceof PsiAnonymousClass || aClass.isEnum())) {
            continue;
        }
        // filter already implemented
        if (skipImplemented && MethodSignatureUtil.findMethodBySignature(aClass, signature, false) != null) {
            continue;
        }
        if (method.hasModifierProperty(PsiModifier.FINAL)) {
            finals.put(signature, method);
            continue;
        }
        Map<MethodSignature, PsiMethod> map = hisClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT) ? abstracts : concretes;
        fillMap(signature, method, map);
        if (isDefaultMethod(aClass, method)) {
            fillMap(signature, method, concretes);
        }
    }
    final Map<MethodSignature, CandidateInfo> result = new TreeMap<>(new MethodSignatureComparator());
    if (toImplement || aClass.isInterface()) {
        collectMethodsToImplement(aClass, abstracts, finals, concretes, result);
    } else {
        for (Map.Entry<MethodSignature, PsiMethod> entry : concretes.entrySet()) {
            MethodSignature signature = entry.getKey();
            PsiMethod concrete = entry.getValue();
            if (finals.get(signature) == null) {
                PsiMethod abstractOne = abstracts.get(signature);
                if (abstractOne == null || !abstractOne.getContainingClass().isInheritor(concrete.getContainingClass(), true) || CommonClassNames.JAVA_LANG_OBJECT.equals(concrete.getContainingClass().getQualifiedName())) {
                    PsiSubstitutor subst = correctSubstitutor(concrete, signature.getSubstitutor());
                    CandidateInfo info = new CandidateInfo(concrete, subst);
                    result.put(signature, info);
                }
            }
        }
    }
    return result;
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with CandidateInfo

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

the class ChangeParameterClassFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiClass myClass = (PsiClass) startElement;
    if (!FileModificationService.getInstance().prepareFileForWrite(file))
        return;
    ApplicationManager.getApplication().runWriteAction(() -> invokeImpl(myClass));
    final Editor editor1 = CodeInsightUtil.positionCursorAtLBrace(project, myClass.getContainingFile(), myClass);
    if (editor1 == null)
        return;
    final Collection<CandidateInfo> toImplement = OverrideImplementExploreUtil.getMethodsToOverrideImplement(myClass, true);
    if (!toImplement.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            ApplicationManager.getApplication().runWriteAction(() -> {
                Collection<PsiMethodMember> members = ContainerUtil.map2List(toImplement, s -> new PsiMethodMember(s));
                OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor1, myClass, members, false);
            });
        } else {
            //SCR 12599
            editor1.getCaretModel().moveToOffset(myClass.getTextRange().getStartOffset());
            OverrideImplementUtil.chooseAndImplementMethods(project, editor1, myClass);
        }
    }
    UndoUtil.markPsiFileForUndo(file);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) PsiClass(com.intellij.psi.PsiClass) Editor(com.intellij.openapi.editor.Editor)

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