001 /*
002 * Copyright 2010-2016 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.asJava.elements;
018
019 import com.intellij.lang.Language;
020 import com.intellij.psi.*;
021 import com.intellij.psi.impl.light.AbstractLightClass;
022 import com.intellij.psi.search.SearchScope;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.kotlin.asJava.LightClassUtilsKt;
026 import org.jetbrains.kotlin.asJava.classes.KtLightClass;
027 import org.jetbrains.kotlin.idea.KotlinLanguage;
028 import org.jetbrains.kotlin.psi.KtTypeParameter;
029 import org.jetbrains.kotlin.psi.KtTypeParameterListOwner;
030
031 public class KtLightTypeParameter
032 extends AbstractLightClass implements PsiTypeParameter, KtLightDeclaration<KtTypeParameter, PsiTypeParameter> {
033 private final PsiTypeParameterListOwner owner;
034 private final int index;
035 private final String name;
036
037 public KtLightTypeParameter(
038 @NotNull PsiTypeParameterListOwner owner,
039 int index,
040 @NotNull String name) {
041 super(owner.getManager(), KotlinLanguage.INSTANCE);
042 this.owner = owner;
043 this.index = index;
044 this.name = name;
045 }
046
047 @NotNull
048 @Override
049 public PsiTypeParameter getClsDelegate() {
050 return getOwnerDelegate().getTypeParameters()[index];
051 }
052
053 @NotNull
054 @Override
055 public PsiClass getDelegate() {
056 return getClsDelegate();
057 }
058
059 @NotNull
060 @Override
061 public KtTypeParameter getKotlinOrigin() {
062 KtTypeParameterListOwner jetOwner = (KtTypeParameterListOwner) LightClassUtilsKt.getUnwrapped(owner);
063 assert (jetOwner != null) : "Invalid type parameter owner: " + owner;
064
065 return jetOwner.getTypeParameters().get(index);
066 }
067
068 @NotNull
069 private PsiTypeParameterListOwner getOwnerDelegate() {
070 if (owner instanceof KtLightClass) return ((KtLightClass) owner).getClsDelegate();
071 if (owner instanceof KtLightMethod) return ((KtLightMethod) owner).getClsDelegate();
072 return owner;
073 }
074
075 @NotNull
076 @Override
077 public PsiElement copy() {
078 return new KtLightTypeParameter(owner, index, name);
079 }
080
081 @Override
082 public void accept(@NotNull PsiElementVisitor visitor) {
083 if (visitor instanceof JavaElementVisitor) {
084 ((JavaElementVisitor) visitor).visitTypeParameter(this);
085 }
086 else {
087 super.accept(visitor);
088 }
089 }
090
091 @Override
092 public String getText() {
093 return "";
094 }
095
096 @Nullable
097 @Override
098 public String getName() {
099 return name;
100 }
101
102 @Override
103 public PsiTypeParameterListOwner getOwner() {
104 return owner;
105 }
106
107 @Override
108 public int getIndex() {
109 return index;
110 }
111
112 @NotNull
113 @Override
114 public PsiAnnotation[] getAnnotations() {
115 return getClsDelegate().getAnnotations();
116 }
117
118 @NotNull
119 @Override
120 public PsiAnnotation[] getApplicableAnnotations() {
121 return getClsDelegate().getApplicableAnnotations();
122 }
123
124 @Override
125 public PsiAnnotation findAnnotation(@NotNull String qualifiedName) {
126 return getClsDelegate().findAnnotation(qualifiedName);
127 }
128
129 @NotNull
130 @Override
131 public PsiAnnotation addAnnotation(@NotNull String qualifiedName) {
132 return getClsDelegate().addAnnotation(qualifiedName);
133 }
134
135 @Override
136 public String toString() {
137 return "KotlinLightTypeParameter:" + getName();
138 }
139
140 @NotNull
141 @Override
142 public PsiElement getNavigationElement() {
143 return getKotlinOrigin();
144 }
145
146 @NotNull
147 @Override
148 public Language getLanguage() {
149 return KotlinLanguage.INSTANCE;
150 }
151
152 @NotNull
153 @Override
154 public SearchScope getUseScope() {
155 return getKotlinOrigin().getUseScope();
156 }
157
158 @Override
159 public boolean equals(Object obj) {
160 if (obj == this) return true;
161 return obj instanceof KtLightTypeParameter && getKotlinOrigin().equals(((KtLightTypeParameter) obj).getKotlinOrigin());
162 }
163
164 @Override
165 public boolean isEquivalentTo(PsiElement another) {
166 if (another instanceof PsiTypeParameter) {
167 PsiTypeParameter anotherTypeParameter = (PsiTypeParameter) another;
168 PsiTypeParameterListOwner owner = getOwner();
169 if (owner != null) {
170 return owner.isEquivalentTo(anotherTypeParameter.getOwner()) && getIndex() == anotherTypeParameter.getIndex();
171 }
172 }
173 return false;
174 }
175 }