Search in sources :

Example 1 with ExpectedTypeInfoImpl

use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.

the class JavaCompletionSorting method calcMatch.

private static int calcMatch(final List<String> words, int max, ExpectedTypeInfo[] myExpectedInfos) {
    for (ExpectedTypeInfo myExpectedInfo : myExpectedInfos) {
        String expectedName = ((ExpectedTypeInfoImpl) myExpectedInfo).getExpectedName();
        if (expectedName == null)
            continue;
        max = calcMatch(expectedName, words, max);
        max = calcMatch(truncDigits(expectedName), words, max);
    }
    return max;
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl)

Example 2 with ExpectedTypeInfoImpl

use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.

the class JavaSmartCompletionContributor method getExpectedTypes.

@NotNull
public static ExpectedTypeInfo[] getExpectedTypes(PsiElement position, boolean voidable) {
    if (psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(PsiThrowStatement.class)).accepts(position)) {
        final PsiElementFactory factory = JavaPsiFacade.getInstance(position.getProject()).getElementFactory();
        final PsiClassType classType = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION, position.getResolveScope());
        final List<ExpectedTypeInfo> result = new SmartList<>();
        result.add(new ExpectedTypeInfoImpl(classType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, classType, TailType.SEMICOLON, null, ExpectedTypeInfoImpl.NULL));
        final PsiMethod method = PsiTreeUtil.getContextOfType(position, PsiMethod.class, true);
        if (method != null) {
            for (final PsiClassType type : method.getThrowsList().getReferencedTypes()) {
                result.add(new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.SEMICOLON, null, ExpectedTypeInfoImpl.NULL));
            }
        }
        return result.toArray(new ExpectedTypeInfo[result.size()]);
    }
    PsiExpression expression = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true);
    if (expression == null)
        return ExpectedTypeInfo.EMPTY_ARRAY;
    return ExpectedTypesProvider.getExpectedTypes(expression, true, voidable, false);
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ExpectedTypeInfoImpl

use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.

the class SmartCastProvider method getParenthesizedCastExpectationByOperandType.

@Nullable
static ExpectedTypeInfo getParenthesizedCastExpectationByOperandType(PsiElement position) {
    PsiElement parenthesisOwner = getParenthesisOwner(position);
    PsiExpression operand = getCastedExpression(parenthesisOwner);
    if (operand == null || !(parenthesisOwner.getParent() instanceof PsiParenthesizedExpression))
        return null;
    PsiType dfaType = GuessManager.getInstance(operand.getProject()).getControlFlowExpressionType(operand);
    if (dfaType != null) {
        return new ExpectedTypeInfoImpl(dfaType, ExpectedTypeInfo.TYPE_OR_SUPERTYPE, dfaType, TailType.NONE, null, () -> null);
    }
    PsiType type = operand.getType();
    return type == null || type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? null : new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.NONE, null, () -> null);
}
Also used : ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ExpectedTypeInfoImpl

use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.

the class MethodReferenceCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet result) {
    if (!PsiUtil.isLanguageLevel8OrHigher(parameters.getOriginalFile()))
        return;
    final PsiElement rulezzRef = parameters.getPosition().getParent();
    if (rulezzRef == null || !LambdaUtil.isValidLambdaContext(rulezzRef.getParent()))
        return;
    final ExpectedTypeInfo[] expectedTypes = JavaSmartCompletionContributor.getExpectedTypes(parameters);
    for (ExpectedTypeInfo expectedType : expectedTypes) {
        final PsiType defaultType = expectedType.getDefaultType();
        if (LambdaUtil.isFunctionalType(defaultType)) {
            final PsiType functionalType = FunctionalInterfaceParameterizationUtil.getGroundTargetType(defaultType);
            final PsiType returnType = LambdaUtil.getFunctionalInterfaceReturnType(functionalType);
            if (returnType != null) {
                final PsiElement position = parameters.getPosition();
                final PsiElement refPlace = position.getParent();
                final ExpectedTypeInfoImpl typeInfo = new ExpectedTypeInfoImpl(returnType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, returnType, TailType.UNKNOWN, null, ExpectedTypeInfoImpl.NULL);
                final Map<PsiElement, PsiType> map = LambdaUtil.getFunctionalTypeMap();
                Consumer<LookupElement> noTypeCheck = new Consumer<LookupElement>() {

                    @Override
                    public void consume(final LookupElement lookupElement) {
                        final PsiElement element = lookupElement.getPsiElement();
                        if (element instanceof PsiMethod) {
                            final PsiMethodReferenceExpression referenceExpression = createMethodReferenceExpression((PsiMethod) element);
                            if (referenceExpression == null) {
                                return;
                            }
                            final PsiType added = map.put(referenceExpression, functionalType);
                            try {
                                final PsiElement resolve = referenceExpression.resolve();
                                if (resolve != null && PsiEquivalenceUtil.areElementsEquivalent(element, resolve) && PsiMethodReferenceUtil.checkMethodReferenceContext(referenceExpression, resolve, functionalType) == null) {
                                    result.addElement(new JavaMethodReferenceElement((PsiMethod) element, refPlace));
                                }
                            } finally {
                                if (added == null) {
                                    map.remove(referenceExpression);
                                }
                            }
                        }
                    }

                    private PsiMethodReferenceExpression createMethodReferenceExpression(PsiMethod method) {
                        PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(method.getProject());
                        if (refPlace instanceof PsiMethodReferenceExpression) {
                            final PsiMethodReferenceExpression referenceExpression = (PsiMethodReferenceExpression) refPlace.copy();
                            final PsiElement referenceNameElement = referenceExpression.getReferenceNameElement();
                            LOG.assertTrue(referenceNameElement != null, referenceExpression);
                            referenceNameElement.replace(method.isConstructor() ? elementFactory.createKeyword("new") : elementFactory.createIdentifier(method.getName()));
                            return referenceExpression;
                        } else if (method.hasModifierProperty(PsiModifier.STATIC)) {
                            final PsiClass aClass = method.getContainingClass();
                            LOG.assertTrue(aClass != null);
                            final String qualifiedName = aClass.getQualifiedName();
                            return (PsiMethodReferenceExpression) elementFactory.createExpressionFromText(qualifiedName + "::" + (method.isConstructor() ? "new" : method.getName()), refPlace);
                        } else {
                            return null;
                        }
                    }
                };
                final Runnable runnable = ReferenceExpressionCompletionContributor.fillCompletionVariants(new JavaSmartCompletionParameters(parameters, typeInfo), noTypeCheck);
                if (runnable != null) {
                    runnable.run();
                }
            }
        }
    }
}
Also used : ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) Consumer(com.intellij.util.Consumer)

Example 5 with ExpectedTypeInfoImpl

use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.

the class CreateMethodFromMethodReferenceFix method invokeImpl.

@Override
protected void invokeImpl(final PsiClass targetClass) {
    if (targetClass == null)
        return;
    PsiMethodReferenceExpression expression = getMethodReference();
    if (expression == null)
        return;
    if (isValidElement(expression))
        return;
    PsiClass parentClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
    PsiMember enclosingContext = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, PsiField.class, PsiClassInitializer.class);
    String methodName = expression.getReferenceName();
    LOG.assertTrue(methodName != null);
    final Project project = targetClass.getProject();
    JVMElementFactory elementFactory = JVMElementFactories.getFactory(targetClass.getLanguage(), project);
    if (elementFactory == null)
        elementFactory = JavaPsiFacade.getElementFactory(project);
    PsiMethod method = expression.isConstructor() ? (PsiMethod) targetClass.add(elementFactory.createConstructor()) : CreateMethodFromUsageFix.createMethod(targetClass, parentClass, enclosingContext, methodName);
    if (method == null) {
        return;
    }
    if (!expression.isConstructor()) {
        setupVisibility(parentClass, targetClass, method.getModifierList());
    }
    expression = getMethodReference();
    LOG.assertTrue(expression.isValid());
    boolean shouldBeAbstract = false;
    if (!expression.isConstructor()) {
        if (shouldCreateStaticMember(expression, targetClass)) {
            PsiUtil.setModifierProperty(method, PsiModifier.STATIC, true);
        } else if (targetClass.isInterface()) {
            shouldBeAbstract = true;
            PsiCodeBlock body = method.getBody();
            assert body != null;
            body.delete();
        }
    }
    final PsiElement context = PsiTreeUtil.getParentOfType(expression, PsiClass.class, PsiMethod.class);
    final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
    final PsiClassType.ClassResolveResult classResolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
    final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(classResolveResult);
    LOG.assertTrue(interfaceMethod != null);
    final PsiType interfaceReturnType = LambdaUtil.getFunctionalInterfaceReturnType(functionalInterfaceType);
    LOG.assertTrue(interfaceReturnType != null);
    final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, classResolveResult);
    final ExpectedTypeInfo[] expectedTypes = { new ExpectedTypeInfoImpl(interfaceReturnType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, interfaceReturnType, TailType.NONE, null, ExpectedTypeInfoImpl.NULL) };
    CreateMethodFromUsageFix.doCreate(targetClass, method, shouldBeAbstract, ContainerUtil.map2List(interfaceMethod.getParameterList().getParameters(), parameter -> Pair.create(null, substitutor.substitute(parameter.getType()))), PsiSubstitutor.EMPTY, expectedTypes, context);
}
Also used : QuickFixBundle(com.intellij.codeInsight.daemon.QuickFixBundle) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) ContainerUtil(com.intellij.util.containers.ContainerUtil) TailType(com.intellij.codeInsight.TailType) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) List(java.util.List) Function(com.intellij.util.Function) Pair(com.intellij.openapi.util.Pair) Project(com.intellij.openapi.project.Project) PsiUtil(com.intellij.psi.util.PsiUtil) com.intellij.psi(com.intellij.psi) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl) ExpectedTypeInfoImpl(com.intellij.codeInsight.ExpectedTypeInfoImpl) Project(com.intellij.openapi.project.Project) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Aggregations

ExpectedTypeInfoImpl (com.intellij.codeInsight.ExpectedTypeInfoImpl)5 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)4 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 TailType (com.intellij.codeInsight.TailType)1 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 com.intellij.psi (com.intellij.psi)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 PsiUtil (com.intellij.psi.util.PsiUtil)1 Consumer (com.intellij.util.Consumer)1 Function (com.intellij.util.Function)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 Collections (java.util.Collections)1 List (java.util.List)1