Search in sources :

Example 21 with GrClosureSignature

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

the class GdkMethodUtil method getClosureMixins.

private static Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>> getClosureMixins(final GrStatement statement) {
    if (!(statement instanceof GrAssignmentExpression))
        return null;
    final GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
    return CachedValuesManager.getCachedValue(statement, new CachedValueProvider<Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>>>() {

        @Nullable
        @Override
        public Result<Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>>> compute() {
            Pair<PsiClassType, GrReferenceExpression> original = getTypeToMixIn(assignment);
            if (original == null)
                return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
            final Pair<GrSignature, String> signatures = getTypeToMix(assignment);
            if (signatures == null)
                return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
            final String name = signatures.second;
            final List<GrMethod> methods = ContainerUtil.newArrayList();
            final PsiClass closure = JavaPsiFacade.getInstance(statement.getProject()).findClass(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, statement.getResolveScope());
            if (closure == null)
                return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
            signatures.first.accept(new GrSignatureVisitor() {

                @Override
                public void visitClosureSignature(GrClosureSignature signature) {
                    super.visitClosureSignature(signature);
                    GrMethod method = createMethod(signature, name, assignment, closure);
                    methods.add(method);
                }
            });
            return Result.create(Trinity.create(original.first, original.second, methods), PsiModificationTracker.MODIFICATION_COUNT);
        }
    });
}
Also used : Trinity(com.intellij.openapi.util.Trinity) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSignatureVisitor(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignatureVisitor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) List(java.util.List) Nullable(org.jetbrains.annotations.Nullable) Pair(com.intellij.openapi.util.Pair)

Example 22 with GrClosureSignature

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

the class PsiUtil method isApplicableConcrete.

public static GrClosureSignatureUtil.ApplicabilityResult isApplicableConcrete(@Nullable PsiType[] argumentTypes, PsiMethod method, PsiSubstitutor substitutor, PsiElement place, final boolean eraseParameterTypes) {
    if (argumentTypes == null)
        return GrClosureSignatureUtil.ApplicabilityResult.canBeApplicable;
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor, eraseParameterTypes);
    //check for default constructor
    if (method.isConstructor()) {
        final PsiParameter[] parameters = method.getParameterList().getParameters();
        if (parameters.length == 0 && argumentTypes.length == 1) {
            return InheritanceUtil.isInheritor(argumentTypes[0], CommonClassNames.JAVA_UTIL_MAP) ? GrClosureSignatureUtil.ApplicabilityResult.applicable : GrClosureSignatureUtil.ApplicabilityResult.inapplicable;
        }
        if (parameters.length == 1 && argumentTypes.length == 0 && InheritanceUtil.isInheritor(parameters[0].getType(), CommonClassNames.JAVA_UTIL_MAP)) {
            return GrClosureSignatureUtil.ApplicabilityResult.inapplicable;
        }
    }
    LOG.assertTrue(signature != null);
    GrClosureSignatureUtil.ApplicabilityResult result = GrClosureSignatureUtil.isSignatureApplicableConcrete(signature, argumentTypes, place);
    if (result != GrClosureSignatureUtil.ApplicabilityResult.inapplicable) {
        return result;
    }
    if (method instanceof GrBuilderMethod && !((GrBuilderMethod) method).hasObligatoryNamedArguments()) {
        final PsiParameter[] parameters = method.getParameterList().getParameters();
        if (parameters.length > 0 && parameters[0].getType() instanceof GrMapType && (argumentTypes.length == 0 || !(argumentTypes[0] instanceof GrMapType))) {
            return GrClosureSignatureUtil.isSignatureApplicableConcrete(GrClosureSignatureUtil.removeParam(signature, 0), argumentTypes, place);
        }
    }
    return GrClosureSignatureUtil.ApplicabilityResult.inapplicable;
}
Also used : GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 23 with GrClosureSignature

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

the class SubstitutorComputer method inferMethodTypeParameters.

private PsiSubstitutor inferMethodTypeParameters(@NotNull PsiMethod method, @NotNull PsiSubstitutor partialSubstitutor, @NotNull PsiTypeParameter[] typeParameters, @NotNull PsiType[] argTypes) {
    if (typeParameters.length == 0 || myArgumentTypes == null)
        return partialSubstitutor;
    final GrClosureSignature erasedSignature = GrClosureSignatureUtil.createSignature(method, partialSubstitutor, true);
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, partialSubstitutor);
    final GrClosureParameter[] params = signature.getParameters();
    final GrClosureSignatureUtil.ArgInfo<PsiType>[] argInfos = GrClosureSignatureUtil.mapArgTypesToParameters(erasedSignature, argTypes, myPlace, true);
    if (argInfos == null)
        return partialSubstitutor;
    int max = Math.max(params.length, argTypes.length);
    PsiType[] parameterTypes = PsiType.createArray(max);
    PsiType[] argumentTypes = PsiType.createArray(max);
    int i = 0;
    for (int paramIndex = 0; paramIndex < argInfos.length; paramIndex++) {
        PsiType paramType = params[paramIndex].getType();
        GrClosureSignatureUtil.ArgInfo<PsiType> argInfo = argInfos[paramIndex];
        if (argInfo != null) {
            if (argInfo.isMultiArg) {
                if (paramType instanceof PsiArrayType)
                    paramType = ((PsiArrayType) paramType).getComponentType();
            }
            for (PsiType type : argInfo.args) {
                argumentTypes[i] = handleConversion(paramType, type);
                parameterTypes[i] = paramType;
                i++;
            }
        } else {
            parameterTypes[i] = paramType;
            argumentTypes[i] = PsiType.NULL;
            i++;
        }
    }
    PsiSubstitutor substitutor = myHelper.inferTypeArguments(typeParameters, parameterTypes, argumentTypes, LanguageLevel.JDK_1_7);
    for (PsiTypeParameter typeParameter : typeParameters) {
        if (!substitutor.getSubstitutionMap().containsKey(typeParameter)) {
            substitutor = inferFromContext(typeParameter, PsiUtil.getSmartReturnType(method), substitutor);
            if (!substitutor.getSubstitutionMap().containsKey(typeParameter)) {
                substitutor = substitutor.put(typeParameter, null);
            }
        }
    }
    return partialSubstitutor.putAll(substitutor);
}
Also used : GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 24 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature 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 25 with GrClosureSignature

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

the class DefaultDelegatesToProvider method getDelegatesToInfo.

@Nullable
public DelegatesToInfo getDelegatesToInfo(@NotNull final GrClosableBlock closableBlock) {
    GrCall call = getContainingCall(closableBlock);
    if (call == null)
        return null;
    final GroovyResolveResult result = resolveActualCall(call);
    final PsiElement element = result.getElement();
    if (GdkMethodUtil.isWithOrIdentity(element)) {
        final GrExpression qualifier = inferCallQualifier((GrMethodCall) call);
        if (qualifier == null)
            return null;
        return new DelegatesToInfo(qualifier.getType(), Closure.DELEGATE_FIRST);
    }
    final GrClosureSignature signature = inferSignature(element);
    if (signature == null)
        return null;
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] map = mapArgs(closableBlock, call, signature);
    if (map == null)
        return null;
    if (!(element instanceof PsiMethod))
        return null;
    final PsiMethod method = (PsiMethod) element;
    final PsiParameterList parameterList = method.getParameterList();
    final PsiParameter parameter = findParameter(parameterList, closableBlock, map);
    if (parameter == null)
        return null;
    final String delegateFqnData = parameter.getUserData(DELEGATES_TO_KEY);
    final Integer strategyData = parameter.getUserData(DELEGATES_TO_STRATEGY_KEY);
    if (delegateFqnData != null) {
        return new DelegatesToInfo(TypesUtil.createType(delegateFqnData, closableBlock), strategyData == null ? Closure.OWNER_FIRST : strategyData);
    }
    final PsiModifierList modifierList = parameter.getModifierList();
    if (modifierList == null)
        return null;
    final PsiAnnotation delegatesTo = modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO);
    if (delegatesTo == null)
        return null;
    PsiType delegateType = getFromValue(delegatesTo);
    if (delegateType == null)
        delegateType = getFromTarget(parameterList, delegatesTo, signature, map);
    if (delegateType == null)
        delegateType = getFromType(result, delegatesTo);
    final int strategyValue = getStrategyValue(delegatesTo.findAttributeValue("strategy"));
    return new DelegatesToInfo(delegateType, strategyValue);
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)8 ArrayList (java.util.ArrayList)6 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)6 GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)6 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 MethodSignature (com.intellij.psi.util.MethodSignature)4 List (java.util.List)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)4 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)4 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 Pair (com.intellij.openapi.util.Pair)3 TIntProcedure (gnu.trove.TIntProcedure)3