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