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