Search in sources :

Example 51 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class MissingReturnInspection method getExpectedClosureReturnType.

@Nullable
public static PsiType getExpectedClosureReturnType(GrClosableBlock closure) {
    List<PsiType> expectedReturnTypes = new ArrayList<>();
    PsiElement parent = closure.getParent();
    if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall || parent instanceof GrMethodCall) {
        GrMethodCall call = (GrMethodCall) (parent instanceof GrArgumentList ? parent.getParent() : parent);
        GroovyResolveResult[] variants = call.getCallVariants(null);
        for (GroovyResolveResult variant : variants) {
            Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(variant, closure, true, true, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
            if (map != null) {
                Pair<PsiParameter, PsiType> pair = map.get(closure);
                if (pair == null)
                    continue;
                PsiParameter parameter = pair.getFirst();
                PsiType type = parameter.getType();
                if (TypesUtil.isPsiClassTypeToClosure(type)) {
                    PsiType[] parameters = ((PsiClassType) type).getParameters();
                    if (parameters.length == 1) {
                        expectedReturnTypes.add(parameters[0]);
                    }
                }
            }
        }
    } else {
        for (PsiType expectedType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
            if (TypesUtil.isPsiClassTypeToClosure(expectedType)) {
                PsiType[] parameters = ((PsiClassType) expectedType).getParameters();
                if (parameters.length == 1) {
                    expectedReturnTypes.add(parameters[0]);
                }
            }
        }
    }
    for (PsiType type : expectedReturnTypes) {
        if (PsiType.VOID.equals(type))
            return PsiType.VOID;
    }
    return TypesUtil.getLeastUpperBoundNullable(expectedReturnTypes, closure.getManager());
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class GrConcatenationInjector method findLanguageParams.

private static BaseInjection findLanguageParams(PsiElement place) {
    PsiElement parent = place.getParent();
    if (parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getRValue() == place) {
        final GrExpression lvalue = ((GrAssignmentExpression) parent).getLValue();
        if (lvalue instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
            if (resolved instanceof PsiModifierListOwner) {
                return getLanguageParams((PsiModifierListOwner) resolved);
            }
        }
    } else if (parent instanceof GrVariable) {
        return getLanguageParams((PsiModifierListOwner) parent);
    } else if (parent instanceof GrArgumentList) {
        final PsiElement pparent = parent.getParent();
        if (pparent instanceof GrCall) {
            final GrCall call = (GrCall) pparent;
            final GroovyResolveResult result = call.advancedResolve();
            if (result.getElement() != null) {
                final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(result, place, false, false, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
                if (map != null) {
                    final Pair<PsiParameter, PsiType> pair = map.get(place);
                    return getLanguageParams(pair.first);
                }
            }
        }
    }
    return null;
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair)

Example 53 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class GrChangeSignatureUsageProcessor method processMethodUsage.

private static void processMethodUsage(PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) {
    if (map == null)
        return;
    if (changeInfo.isNameChanged()) {
        if (element instanceof GrReferenceElement) {
            element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName());
        }
    }
    if (toChangeArguments) {
        JavaParameterInfo[] parameters = changeInfo.getNewParameters();
        GrArgumentList argumentList = PsiUtil.getArgumentsList(element);
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
        if (argumentList == null) {
            if (element instanceof GrEnumConstant) {
                argumentList = factory.createArgumentList();
                argumentList = (GrArgumentList) element.add(argumentList);
            } else {
                return;
            }
        }
        Set<PsiElement> argsToDelete = new HashSet<>(map.length * 2);
        for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) {
            argsToDelete.addAll(argInfo.args);
        }
        GrExpression[] values = new GrExpression[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            JavaParameterInfo parameter = parameters[i];
            int index = parameter.getOldIndex();
            if (index >= 0) {
                argsToDelete.removeAll(map[index].args);
            } else {
                values[i] = createDefaultValue(factory, changeInfo, parameter, argumentList, substitutor);
            }
        }
        for (PsiElement arg : argsToDelete) {
            arg.delete();
        }
        boolean skipOptionals = false;
        //PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, GrNamedArgument.class);
        PsiElement anchor = null;
        for (int i = 0; i < parameters.length; i++) {
            JavaParameterInfo parameter = parameters[i];
            int index = parameter.getOldIndex();
            if (index >= 0) {
                GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index];
                List<PsiElement> arguments = argInfo.args;
                if (argInfo.isMultiArg) {
                    //arguments for Map and varArg
                    if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) {
                        final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager());
                        final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject());
                        for (PsiElement argument : arguments) {
                            argument.delete();
                        }
                        anchor = argumentList.addAfter(arg, anchor);
                        JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
                    }
                } else {
                    //arguments for simple parameters
                    if (arguments.size() == 1) {
                        //arg exists
                        PsiElement arg = arguments.iterator().next();
                        if (i == parameters.length - 1 && parameter.isVarargType()) {
                            if (arg instanceof GrSafeCastExpression) {
                                PsiElement expr = ((GrSafeCastExpression) arg).getOperand();
                                if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) {
                                    final PsiElement copy = expr.copy();
                                    PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers();
                                    for (PsiElement vararg : newVarargs) {
                                        anchor = argumentList.addAfter(vararg, anchor);
                                    }
                                    arg.delete();
                                    continue;
                                }
                            }
                        }
                        PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class);
                        if (curArg == arg) {
                            anchor = arg;
                        } else {
                            final PsiElement copy = arg.copy();
                            anchor = argumentList.addAfter(copy, anchor);
                            arg.delete();
                        }
                    } else {
                        //arg is skipped. Parameter is optional
                        skipOptionals = true;
                    }
                }
            } else {
                if (skipOptionals && isParameterOptional(parameter))
                    continue;
                if (forceOptional(parameter)) {
                    skipOptionals = true;
                    continue;
                }
                try {
                    final GrExpression value = values[i];
                    if (i > 0 && (value == null || anchor == null)) {
                        PsiElement comma = Factory.createSingleLeafElement(GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()).getPsi();
                        if (anchor == null)
                            anchor = argumentList.getLeftParen();
                        anchor = argumentList.addAfter(comma, anchor);
                    }
                    if (value != null) {
                        anchor = argumentList.addAfter(value, anchor);
                    }
                } catch (IncorrectOperationException e) {
                    LOG.error(e.getMessage());
                }
            }
        }
        for (PsiElement arg : argsToDelete) {
            arg.delete();
        }
        GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
        if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) {
            argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList());
        }
        CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList);
    }
    if (toCatchExceptions) {
        final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions();
        PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager());
        fixExceptions(element, exceptions);
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 54 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class ParameterCastFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    final PsiElement element = descriptor.getPsiElement();
    final GrArgumentList list = element instanceof GrArgumentList ? (GrArgumentList) element : PsiUtil.getArgumentsList(element);
    if (list == null)
        return;
    GrCastFix.doCast(project, myType, myArgument);
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) PsiElement(com.intellij.psi.PsiElement)

Example 55 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class RecursionUtils method callExpressionDefinitelyRecurses.

private static boolean callExpressionDefinitelyRecurses(GrCallExpression exp, GrMethod method) {
    final GrArgumentList argumentList = exp.getArgumentList();
    if (argumentList != null) {
        final GrExpression[] args = argumentList.getExpressionArguments();
        for (final GrExpression arg : args) {
            if (expressionDefinitelyRecurses(arg, method)) {
                return true;
            }
        }
        final GrNamedArgument[] namedArgs = argumentList.getNamedArguments();
        for (final GrNamedArgument arg : namedArgs) {
            if (expressionDefinitelyRecurses(arg.getExpression(), method)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Aggregations

GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)80 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)21 Nullable (org.jetbrains.annotations.Nullable)20 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)15 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)14 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)14 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)9 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)8 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)8 NotNull (org.jetbrains.annotations.NotNull)7 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)6 ASTNode (com.intellij.lang.ASTNode)5