Search in sources :

Example 6 with GrClosureParameter

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

the class GdkMethodUtil method createMethod.

@NotNull
private static GrMethod createMethod(@NotNull GrClosureSignature signature, @NotNull String name, @NotNull GrAssignmentExpression statement, @NotNull PsiClass closure) {
    final GrLightMethodBuilder builder = new GrLightMethodBuilder(statement.getManager(), name);
    GrClosureParameter[] parameters = signature.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        GrClosureParameter parameter = parameters[i];
        final String parameterName = parameter.getName() != null ? parameter.getName() : "p" + i;
        final PsiType type = parameter.getType() != null ? parameter.getType() : TypesUtil.getJavaLangObject(statement);
        builder.addParameter(parameterName, type, parameter.isOptional());
    }
    builder.setNavigationElement(statement.getLValue());
    builder.setReturnType(signature.getReturnType());
    builder.setContainingClass(closure);
    return builder;
}
Also used : GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder) ElementClassHint(com.intellij.psi.scope.ElementClassHint) ClassHint(org.jetbrains.plugins.groovy.lang.resolve.processors.ClassHint) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GrClosureParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter 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 8 with GrClosureParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter 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 9 with GrClosureParameter

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

the class GroovyParameterInfoHandler method updateUI.

@Override
public void updateUI(Object o, @NotNull ParameterInfoUIContext context) {
    CodeInsightSettings settings = CodeInsightSettings.getInstance();
    if (o == null)
        return;
    Object element;
    if (o instanceof GroovyResolveResult) {
        element = ((GroovyResolveResult) o).getElement();
        if (element == null || !((PsiElement) element).isValid()) {
            context.setUIComponentEnabled(false);
            return;
        }
    } else if (o instanceof GrClosureSignature) {
        if (!((GrClosureSignature) o).isValid()) {
            context.setUIComponentEnabled(false);
            return;
        }
        element = o;
    } else {
        return;
    }
    int highlightStartOffset = -1;
    int highlightEndOffset = -1;
    final int currentParameter = context.getCurrentParameterIndex();
    StringBuilder buffer = new StringBuilder();
    if (element instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) element;
        if (method instanceof GrReflectedMethod)
            method = ((GrReflectedMethod) method).getBaseMethod();
        if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
            if (!method.isConstructor()) {
                PsiType returnType = PsiUtil.getSmartReturnType(method);
                if (returnType != null) {
                    buffer.append(returnType.getPresentableText());
                    buffer.append(' ');
                }
            }
            buffer.append(method.getName());
            buffer.append('(');
        }
        PsiParameter[] params = method.getParameterList().getParameters();
        params = updateConstructorParams(method, params, context.getParameterOwner());
        int numParams = params.length;
        if (numParams > 0) {
            LOG.assertTrue(o instanceof GroovyResolveResult, o.getClass());
            final PsiSubstitutor substitutor = ((GroovyResolveResult) o).getSubstitutor();
            for (int j = 0; j < numParams; j++) {
                PsiParameter param = params[j];
                int startOffset = buffer.length();
                appendParameterText(param, substitutor, buffer);
                int endOffset = buffer.length();
                if (j < numParams - 1) {
                    buffer.append(", ");
                }
                if (context.isUIComponentEnabled() && (j == currentParameter || (j == numParams - 1 && param.isVarArgs() && currentParameter >= numParams))) {
                    highlightStartOffset = startOffset;
                    highlightEndOffset = endOffset;
                }
            }
        } else {
            buffer.append("no parameters");
        }
        if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
            buffer.append(")");
        }
    } else if (element instanceof PsiClass) {
        buffer.append("no parameters");
    } else if (element instanceof GrClosureSignature) {
        GrClosureParameter[] parameters = ((GrClosureSignature) element).getParameters();
        if (parameters.length > 0) {
            for (int i = 0; i < parameters.length; i++) {
                if (i > 0)
                    buffer.append(", ");
                int startOffset = buffer.length();
                final PsiType psiType = parameters[i].getType();
                if (psiType == null) {
                    buffer.append("def");
                } else {
                    buffer.append(psiType.getPresentableText());
                }
                buffer.append(' ').append(parameters[i].getName() != null ? parameters[i].getName() : "<unknown>");
                int endOffset = buffer.length();
                if (context.isUIComponentEnabled() && (i == currentParameter || (i == parameters.length - 1 && ((GrClosureSignature) element).isVarargs() && currentParameter >= parameters.length))) {
                    highlightStartOffset = startOffset;
                    highlightEndOffset = endOffset;
                }
                final GrExpression initializer = parameters[i].getDefaultInitializer();
                if (initializer != null) {
                    buffer.append(" = ").append(initializer.getText());
                }
            }
        } else {
            buffer.append("no parameters");
        }
    }
    final boolean isDeprecated = o instanceof PsiDocCommentOwner && ((PsiDocCommentOwner) o).isDeprecated();
    context.setupUIComponentPresentation(buffer.toString(), highlightStartOffset, highlightEndOffset, !context.isUIComponentEnabled(), isDeprecated, false, context.getDefaultParameterColor());
}
Also used : CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) 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) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 10 with GrClosureParameter

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

the class GroovyParameterInfoHandler method updateParameterInfo.

@Override
public void updateParameterInfo(@NotNull GroovyPsiElement place, @NotNull UpdateParameterInfoContext context) {
    final PsiElement parameterOwner = context.getParameterOwner();
    if (parameterOwner != place) {
        context.removeHint();
        return;
    }
    int offset = context.getEditor().getCaretModel().getOffset();
    offset = CharArrayUtil.shiftForward(context.getEditor().getDocument().getText(), offset, " \t\n");
    final int currIndex = getCurrentParameterIndex(place, offset);
    context.setCurrentParameter(currIndex);
    final Object[] objects = context.getObjectsToView();
    Outer: for (int i = 0; i < objects.length; i++) {
        PsiType[] parameterTypes = null;
        PsiType[] argTypes = null;
        PsiSubstitutor substitutor = null;
        if (objects[i] instanceof GroovyResolveResult) {
            final GroovyResolveResult resolveResult = (GroovyResolveResult) objects[i];
            PsiNamedElement namedElement = (PsiNamedElement) resolveResult.getElement();
            if (namedElement instanceof GrReflectedMethod)
                namedElement = ((GrReflectedMethod) namedElement).getBaseMethod();
            substitutor = resolveResult.getSubstitutor();
            assert namedElement != null;
            if (!namedElement.isValid()) {
                context.setUIComponentEnabled(i, false);
                continue Outer;
            }
            if (namedElement instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) namedElement;
                PsiParameter[] parameters = method.getParameterList().getParameters();
                parameters = updateConstructorParams(method, parameters, context.getParameterOwner());
                parameterTypes = PsiType.createArray(parameters.length);
                for (int j = 0; j < parameters.length; j++) {
                    parameterTypes[j] = parameters[j].getType();
                }
                argTypes = PsiUtil.getArgumentTypes(place, false);
            }
            if (argTypes == null)
                continue;
        } else if (objects[i] instanceof GrClosureSignature) {
            final GrClosureSignature signature = (GrClosureSignature) objects[i];
            argTypes = PsiUtil.getArgumentTypes(place, false);
            parameterTypes = PsiType.createArray(signature.getParameterCount());
            int j = 0;
            for (GrClosureParameter parameter : signature.getParameters()) {
                parameterTypes[j++] = parameter.getType();
            }
        } else {
            continue Outer;
        }
        assert argTypes != null;
        if (argTypes.length > currIndex) {
            if (parameterTypes.length <= currIndex) {
                context.setUIComponentEnabled(i, false);
                continue;
            } else {
                for (int j = 0; j < currIndex; j++) {
                    PsiType argType = argTypes[j];
                    final PsiType paramType = substitutor != null ? substitutor.substitute(parameterTypes[j]) : parameterTypes[j];
                    if (!TypesUtil.isAssignableByMethodCallConversion(paramType, argType, place)) {
                        context.setUIComponentEnabled(i, false);
                        break Outer;
                    }
                }
            }
        }
        context.setUIComponentEnabled(i, true);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)12 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)2 GrReflectedMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod)2 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)2 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiType (com.intellij.psi.PsiType)1 ElementClassHint (com.intellij.psi.scope.ElementClassHint)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)1 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)1