Search in sources :

Example 11 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 12 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)

Example 13 with CandidateInfo

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

the class GrClassImplUtil method getInnerClassesForResolve.

@NotNull
private static List<PsiClass> getInnerClassesForResolve(@NotNull final GrTypeDefinition grType, @Nullable final PsiElement lastParent, @NotNull final PsiElement place) {
    if (lastParent instanceof GrReferenceList || PsiTreeUtil.getParentOfType(place, GrReferenceList.class) != null) {
        return Arrays.asList(grType.getCodeInnerClasses());
    }
    boolean includeSynthetic = !PsiTreeUtil.isContextAncestor(grType, place, true);
    Object key = Trinity.create(grType, lastParent, place);
    List<PsiClass> classes = RecursionManager.doPreventingRecursion(key, false, () -> {
        List<PsiClass> result = new ArrayList<>();
        for (CandidateInfo info : CollectClassMembersUtil.getAllInnerClasses(grType, includeSynthetic).values()) {
            final PsiClass inner = (PsiClass) info.getElement();
            final PsiClass containingClass = inner.getContainingClass();
            assert containingClass != null;
            if (lastParent == null || !containingClass.isInterface() || PsiTreeUtil.isAncestor(containingClass, place, false)) {
                ContainerUtil.addIfNotNull(result, inner);
            }
        }
        return result;
    });
    if (classes == null) {
        return Arrays.asList(grType.getCodeInnerClasses());
    }
    return classes;
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with CandidateInfo

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

the class GrClassImplUtil method findInnerClassByName.

@Nullable
public static PsiClass findInnerClassByName(GrTypeDefinition grType, String name, boolean checkBases) {
    if (!checkBases) {
        for (PsiClass inner : grType.getInnerClasses()) {
            if (name.equals(inner.getName()))
                return inner;
        }
        return null;
    } else {
        Map<String, CandidateInfo> innerClasses = CollectClassMembersUtil.getAllInnerClasses(grType, true);
        final CandidateInfo info = innerClasses.get(name);
        return info == null ? null : (PsiClass) info.getElement();
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with CandidateInfo

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

the class CollectClassMembersUtil method addMethod.

private static void addMethod(@NotNull Map<String, List<CandidateInfo>> allMethods, @NotNull PsiMethod method, @NotNull PsiSubstitutor substitutor) {
    String name = method.getName();
    List<CandidateInfo> methods = allMethods.get(name);
    if (methods == null) {
        methods = new ArrayList<>();
        allMethods.put(name, methods);
    }
    methods.add(new CandidateInfo(method, substitutor));
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo)

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