Search in sources :

Example 16 with ExpectedTypeInfo

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

the class ExpectedTypeMacro method getExpectedTypes.

@Nullable
private static PsiType[] getExpectedTypes(Expression[] params, final ExpressionContext context) {
    if (params.length != 0)
        return null;
    final Project project = context.getProject();
    PsiType[] types = null;
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    assert file != null;
    final PsiFile fileCopy = (PsiFile) file.copy();
    BlockSupport.getInstance(project).reparseRange(fileCopy, context.getTemplateStartOffset(), context.getTemplateEndOffset(), CompletionUtil.DUMMY_IDENTIFIER);
    PsiElement element = fileCopy.findElementAt(context.getTemplateStartOffset());
    if (element instanceof PsiIdentifier && element.getParent() instanceof PsiExpression) {
        ExpectedTypeInfo[] infos = ExpectedTypesProvider.getExpectedTypes((PsiExpression) element.getParent(), true);
        if (infos.length > 0) {
            types = new PsiType[infos.length];
            for (int i = 0; i < infos.length; i++) {
                ExpectedTypeInfo info = infos[i];
                types[i] = info.getType();
            }
        }
    }
    return types;
}
Also used : Project(com.intellij.openapi.project.Project) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with ExpectedTypeInfo

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

the class ExpectedTypesGetter method extractTypes.

@NotNull
public static PsiType[] extractTypes(ExpectedTypeInfo[] infos, boolean defaultTypes) {
    Set<PsiType> result = new THashSet<>(infos.length);
    for (ExpectedTypeInfo info : infos) {
        final PsiType type = info.getType();
        final PsiType defaultType = info.getDefaultType();
        if (!defaultTypes && !defaultType.equals(type)) {
            result.add(type);
        }
        result.add(defaultType);
    }
    return result.toArray(PsiType.createArray(result.size()));
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) THashSet(gnu.trove.THashSet) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with ExpectedTypeInfo

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

the class CreateClassFromNewFix method setupInheritance.

private static void setupInheritance(PsiNewExpression element, PsiClass targetClass) throws IncorrectOperationException {
    if (element.getParent() instanceof PsiReferenceExpression)
        return;
    ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes(element, false);
    for (ExpectedTypeInfo expectedType : expectedTypes) {
        PsiType type = expectedType.getType();
        if (!(type instanceof PsiClassType))
            continue;
        final PsiClassType classType = (PsiClassType) type;
        PsiClass aClass = classType.resolve();
        if (aClass == null)
            continue;
        if (aClass.equals(targetClass) || aClass.hasModifierProperty(PsiModifier.FINAL))
            continue;
        PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
        if (aClass.isInterface()) {
            PsiReferenceList implementsList = targetClass.getImplementsList();
            assert implementsList != null : targetClass;
            implementsList.add(factory.createReferenceElementByType(classType));
        } else {
            PsiReferenceList extendsList = targetClass.getExtendsList();
            assert extendsList != null : targetClass;
            if (extendsList.getReferencedTypes().length == 0 && !CommonClassNames.JAVA_LANG_OBJECT.equals(classType.getCanonicalText())) {
                extendsList.add(factory.createReferenceElementByType(classType));
            }
        }
    }
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Example 19 with ExpectedTypeInfo

use of com.intellij.codeInsight.ExpectedTypeInfo 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)

Example 20 with ExpectedTypeInfo

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

the class CreateEnumConstantFromUsageFix method isAvailableImpl.

@Override
protected boolean isAvailableImpl(int offset) {
    if (!super.isAvailableImpl(offset))
        return false;
    PsiElement element = getElement();
    final List<PsiClass> classes = getTargetClasses(element);
    if (classes.size() != 1 || !classes.get(0).isEnum())
        return false;
    ExpectedTypeInfo[] typeInfos = CreateFromUsageUtils.guessExpectedTypes(myReferenceExpression, false);
    PsiType enumType = JavaPsiFacade.getInstance(myReferenceExpression.getProject()).getElementFactory().createType(classes.get(0));
    for (final ExpectedTypeInfo typeInfo : typeInfos) {
        if (ExpectedTypeUtil.matches(enumType, typeInfo))
            return true;
    }
    return false;
}
Also used : ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo)

Aggregations

ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)24 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)5 ExpectedTypeInfoImpl (com.intellij.codeInsight.ExpectedTypeInfoImpl)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 THashSet (gnu.trove.THashSet)3 Nullable (org.jetbrains.annotations.Nullable)3 TailType (com.intellij.codeInsight.TailType)2 Template (com.intellij.codeInsight.template.Template)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2 com.intellij.psi (com.intellij.psi)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2 PsiUtil (com.intellij.psi.util.PsiUtil)2 Consumer (com.intellij.util.Consumer)2 ProcessingContext (com.intellij.util.ProcessingContext)2 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 EmptyExpression (com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression)1 GuessTypeParameters (com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters)1 GenerateMembersUtil (com.intellij.codeInsight.generation.GenerateMembersUtil)1