Search in sources :

Example 46 with CandidateInfo

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

the class DuplicateConflictResolver method resolveConflict.

@Override
public CandidateInfo resolveConflict(@NotNull List<CandidateInfo> conflicts) {
    if (conflicts.size() == 1)
        return conflicts.get(0);
    final Map<Object, CandidateInfo> uniqueItems = new HashMap<>();
    for (CandidateInfo info : conflicts) {
        final PsiElement element = info.getElement();
        Object key;
        if (info instanceof MethodCandidateInfo) {
            key = ((PsiMethod) element).getSignature(((MethodCandidateInfo) info).getSubstitutor(false));
        } else {
            key = PsiUtilCore.getName(element);
        }
        if (!uniqueItems.containsKey(key)) {
            uniqueItems.put(key, info);
        }
    }
    if (uniqueItems.size() == 1)
        return uniqueItems.values().iterator().next();
    return null;
}
Also used : HashMap(com.intellij.util.containers.HashMap) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) PsiElement(com.intellij.psi.PsiElement)

Example 47 with CandidateInfo

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

the class GroovyOverrideImplementExploreUtil method collectMethodsToImplement.

public static void collectMethodsToImplement(PsiClass aClass, Map<MethodSignature, PsiMethod> abstracts, Map<MethodSignature, PsiMethod> finals, Map<MethodSignature, PsiMethod> concretes, Map<MethodSignature, CandidateInfo> result) {
    for (Map.Entry<MethodSignature, PsiMethod> entry : abstracts.entrySet()) {
        MethodSignature signature = entry.getKey();
        PsiMethod abstractOne = entry.getValue();
        PsiMethod concrete = concretes.get(signature);
        if (concrete == null || PsiUtil.getAccessLevel(concrete.getModifierList()) < PsiUtil.getAccessLevel(abstractOne.getModifierList()) || !abstractOne.getContainingClass().isInterface() && abstractOne.getContainingClass().isInheritor(concrete.getContainingClass(), true) || isDefaultMethod(abstractOne)) {
            if (finals.get(signature) == null) {
                PsiSubstitutor subst = OverrideImplementExploreUtil.correctSubstitutor(abstractOne, signature.getSubstitutor());
                CandidateInfo info = new CandidateInfo(abstractOne, subst);
                result.put(signature, info);
            }
        }
    }
    for (final PsiMethod method : new GroovyMethodImplementor().getMethodsToImplement(aClass)) {
        MethodSignature signature = MethodSignatureUtil.createMethodSignature(method.getName(), method.getParameterList(), method.getTypeParameterList(), PsiSubstitutor.EMPTY, method.isConstructor());
        CandidateInfo info = new CandidateInfo(method, PsiSubstitutor.EMPTY);
        result.put(signature, info);
    }
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) CandidateInfo(com.intellij.psi.infos.CandidateInfo) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 48 with CandidateInfo

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

the class GroovyOverrideImplementUtil method chooseAndOverrideOrImplementMethods.

public static void chooseAndOverrideOrImplementMethods(@NotNull Project project, @NotNull final Editor editor, @NotNull final GrTypeDefinition aClass, boolean toImplement) {
    LOG.assertTrue(aClass.isValid());
    ApplicationManager.getApplication().assertReadAccessAllowed();
    Collection<CandidateInfo> candidates = GroovyOverrideImplementExploreUtil.getMethodsToOverrideImplement(aClass, toImplement);
    Collection<CandidateInfo> secondary = toImplement || aClass.isInterface() ? ContainerUtil.<CandidateInfo>newArrayList() : GroovyOverrideImplementExploreUtil.getMethodsToOverrideImplement(aClass, true);
    if (toImplement) {
        for (Iterator<CandidateInfo> iterator = candidates.iterator(); iterator.hasNext(); ) {
            CandidateInfo candidate = iterator.next();
            PsiElement element = candidate.getElement();
            if (element instanceof GrMethod) {
                GrMethod method = (GrMethod) element;
                if (GrTraitUtil.isTrait(method.getContainingClass()) && !GrTraitUtil.isMethodAbstract(method)) {
                    iterator.remove();
                    secondary.add(candidate);
                }
            }
        }
    }
    final MemberChooser<PsiMethodMember> chooser = OverrideImplementUtil.showOverrideImplementChooser(editor, aClass, toImplement, candidates, secondary);
    if (chooser == null)
        return;
    final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
    if (selectedElements == null || selectedElements.isEmpty())
        return;
    LOG.assertTrue(aClass.isValid());
    new WriteCommandAction(project, aClass.getContainingFile()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, aClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) CandidateInfo(com.intellij.psi.infos.CandidateInfo) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Result(com.intellij.openapi.application.Result)

Example 49 with CandidateInfo

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

the class GrClassImplUtil method processDeclarations.

public static boolean processDeclarations(@NotNull GrTypeDefinition grType, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
    if (place instanceof GrCodeReferenceElement && lastParent instanceof GrModifierList) {
        final PsiElement possibleAnnotation = PsiTreeUtil.skipParentsOfType(place, GrCodeReferenceElement.class);
        if (possibleAnnotation instanceof GrAnnotation && possibleAnnotation.getParent() == lastParent) {
            //don't process class members while resolving annotation which annotates current class
            return true;
        }
    }
    for (final PsiTypeParameter typeParameter : grType.getTypeParameters()) {
        if (!ResolveUtil.processElement(processor, typeParameter, state))
            return false;
    }
    NameHint nameHint = processor.getHint(NameHint.KEY);
    String name = nameHint == null ? null : nameHint.getName(state);
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    final PsiSubstitutor substitutor = state.get(PsiSubstitutor.KEY);
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(place.getProject());
    boolean processInstanceMethods = (ResolveUtil.shouldProcessMethods(classHint) || ResolveUtil.shouldProcessProperties(classHint)) && shouldProcessInstanceMembers(grType, lastParent);
    LanguageLevel level = PsiUtil.getLanguageLevel(place);
    if (ResolveUtil.shouldProcessProperties(classHint)) {
        Map<String, CandidateInfo> fieldsMap = CollectClassMembersUtil.getAllFields(grType);
        if (name != null) {
            CandidateInfo fieldInfo = fieldsMap.get(name);
            if (fieldInfo != null) {
                if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, fieldInfo)) {
                    return false;
                }
            } else if (grType.isTrait() && lastParent != null) {
                PsiField field = findFieldByName(grType, name, false, true);
                if (field != null && field.hasModifierProperty(PsiModifier.PUBLIC)) {
                    if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, new CandidateInfo(field, PsiSubstitutor.EMPTY))) {
                        return false;
                    }
                }
            }
        } else {
            for (CandidateInfo info : fieldsMap.values()) {
                if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, info)) {
                    return false;
                }
            }
            if (grType.isTrait() && lastParent != null) {
                for (PsiField field : CollectClassMembersUtil.getFields(grType, true)) {
                    if (field.hasModifierProperty(PsiModifier.PUBLIC)) {
                        if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, new CandidateInfo(field, PsiSubstitutor.EMPTY))) {
                            return false;
                        }
                    }
                }
            }
        }
    }
    if (ResolveUtil.shouldProcessMethods(classHint)) {
        Map<String, List<CandidateInfo>> methodsMap = CollectClassMembersUtil.getAllMethods(grType, true);
        boolean isPlaceGroovy = place.getLanguage() == GroovyLanguage.INSTANCE;
        if (name == null) {
            for (List<CandidateInfo> list : methodsMap.values()) {
                for (CandidateInfo info : list) {
                    if (!processMethod(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, isPlaceGroovy, info)) {
                        return false;
                    }
                }
            }
        } else {
            List<CandidateInfo> byName = methodsMap.get(name);
            if (byName != null) {
                for (CandidateInfo info : byName) {
                    if (!processMethod(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, isPlaceGroovy, info)) {
                        return false;
                    }
                }
            }
        }
    }
    final GrTypeDefinitionBody body = grType.getBody();
    if (body != null) {
        if (ResolveUtil.shouldProcessClasses(classHint)) {
            for (PsiClass innerClass : getInnerClassesForResolve(grType, lastParent, place)) {
                if (name != null && !name.equals(innerClass.getName()))
                    continue;
                if (!processor.execute(innerClass, state))
                    return false;
            }
        }
    }
    return true;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) ElementClassHint(com.intellij.psi.scope.ElementClassHint) CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) LanguageLevel(com.intellij.pom.java.LanguageLevel) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) NameHint(com.intellij.psi.scope.NameHint)

Example 50 with CandidateInfo

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

the class GrClassImplUtil method findFieldByName.

@Nullable
public static PsiField findFieldByName(GrTypeDefinition grType, String name, boolean checkBases, boolean includeSynthetic) {
    if (!checkBases) {
        for (PsiField field : CollectClassMembersUtil.getFields(grType, includeSynthetic)) {
            if (name.equals(field.getName()))
                return field;
        }
        return null;
    }
    Map<String, CandidateInfo> fieldsMap = CollectClassMembersUtil.getAllFields(grType, includeSynthetic);
    final CandidateInfo info = fieldsMap.get(name);
    return info == null ? null : (PsiField) info.getElement();
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) Nullable(org.jetbrains.annotations.Nullable)

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