Search in sources :

Example 56 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 57 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 58 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)

Example 59 with CandidateInfo

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

the class GrClassImplUtil method getAllMethodsAndTheirSubstitutors.

@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> getAllMethodsAndTheirSubstitutors(GrTypeDefinition grType) {
    final Map<String, List<CandidateInfo>> allMethodsMap = CollectClassMembersUtil.getAllMethods(grType, true);
    List<Pair<PsiMethod, PsiSubstitutor>> result = new ArrayList<>();
    for (List<CandidateInfo> infos : allMethodsMap.values()) {
        for (CandidateInfo info : infos) {
            result.add(Pair.create((PsiMethod) info.getElement(), info.getSubstitutor()));
        }
    }
    return result;
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 60 with CandidateInfo

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

the class GrClassImplUtil method findMethodsAndTheirSubstitutorsByName.

@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> findMethodsAndTheirSubstitutorsByName(GrTypeDefinition grType, String name, boolean checkBases) {
    final ArrayList<Pair<PsiMethod, PsiSubstitutor>> result = new ArrayList<>();
    if (!checkBases) {
        final PsiMethod[] methods = grType.findMethodsByName(name, false);
        for (PsiMethod method : methods) {
            result.add(Pair.create(method, PsiSubstitutor.EMPTY));
        }
    } else {
        final Map<String, List<CandidateInfo>> map = CollectClassMembersUtil.getAllMethods(grType, true);
        final List<CandidateInfo> candidateInfos = map.get(name);
        if (candidateInfos != null) {
            for (CandidateInfo info : candidateInfos) {
                final PsiElement element = info.getElement();
                result.add(Pair.create((PsiMethod) element, info.getSubstitutor()));
            }
        }
    }
    return result;
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

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