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.search.LocalSearchScope;
022    import com.intellij.psi.search.SearchScope;
023    import com.intellij.psi.util.PsiTreeUtil;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.kotlin.lexer.KtTokens;
027    import org.jetbrains.kotlin.psi.stubs.KotlinTypeParameterStub;
028    import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
029    import org.jetbrains.kotlin.types.Variance;
030    
031    public class KtTypeParameter extends KtNamedDeclarationStub<KotlinTypeParameterStub> {
032    
033        public KtTypeParameter(@NotNull ASTNode node) {
034            super(node);
035        }
036    
037        public KtTypeParameter(@NotNull KotlinTypeParameterStub stub) {
038            super(stub, KtStubElementTypes.TYPE_PARAMETER);
039        }
040    
041        @Override
042        public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
043            return visitor.visitTypeParameter(this, data);
044        }
045    
046        @NotNull
047        public Variance getVariance() {
048            KotlinTypeParameterStub stub = getStub();
049            if (stub != null) {
050                if (stub.isOutVariance()) return Variance.OUT_VARIANCE;
051                if (stub.isInVariance()) return Variance.IN_VARIANCE;
052                return Variance.INVARIANT;
053            }
054    
055            KtModifierList modifierList = getModifierList();
056            if (modifierList == null) return Variance.INVARIANT;
057    
058            if (modifierList.hasModifier(KtTokens.OUT_KEYWORD)) return Variance.OUT_VARIANCE;
059            if (modifierList.hasModifier(KtTokens.IN_KEYWORD)) return Variance.IN_VARIANCE;
060            return Variance.INVARIANT;
061        }
062    
063        @Nullable
064        public KtTypeReference setExtendsBound(@Nullable KtTypeReference typeReference) {
065            KtTypeReference currentExtendsBound = getExtendsBound();
066            if (currentExtendsBound != null) {
067                if (typeReference == null) {
068                    PsiElement colon = findChildByType(KtTokens.COLON);
069                    if (colon != null) colon.delete();
070                    currentExtendsBound.delete();
071                    return null;
072                }
073                return (KtTypeReference) currentExtendsBound.replace(typeReference);
074            }
075    
076            if (typeReference != null) {
077                PsiElement colon = addAfter(new KtPsiFactory(getProject()).createColon(), getNameIdentifier());
078                return (KtTypeReference) addAfter(typeReference, colon);
079            }
080    
081            return null;
082        }
083    
084        @Nullable
085        public KtTypeReference getExtendsBound() {
086            return getStubOrPsiChild(KtStubElementTypes.TYPE_REFERENCE);
087        }
088    
089        @NotNull
090        @Override
091        public SearchScope getUseScope() {
092            KtTypeParameterListOwner owner = PsiTreeUtil.getParentOfType(this, KtTypeParameterListOwner.class);
093            return new LocalSearchScope(owner != null ? owner : this);
094        }
095    }