001    /*
002     * Copyright 2010-2013 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.jet.lang.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.tree.IElementType;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.JetNodeTypes;
025    import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
026    import org.jetbrains.jet.lexer.JetTokens;
027    
028    import java.util.Collections;
029    import java.util.List;
030    
031    abstract public class JetFunctionNotStubbed extends JetTypeParameterListOwnerNotStubbed
032            implements JetFunction {
033    
034        public JetFunctionNotStubbed(@NotNull ASTNode node) {
035            super(node);
036        }
037    
038        @Override
039        @Nullable
040        public JetParameterList getValueParameterList() {
041            return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
042        }
043    
044        @Override
045        @NotNull
046        public List<JetParameter> getValueParameters() {
047            JetParameterList list = getValueParameterList();
048            return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
049        }
050    
051        @Override
052        @Nullable
053        public JetExpression getBodyExpression() {
054            return findChildByClass(JetExpression.class);
055        }
056    
057        @Override
058        public boolean hasDeclaredReturnType() {
059            return getTypeReference() != null;
060        }
061    
062        @Override
063        @Nullable
064        public JetTypeReference getReceiverTypeReference() {
065            PsiElement child = getFirstChild();
066            while (child != null) {
067                IElementType tt = child.getNode().getElementType();
068                if (tt == JetTokens.LPAR || tt == JetTokens.COLON) break;
069                if (child instanceof JetTypeReference) {
070                    return (JetTypeReference) child;
071                }
072                child = child.getNextSibling();
073            }
074    
075            return null;
076        }
077    
078        @Override
079        @Nullable
080        public JetTypeReference getTypeReference() {
081            return TypeRefHelpersPackage.getTypeReference(this);
082        }
083    
084        @Nullable
085        @Override
086        public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
087            return TypeRefHelpersPackage.setTypeReference(this, getValueParameterList(), typeRef);
088        }
089    
090        @Override
091        public boolean isLocal() {
092            PsiElement parent = getParent();
093            return !(parent instanceof JetFile || parent instanceof JetClassBody);
094        }
095    }