Search in sources :

Example 16 with CandidateInfo

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

the class Java8ExpressionsCheckTest method testCachingOfResultsDuringCandidatesIteration.

public void testCachingOfResultsDuringCandidatesIteration() throws Exception {
    configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
    final Collection<PsiMethodCallExpression> methodCallExpressions = PsiTreeUtil.findChildrenOfType(getFile(), PsiMethodCallExpression.class);
    final PsiResolveHelper helper = JavaPsiFacade.getInstance(getProject()).getResolveHelper();
    for (PsiMethodCallExpression expression : methodCallExpressions) {
        CandidateInfo[] candidates = helper.getReferencedMethodCandidates(expression, false, true);
        PsiExpressionList argumentList = expression.getArgumentList();
        PsiExpression[] args = argumentList.getExpressions();
        for (JavaResolveResult result : candidates) {
            if (result instanceof MethodCandidateInfo) {
                final MethodCandidateInfo info = (MethodCandidateInfo) result;
                MethodCandidateInfo.ourOverloadGuard.doPreventingRecursion(argumentList, false, () -> info.inferTypeArguments(DefaultParameterTypeInferencePolicy.INSTANCE, args, true));
            }
        }
        PsiMethodCallExpression parentCall = PsiTreeUtil.getParentOfType(expression, PsiMethodCallExpression.class, true);
        if (parentCall != null) {
            JavaResolveResult result = parentCall.getMethodExpression().advancedResolve(false);
            if (result instanceof MethodCandidateInfo) {
                assertNull(((MethodCandidateInfo) result).getInferenceErrorMessage());
            }
        }
    }
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo)

Example 17 with CandidateInfo

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

the class ChangeStringLiteralToCharInMethodCallFix method registerFixes.

public static void registerFixes(@NotNull final CandidateInfo[] candidates, @NotNull final PsiMethodCallExpression methodCall, @Nullable final HighlightInfo info) {
    if (info == null)
        return;
    final Set<PsiLiteralExpression> literals = new HashSet<>();
    boolean exactMatch = false;
    for (CandidateInfo candidate : candidates) {
        if (candidate instanceof MethodCandidateInfo) {
            final PsiMethod method = ((MethodCandidateInfo) candidate).getElement();
            exactMatch |= findMatchingExpressions(methodCall.getArgumentList().getExpressions(), method, literals);
        }
    }
    if (!exactMatch) {
        processLiterals(literals, methodCall, info);
    }
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) HashSet(java.util.HashSet)

Example 18 with CandidateInfo

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

the class JavaOverrideImplementMemberChooser method create.

@Nullable
public static JavaOverrideImplementMemberChooser create(final PsiElement aClass, final boolean toImplement, final Collection<CandidateInfo> candidates, final Collection<CandidateInfo> secondary) {
    final Project project = aClass.getProject();
    if (candidates.isEmpty() && secondary.isEmpty())
        return null;
    final PsiMethodMember[] onlyPrimary = convertToMethodMembers(candidates);
    final LinkedHashSet<CandidateInfo> allCandidates = new LinkedHashSet<>(candidates);
    allCandidates.addAll(secondary);
    final PsiMethodMember[] all = convertToMethodMembers(allCandidates);
    final NotNullLazyValue<PsiMethodWithOverridingPercentMember[]> lazyElementsWithPercent = new NotNullLazyValue<PsiMethodWithOverridingPercentMember[]>() {

        @NotNull
        @Override
        protected PsiMethodWithOverridingPercentMember[] compute() {
            final PsiMethodWithOverridingPercentMember[] elements = PsiMethodWithOverridingPercentMember.calculateOverridingPercents(candidates);
            Arrays.sort(elements, PsiMethodWithOverridingPercentMember.COMPARATOR);
            return elements;
        }
    };
    final boolean merge = PropertiesComponent.getInstance(project).getBoolean(PROP_COMBINED_OVERRIDE_IMPLEMENT, true);
    final LanguageLevel languageLevel = PsiUtil.getLanguageLevel(aClass);
    //hide option if implement interface for 1.5 language level
    final boolean overrideVisible = languageLevel.isAtLeast(LanguageLevel.JDK_1_6) || languageLevel.equals(LanguageLevel.JDK_1_5) && !toImplement;
    final JavaOverrideImplementMemberChooser javaOverrideImplementMemberChooser = new JavaOverrideImplementMemberChooser(all, onlyPrimary, lazyElementsWithPercent, project, overrideVisible, merge, toImplement, PropertiesComponent.getInstance(project).getBoolean(PROP_OVERRIDING_SORTED_OVERRIDE_IMPLEMENT));
    javaOverrideImplementMemberChooser.setTitle(getChooserTitle(toImplement, merge));
    javaOverrideImplementMemberChooser.setCopyJavadocVisible(true);
    if (toImplement) {
        if (onlyPrimary.length == 0) {
            javaOverrideImplementMemberChooser.selectElements(new ClassMember[] { all[0] });
        } else {
            javaOverrideImplementMemberChooser.selectElements(onlyPrimary);
        }
    }
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!toImplement || onlyPrimary.length == 0) {
            javaOverrideImplementMemberChooser.selectElements(all);
        }
        javaOverrideImplementMemberChooser.close(DialogWrapper.OK_EXIT_CODE);
        return javaOverrideImplementMemberChooser;
    }
    return javaOverrideImplementMemberChooser;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) CandidateInfo(com.intellij.psi.infos.CandidateInfo) Project(com.intellij.openapi.project.Project) LanguageLevel(com.intellij.pom.java.LanguageLevel) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with CandidateInfo

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

the class OverrideImplementUtil method overrideOrImplementMethods.

@NotNull
public static List<PsiGenerationInfo<PsiMethod>> overrideOrImplementMethods(PsiClass aClass, Collection<PsiMethodMember> candidates, boolean toCopyJavaDoc, boolean toInsertAtOverride) throws IncorrectOperationException {
    List<CandidateInfo> candidateInfos = ContainerUtil.map2List(candidates, s -> new CandidateInfo(s.getElement(), s.getSubstitutor()));
    final List<PsiMethod> methods = overrideOrImplementMethodCandidates(aClass, candidateInfos, toCopyJavaDoc, toInsertAtOverride);
    return convert2GenerationInfos(methods);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with CandidateInfo

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

the class OverrideImplementUtil method showOverrideImplementChooser.

/**
   * @param candidates, secondary should allow modifications
   */
@Nullable
public static MemberChooser<PsiMethodMember> showOverrideImplementChooser(Editor editor, final PsiElement aClass, final boolean toImplement, final Collection<CandidateInfo> candidates, Collection<CandidateInfo> secondary) {
    if (toImplement) {
        for (Iterator<CandidateInfo> iterator = candidates.iterator(); iterator.hasNext(); ) {
            CandidateInfo candidate = iterator.next();
            PsiElement element = candidate.getElement();
            if (element instanceof PsiMethod && ((PsiMethod) element).hasModifierProperty(PsiModifier.DEFAULT)) {
                iterator.remove();
                secondary.add(candidate);
            }
        }
    }
    final JavaOverrideImplementMemberChooser chooser = JavaOverrideImplementMemberChooser.create(aClass, toImplement, candidates, secondary);
    if (chooser == null) {
        return null;
    }
    Project project = aClass.getProject();
    registerHandlerForComplementaryAction(project, editor, aClass, toImplement, chooser);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return chooser;
    }
    chooser.show();
    if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
        return null;
    return chooser;
}
Also used : Project(com.intellij.openapi.project.Project) 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