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.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.kotlin.lexer.KtTokens;
025    import org.jetbrains.kotlin.psi.stubs.KotlinTypeProjectionStub;
026    import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
027    
028    public class KtTypeProjection extends KtModifierListOwnerStub<KotlinTypeProjectionStub> {
029    
030        public KtTypeProjection(@NotNull ASTNode node) {
031            super(node);
032        }
033    
034        public KtTypeProjection(@NotNull KotlinTypeProjectionStub stub) {
035            super(stub, KtStubElementTypes.TYPE_PROJECTION);
036        }
037    
038        @NotNull
039        public KtProjectionKind getProjectionKind() {
040            KotlinTypeProjectionStub stub = getStub();
041            if (stub != null) {
042                return stub.getProjectionKind();
043            }
044    
045            PsiElement projectionToken = getProjectionToken();
046            IElementType token = projectionToken != null ? projectionToken.getNode().getElementType() : null;
047            for (KtProjectionKind projectionKind : KtProjectionKind.values()) {
048                if (projectionKind.getToken() == token) {
049                    return projectionKind;
050                }
051            }
052            throw new IllegalStateException(projectionToken.getText());
053        }
054    
055        @Override
056        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
057            return visitor.visitTypeProjection(this, data);
058        }
059    
060        @Nullable
061        public KtTypeReference getTypeReference() {
062            return getStubOrPsiChild(KtStubElementTypes.TYPE_REFERENCE);
063        }
064    
065        @Nullable
066        public PsiElement getProjectionToken() {
067            PsiElement star = findChildByType(KtTokens.MUL);
068            if (star != null) {
069                return star;
070            }
071    
072            KtModifierList modifierList = getModifierList();
073            if (modifierList != null) {
074                PsiElement element = modifierList.getModifier(KtTokens.IN_KEYWORD);
075                if (element != null) return element;
076    
077                element = modifierList.getModifier(KtTokens.OUT_KEYWORD);
078                if (element != null) return element;
079            }
080    
081            return null;
082        }
083    }