Search in sources :

Example 6 with GrClosureType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType in project intellij-community by JetBrains.

the class ResolveUtil method extractReturnTypeFromCandidate.

@Nullable
public static PsiType extractReturnTypeFromCandidate(GroovyResolveResult candidate, GrExpression expression, @Nullable PsiType[] args) {
    final PsiElement element = candidate.getElement();
    if (element instanceof PsiMethod && !candidate.isInvokedOnProperty()) {
        return TypesUtil.substituteAndNormalizeType(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.getSmartReturnType((PsiMethod) element), candidate.getSubstitutor(), candidate.getSpreadState(), expression);
    }
    final PsiType type;
    if (element instanceof GrField) {
        type = ((GrField) element).getTypeGroovy();
    } else if (element instanceof PsiMethod) {
        type = org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.getSmartReturnType((PsiMethod) element);
    } else {
        return null;
    }
    if (type instanceof GrClosureType) {
        final GrSignature signature = ((GrClosureType) type).getSignature();
        PsiType returnType = GrClosureSignatureUtil.getReturnType(signature, args, expression);
        return TypesUtil.substituteAndNormalizeType(returnType, candidate.getSubstitutor(), candidate.getSpreadState(), expression);
    }
    return null;
}
Also used : 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 7 with GrClosureType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType in project intellij-community by JetBrains.

the class GrMethodConflictUtil method checkForClosurePropertySignatureOverload.

private static void checkForClosurePropertySignatureOverload(PsiClass clazz, GrMethod prototype, final GrMethod refactoredMethod, final MultiMap<PsiElement, String> conflicts, final List<MethodSignature> prototypeSignatures) {
    final boolean isStatic = prototype.hasModifierProperty(PsiModifier.STATIC);
    final String name = prototype.getName();
    if (!GroovyPropertyUtils.isProperty(clazz, name, isStatic))
        return;
    final PsiMethod getter = GroovyPropertyUtils.findPropertyGetter(clazz, name, isStatic, true);
    final PsiType returnType;
    if (getter instanceof GrMethod) {
        returnType = ((GrMethod) getter).getInferredReturnType();
    } else if (getter instanceof GrAccessorMethod) {
        returnType = ((GrAccessorMethod) getter).getInferredReturnType();
    } else {
        return;
    }
    if (!(returnType instanceof GrClosureType))
        return;
    final GrSignature signature = ((GrClosureType) returnType).getSignature();
    signature.accept(new GrRecursiveSignatureVisitor() {

        @Override
        public void visitClosureSignature(GrClosureSignature signature) {
            NextSignature: for (MethodSignature prototypeSignature : prototypeSignatures) {
                final GrClosureParameter[] params = signature.getParameters();
                final PsiType[] types = prototypeSignature.getParameterTypes();
                if (types.length != params.length)
                    continue;
                for (int i = 0; i < types.length; i++) {
                    if (!TypesUtil.isAssignableByMethodCallConversion(types[i], params[i].getType(), refactoredMethod.getParameterList())) {
                        continue NextSignature;
                    }
                }
                conflicts.putValue(getter, GroovyRefactoringBundle.message("refactored.method.will.cover.closure.property", name, RefactoringUIUtil.getDescription(getter.getContainingClass(), false)));
            }
        }
    });
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrRecursiveSignatureVisitor(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 8 with GrClosureType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType in project intellij-community by JetBrains.

the class DefaultCallExpressionTypeCalculator method extractReturnTypeFromType.

@Nullable
private static PsiType extractReturnTypeFromType(PsiType type, boolean returnTypeIfFail, GrMethodCall callExpression) {
    PsiType returnType = returnTypeIfFail ? type : null;
    if (type instanceof GrClosureType) {
        returnType = GrClosureSignatureUtil.getReturnType(((GrClosureType) type).getSignature(), callExpression);
    } else if (TypesUtil.isPsiClassTypeToClosure(type)) {
        assert type instanceof PsiClassType;
        final PsiType[] parameters = ((PsiClassType) type).getParameters();
        if (parameters.length == 1) {
            returnType = parameters[0];
        }
    } else if (type instanceof PsiClassType) {
        final GrExpression invoked = callExpression.getInvokedExpression();
        final GroovyResolveResult[] calls = ResolveUtil.getMethodCandidates(type, "call", invoked != null ? invoked : callExpression, PsiUtil.getArgumentTypes(invoked, false));
        returnType = null;
        final PsiManager manager = callExpression.getManager();
        for (GroovyResolveResult call : calls) {
            final PsiType substituted = ResolveUtil.extractReturnTypeFromCandidate(call, callExpression, PsiUtil.getArgumentTypes(invoked, true));
            returnType = TypesUtil.getLeastUpperBoundNullable(returnType, substituted, manager);
        }
    }
    return returnType;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrClosureType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType in project intellij-community by JetBrains.

the class GdkMethodUtil method getTypeToMix.

@Nullable
private static Pair<GrSignature, String> getTypeToMix(GrAssignmentExpression assignment) {
    GrExpression mixinRef = assignment.getRValue();
    if (mixinRef == null)
        return null;
    final PsiType type = mixinRef.getType();
    if (type instanceof GrClosureType) {
        final GrSignature signature = ((GrClosureType) type).getSignature();
        final GrExpression lValue = assignment.getLValue();
        assert lValue instanceof GrReferenceExpression;
        final String name = ((GrReferenceExpression) lValue).getReferenceName();
        return Pair.create(signature, name);
    }
    return null;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrClosureType

use of org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType in project intellij-community by JetBrains.

the class ClosureMemberContributor method processDynamicElements.

@Override
public final void processDynamicElements(@NotNull PsiType qualifierType, @NotNull PsiScopeProcessor processor, @NotNull PsiElement place, @NotNull ResolveState state) {
    if (!(qualifierType instanceof GrClosureType))
        return;
    final PsiElement context = state.get(ClassHint.RESOLVE_CONTEXT);
    if (!(context instanceof GrClosableBlock))
        return;
    processMembers((GrClosableBlock) context, processor, place, state);
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Aggregations

GrClosureType (org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)11 Nullable (org.jetbrains.annotations.Nullable)6 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 MethodSignature (com.intellij.psi.util.MethodSignature)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)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 PsiElement (com.intellij.psi.PsiElement)1 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)1 HashSet (com.intellij.util.containers.HashSet)1 MultiMap (com.intellij.util.containers.MultiMap)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)1 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)1