Search in sources :

Example 1 with GrSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.

the class ConvertClosureToMethodIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrField field;
    if (element.getParent() instanceof GrField) {
        field = (GrField) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        PsiElement resolved = ref.resolve();
        if (resolved instanceof GrAccessorMethod) {
            resolved = ((GrAccessorMethod) resolved).getProperty();
        }
        LOG.assertTrue(resolved instanceof GrField);
        field = (GrField) resolved;
    }
    final HashSet<PsiReference> usages = new HashSet<>();
    usages.addAll(ReferencesSearch.search(field).findAll());
    final GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        usages.addAll(MethodReferencesSearch.search(getter).findAll());
    }
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null) {
        usages.addAll(MethodReferencesSearch.search(setter).findAll());
    }
    final String fieldName = field.getName();
    LOG.assertTrue(fieldName != null);
    final Collection<PsiElement> fieldUsages = new HashSet<>();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (PsiReference usage : usages) {
        final PsiElement psiElement = usage.getElement();
        if (PsiUtil.isMethodUsage(psiElement))
            continue;
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
        } else {
            if (psiElement instanceof GrReferenceExpression) {
                fieldUsages.add(psiElement);
                if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
                    conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
                }
            } else if (psiElement instanceof GrArgumentLabel) {
                conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final GrExpression initializer = field.getInitializerGroovy();
    LOG.assertTrue(initializer != null);
    final PsiType type = initializer.getType();
    LOG.assertTrue(type instanceof GrClosureType);
    final GrSignature signature = ((GrClosureType) type).getSignature();
    final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
    for (MethodSignature s : signatures) {
        final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
        if (method != null) {
            conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
        }
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(field, fieldUsages);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignature(com.intellij.psi.util.MethodSignature) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) HashSet(com.intellij.util.containers.HashSet) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 2 with GrSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method createSignature.

@Nullable
public static GrClosureSignature createSignature(GrCall call) {
    if (call instanceof GrMethodCall) {
        final GrExpression invokedExpression = ((GrMethodCall) call).getInvokedExpression();
        final PsiType type = invokedExpression.getType();
        if (type instanceof GrClosureType) {
            final GrSignature signature = ((GrClosureType) type).getSignature();
            final Trinity<GrClosureSignature, ArgInfo<PsiType>[], ApplicabilityResult> trinity = getApplicableSignature(signature, PsiUtil.getArgumentTypes(invokedExpression, true), call);
            if (trinity != null) {
                return trinity.first;
            }
            return null;
        }
    }
    final GroovyResolveResult resolveResult = call.advancedResolve();
    final PsiElement element = resolveResult.getElement();
    if (element instanceof PsiMethod) {
        return createSignature((PsiMethod) element, resolveResult.getSubstitutor());
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.

the class TypesUtil method getLeastUpperBound.

@Nullable
public static PsiType getLeastUpperBound(@NotNull PsiType type1, @NotNull PsiType type2, PsiManager manager) {
    {
        PsiType numericLUB = getNumericLUB(type1, type2);
        if (numericLUB != null)
            return numericLUB;
    }
    if (type1 instanceof GrTupleType && type2 instanceof GrTupleType) {
        GrTupleType tuple1 = (GrTupleType) type1;
        GrTupleType tuple2 = (GrTupleType) type2;
        PsiType[] components1 = tuple1.getComponentTypes();
        PsiType[] components2 = tuple2.getComponentTypes();
        if (components1.length == 0)
            return genNewListBy(type2, manager);
        if (components2.length == 0)
            return genNewListBy(type1, manager);
        PsiType[] components3 = PsiType.createArray(Math.min(components1.length, components2.length));
        for (int i = 0; i < components3.length; i++) {
            PsiType c1 = components1[i];
            PsiType c2 = components2[i];
            if (c1 == null || c2 == null) {
                components3[i] = null;
            } else {
                components3[i] = getLeastUpperBound(c1, c2, manager);
            }
        }
        return new GrImmediateTupleType(components3, JavaPsiFacade.getInstance(manager.getProject()), tuple1.getScope().intersectWith(tuple2.getResolveScope()));
    } else if (checkEmptyListAndList(type1, type2)) {
        return genNewListBy(type2, manager);
    } else if (checkEmptyListAndList(type2, type1)) {
        return genNewListBy(type1, manager);
    } else if (type1 instanceof GrMapType && type2 instanceof GrMapType) {
        return GrMapType.merge(((GrMapType) type1), ((GrMapType) type2));
    } else if (checkEmptyMapAndMap(type1, type2)) {
        return genNewMapBy(type2, manager);
    } else if (checkEmptyMapAndMap(type2, type1)) {
        return genNewMapBy(type1, manager);
    } else if (type1 instanceof GrClosureType && type2 instanceof GrClosureType) {
        GrClosureType clType1 = (GrClosureType) type1;
        GrClosureType clType2 = (GrClosureType) type2;
        GrSignature signature1 = clType1.getSignature();
        GrSignature signature2 = clType2.getSignature();
        if (signature1 instanceof GrClosureSignature && signature2 instanceof GrClosureSignature) {
            if (((GrClosureSignature) signature1).getParameterCount() == ((GrClosureSignature) signature2).getParameterCount()) {
                final GrClosureSignature signature = GrImmediateClosureSignatureImpl.getLeastUpperBound(((GrClosureSignature) signature1), ((GrClosureSignature) signature2), manager);
                if (signature != null) {
                    GlobalSearchScope scope = clType1.getResolveScope().intersectWith(clType2.getResolveScope());
                    final LanguageLevel languageLevel = ComparatorUtil.max(clType1.getLanguageLevel(), clType2.getLanguageLevel());
                    return GrClosureType.create(signature, scope, JavaPsiFacade.getInstance(manager.getProject()), languageLevel, true);
                }
            }
        }
    } else if (GroovyCommonClassNames.GROOVY_LANG_GSTRING.equals(getQualifiedName(type1)) && CommonClassNames.JAVA_LANG_STRING.equals(getQualifiedName(type2))) {
        return type2;
    } else if (GroovyCommonClassNames.GROOVY_LANG_GSTRING.equals(getQualifiedName(type2)) && CommonClassNames.JAVA_LANG_STRING.equals(getQualifiedName(type1))) {
        return type1;
    }
    return GenericsUtil.getLeastUpperBound(type1, type2, manager);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) LanguageLevel(com.intellij.pom.java.LanguageLevel) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.

the class PsiUtil method isRawClosureCall.

private static boolean isRawClosureCall(GrMethodCallExpression call, GroovyResolveResult result, GrClosureType returnType) {
    final GrSignature signature = returnType.getSignature();
    GrClosureSignature _signature;
    if (signature instanceof GrClosureSignature) {
        _signature = (GrClosureSignature) signature;
    } else {
        final PsiType[] types = getArgumentTypes(call.getInvokedExpression(), true);
        final Trinity<GrClosureSignature, GrClosureSignatureUtil.ArgInfo<PsiType>[], GrClosureSignatureUtil.ApplicabilityResult> resultTrinity = types != null ? GrClosureSignatureUtil.getApplicableSignature(signature, types, call) : null;
        _signature = resultTrinity != null ? resultTrinity.first : null;
    }
    if (_signature != null) {
        return isRawType(_signature.getReturnType(), TypesUtil.composeSubstitutors(_signature.getSubstitutor(), result.getSubstitutor()));
    }
    return false;
}
Also used : GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)

Example 5 with GrSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature in project intellij-community by JetBrains.

the class ClosureToSamConverter method isConvertible.

@Override
public Boolean isConvertible(@NotNull PsiType ltype, @NotNull PsiType rtype, @NotNull final GroovyPsiElement context) {
    if (rtype instanceof GrClosureType && ltype instanceof PsiClassType && isSamConversionAllowed(context) && !TypesUtil.isClassType(ltype, GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
        MethodSignature signature = findSAMSignature(ltype);
        if (signature != null) {
            final PsiType[] samParameterTypes = signature.getParameterTypes();
            GrSignature closureSignature = ((GrClosureType) rtype).getSignature();
            boolean raw = ((PsiClassType) ltype).isRaw();
            if (raw)
                return true;
            if (GrClosureSignatureUtil.isSignatureApplicable(closureSignature, samParameterTypes, context)) {
                return true;
            }
        }
    }
    return null;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Aggregations

GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)8 GrClosureType (org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)6 Nullable (org.jetbrains.annotations.Nullable)4 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)4 MethodSignature (com.intellij.psi.util.MethodSignature)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)2 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)1 HashSet (com.intellij.util.containers.HashSet)1 MultiMap (com.intellij.util.containers.MultiMap)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)1 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1