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.asJava;
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.idea.JetLanguage;
026 import org.jetbrains.kotlin.psi.JetTypeParameter;
027 import org.jetbrains.kotlin.psi.JetTypeParameterListOwner;
028
029 public class KotlinLightTypeParameter
030 extends AbstractLightClass implements PsiTypeParameter, KotlinLightElement<JetTypeParameter, PsiTypeParameter> {
031 private final PsiTypeParameterListOwner owner;
032 private final int index;
033 private final String name;
034
035 protected KotlinLightTypeParameter(
036 @NotNull PsiTypeParameterListOwner owner,
037 int index,
038 @NotNull String name) {
039 super(owner.getManager(), JetLanguage.INSTANCE);
040 this.owner = owner;
041 this.index = index;
042 this.name = name;
043 }
044
045 @NotNull
046 @Override
047 public PsiTypeParameter getDelegate() {
048 return getOwnerDelegate().getTypeParameters()[index];
049 }
050
051 @NotNull
052 @Override
053 public JetTypeParameter getOrigin() {
054 JetTypeParameterListOwner jetOwner = (JetTypeParameterListOwner) AsJavaPackage.getUnwrapped(owner);
055 assert (jetOwner != null) : "Invalid type parameter owner: " + owner;
056
057 return jetOwner.getTypeParameters().get(index);
058 }
059
060 @NotNull
061 private PsiTypeParameterListOwner getOwnerDelegate() {
062 if (owner instanceof KotlinLightClass) return ((KotlinLightClass) owner).getDelegate();
063 if (owner instanceof KotlinLightMethod) return ((KotlinLightMethod) owner).getDelegate();
064 return owner;
065 }
066
067 @NotNull
068 @Override
069 public PsiElement copy() {
070 return new KotlinLightTypeParameter(owner, index, name);
071 }
072
073 @Override
074 public void accept(@NotNull PsiElementVisitor visitor) {
075 if (visitor instanceof JavaElementVisitor) {
076 ((JavaElementVisitor) visitor).visitTypeParameter(this);
077 }
078 else {
079 super.accept(visitor);
080 }
081 }
082
083 @Nullable
084 @Override
085 public String getName() {
086 return name;
087 }
088
089 @Override
090 public PsiTypeParameterListOwner getOwner() {
091 return owner;
092 }
093
094 @Override
095 public int getIndex() {
096 return index;
097 }
098
099 @NotNull
100 @Override
101 public PsiAnnotation[] getAnnotations() {
102 return getDelegate().getAnnotations();
103 }
104
105 @NotNull
106 @Override
107 public PsiAnnotation[] getApplicableAnnotations() {
108 return getDelegate().getApplicableAnnotations();
109 }
110
111 @Override
112 public PsiAnnotation findAnnotation(@NotNull String qualifiedName) {
113 return getDelegate().findAnnotation(qualifiedName);
114 }
115
116 @NotNull
117 @Override
118 public PsiAnnotation addAnnotation(@NotNull String qualifiedName) {
119 return getDelegate().addAnnotation(qualifiedName);
120 }
121
122 @Override
123 public String toString() {
124 return "KotlinLightTypeParameter:" + getName();
125 }
126
127 @NotNull
128 @Override
129 public PsiElement getNavigationElement() {
130 return getOrigin();
131 }
132
133 @NotNull
134 @Override
135 public Language getLanguage() {
136 return JetLanguage.INSTANCE;
137 }
138
139 @NotNull
140 @Override
141 public SearchScope getUseScope() {
142 return getOrigin().getUseScope();
143 }
144
145 @Override
146 public boolean equals(Object obj) {
147 if (obj == this) return true;
148 return obj instanceof KotlinLightTypeParameter && getOrigin().equals(((KotlinLightTypeParameter) obj).getOrigin());
149 }
150 }