Search in sources :

Example 46 with GrArgumentList

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

the class GrIntroduceClosureParameterProcessor method processChangedMethodCall.

private static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings) {
    if (element.getParent() instanceof GrMethodCallExpression) {
        GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
        GrExpression expression = factory.createExpressionFromText(settings.getName(), null);
        final GrArgumentList argList = methodCall.getArgumentList();
        final PsiElement[] exprs = argList.getAllArguments();
        if (exprs.length > 0) {
            argList.addAfter(expression, exprs[exprs.length - 1]);
        } else {
            argList.add(expression);
        }
        removeParametersFromCall(methodCall, settings);
    } else {
        LOG.error(element.getParent());
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Example 47 with GrArgumentList

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

the class ExpressionGenerator method visitNewExpression.

@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
    boolean hasFieldInitialization = hasFieldInitialization(newExpression);
    StringBuilder builder;
    final PsiType type = newExpression.getType();
    final String varName;
    if (hasFieldInitialization) {
        builder = new StringBuilder();
        varName = GenerationUtil.suggestVarName(type, newExpression, context);
        TypeWriter.writeType(builder, type, newExpression);
        builder.append(' ').append(varName).append(" = ");
    } else {
        varName = null;
        builder = this.builder;
    }
    final GrTypeElement typeElement = newExpression.getTypeElement();
    final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
    final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
    builder.append("new ");
    if (typeElement != null) {
        final PsiType builtIn = typeElement.getType();
        LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
        final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
        TypeWriter.writeTypeForNew(builder, boxed, newExpression);
    } else if (referenceElement != null) {
        GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
    }
    final GrArgumentList argList = newExpression.getArgumentList();
    if (argList != null) {
        GrClosureSignature signature = null;
        final GroovyResolveResult resolveResult = newExpression.advancedResolve();
        final PsiElement constructor = resolveResult.getElement();
        if (constructor instanceof PsiMethod) {
            signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
        } else if (referenceElement != null) {
            final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
            final PsiElement clazz = clazzResult.getElement();
            if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
                signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
            }
        }
        final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
        new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
    }
    final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
    if (anonymous != null) {
        writeTypeBody(builder, anonymous);
    }
    if (arrayDeclaration != null) {
        final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
        for (GrExpression boundExpression : boundExpressions) {
            builder.append('[');
            boundExpression.accept(this);
            builder.append(']');
        }
        if (boundExpressions.length == 0) {
            builder.append("[]");
        }
    }
    if (hasFieldInitialization) {
        builder.append(';');
        context.myStatements.add(builder.toString());
        final GrNamedArgument[] namedArguments = argList.getNamedArguments();
        for (GrNamedArgument namedArgument : namedArguments) {
            final String fieldName = namedArgument.getLabelName();
            if (fieldName == null) {
                //todo try to initialize field
                final GrArgumentLabel label = namedArgument.getLabel();
                LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
            } else {
                final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
                final PsiElement resolved = resolveResult.getElement();
                LOG.assertTrue(resolved instanceof PsiClass);
                initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
            }
        }
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 48 with GrArgumentList

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

the class ClassItemGeneratorImpl method writeEnumConstant.

@Override
public void writeEnumConstant(StringBuilder builder, GrEnumConstant constant) {
    GenerationUtil.writeDocComment(builder, constant, false);
    builder.append(constant.getName());
    final GrArgumentList argumentList = constant.getArgumentList();
    if (argumentList != null) {
        final GroovyResolveResult resolveResult = constant.advancedResolve();
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(resolveResult);
        new ArgumentListGenerator(builder, context.extend()).generate(signature, argumentList.getExpressionArguments(), argumentList.getNamedArguments(), GrClosableBlock.EMPTY_ARRAY, constant);
    }
    final GrEnumConstantInitializer anonymousBlock = constant.getInitializingClass();
    if (anonymousBlock != null) {
        builder.append("{\n");
        new ClassGenerator(classNameProvider, this).writeMembers(builder, anonymousBlock);
        builder.append("\n}");
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrEnumConstantInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumConstantInitializer) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 49 with GrArgumentList

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

the class ExpressionGenerator method visitIndexProperty.

@Override
public void visitIndexProperty(@NotNull GrIndexProperty expression) {
    final GrExpression selectedExpression = expression.getInvokedExpression();
    final PsiType thisType = selectedExpression.getType();
    final GrArgumentList argList = expression.getArgumentList();
    if (argList.getAllArguments().length == 0) {
        // int[] or String[]
        if (selectedExpression instanceof GrBuiltinTypeClassExpression) {
            selectedExpression.accept(this);
            return;
        } else if (selectedExpression instanceof GrReferenceExpression) {
            PsiElement resolved = ((GrReferenceExpression) selectedExpression).resolve();
            if (resolved instanceof PsiClass) {
                builder.append(((PsiClass) resolved).getQualifiedName());
                builder.append("[].class");
                return;
            }
        }
    }
    final PsiType[] argTypes = PsiUtil.getArgumentTypes(argList);
    final GrExpression[] exprArgs = argList.getExpressionArguments();
    final GrNamedArgument[] namedArgs = argList.getNamedArguments();
    if (!PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, expression, PsiUtil.isLValue(expression))) {
        final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
        PsiElement element = candidate.getElement();
        if (element != null || !PsiUtil.isLValue(expression)) {
            //see the case of l-value in assignment expression
            if (element instanceof GrGdkMethod && ((GrGdkMethod) element).getStaticMethod().getParameterList().getParameters()[0].getType().equalsToText("java.util.Map<K,V>")) {
                PsiClass map = JavaPsiFacade.getInstance(context.project).findClass(CommonClassNames.JAVA_UTIL_MAP, expression.getResolveScope());
                if (map != null) {
                    PsiMethod[] gets = map.findMethodsByName("get", false);
                    invokeMethodOn(gets[0], selectedExpression, exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
                    return;
                }
            } else if (element instanceof GrGdkMethod && ((GrGdkMethod) element).getStaticMethod().getParameterList().getParameters()[0].getType().equalsToText("java.util.List<T>")) {
                PsiClass list = JavaPsiFacade.getInstance(context.project).findClass(CommonClassNames.JAVA_UTIL_LIST, expression.getResolveScope());
                if (list != null) {
                    PsiMethod[] gets = list.findMethodsByName("get", false);
                    invokeMethodOn(gets[0], selectedExpression, exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
                    return;
                }
            }
            GenerationUtil.invokeMethodByResolveResult(selectedExpression, candidate, "getAt", exprArgs, namedArgs, GrClosableBlock.EMPTY_ARRAY, this, expression);
            return;
        }
    }
    selectedExpression.accept(this);
    builder.append('[');
    final GrExpression arg = exprArgs[0];
    arg.accept(this);
    builder.append(']');
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 50 with GrArgumentList

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

the class GroovyTypeCheckVisitor method checkConstructorApplicability.

private <T extends GroovyPsiElement> boolean checkConstructorApplicability(@NotNull GroovyResolveResult constructorResolveResult, @NotNull CallInfo<T> info, boolean checkUnknownArgs) {
    final PsiElement element = constructorResolveResult.getElement();
    LOG.assertTrue(element instanceof PsiMethod && ((PsiMethod) element).isConstructor(), element);
    final PsiMethod constructor = (PsiMethod) element;
    final GrArgumentList argList = info.getArgumentList();
    if (argList != null) {
        final GrExpression[] exprArgs = argList.getExpressionArguments();
        if (exprArgs.length == 0 && !PsiUtil.isConstructorHasRequiredParameters(constructor))
            return true;
    }
    PsiType[] types = info.getArgumentTypes();
    PsiClass containingClass = constructor.getContainingClass();
    if (types != null && containingClass != null) {
        final PsiType[] newTypes = GrInnerClassConstructorUtil.addEnclosingArgIfNeeded(types, info.getCall(), containingClass);
        if (newTypes.length != types.length) {
            return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {

                @Nullable
                @Override
                public PsiType[] getArgumentTypes() {
                    return newTypes;
                }
            });
        }
    }
    return checkMethodApplicability(constructorResolveResult, checkUnknownArgs, info);
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

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