001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.psi;
018    
019    import com.google.common.collect.Lists;
020    import com.intellij.lang.ASTNode;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.KtNodeTypes;
024    
025    import java.util.Collections;
026    import java.util.List;
027    
028    public class KtCallExpression extends KtExpressionImpl implements KtCallElement, KtReferenceExpression {
029        public KtCallExpression(@NotNull ASTNode node) {
030            super(node);
031        }
032    
033        @Override
034        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
035            return visitor.visitCallExpression(this, data);
036        }
037    
038        @Override
039        @Nullable
040        public KtExpression getCalleeExpression() {
041            return findChildByClass(KtExpression.class);
042        }
043    
044        @Override
045        @Nullable
046        public KtValueArgumentList getValueArgumentList() {
047            return (KtValueArgumentList) findChildByType(KtNodeTypes.VALUE_ARGUMENT_LIST);
048        }
049    
050        @Override
051        @Nullable
052        public KtTypeArgumentList getTypeArgumentList() {
053            return (KtTypeArgumentList) findChildByType(KtNodeTypes.TYPE_ARGUMENT_LIST);
054        }
055    
056        /**
057         * Normally there should be only one (or zero) function literal arguments.
058         * The returned value is a list for better handling of commonly made mistake of a function taking a lambda and returning another function.
059         * Most of users can simply ignore lists of more than one element.
060         */
061        @Override
062        @NotNull
063        public List<KtLambdaArgument> getLambdaArguments() {
064            return findChildrenByType(KtNodeTypes.LAMBDA_ARGUMENT);
065        }
066    
067        @Override
068        @NotNull
069        public List<KtValueArgument> getValueArguments() {
070            KtValueArgumentList list = getValueArgumentList();
071            List<KtValueArgument> valueArgumentsInParentheses = list != null ? list.getArguments() : Collections.<KtValueArgument>emptyList();
072            List<KtLambdaArgument> functionLiteralArguments = getLambdaArguments();
073            if (functionLiteralArguments.isEmpty()) {
074                return valueArgumentsInParentheses;
075            }
076            List<KtValueArgument> allValueArguments = Lists.newArrayList();
077            allValueArguments.addAll(valueArgumentsInParentheses);
078            allValueArguments.addAll(functionLiteralArguments);
079            return allValueArguments;
080        }
081    
082        @Override
083        @NotNull
084        public List<KtTypeProjection> getTypeArguments() {
085            KtTypeArgumentList list = getTypeArgumentList();
086            return list != null ? list.getArguments() : Collections.<KtTypeProjection>emptyList();
087        }
088    }