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.KotlinLanguage;
026    import org.jetbrains.kotlin.psi.KtTypeParameter;
027    import org.jetbrains.kotlin.psi.KtTypeParameterListOwner;
028    
029    public class KtLightTypeParameter
030            extends AbstractLightClass implements PsiTypeParameter, KtLightElement<KtTypeParameter, PsiTypeParameter> {
031        private final PsiTypeParameterListOwner owner;
032        private final int index;
033        private final String name;
034    
035        protected KtLightTypeParameter(
036                @NotNull PsiTypeParameterListOwner owner,
037                int index,
038                @NotNull String name) {
039            super(owner.getManager(), KotlinLanguage.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 KtTypeParameter getOrigin() {
054            KtTypeParameterListOwner jetOwner = (KtTypeParameterListOwner) LightClassUtilsKt.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 KtLightClass) return ((KtLightClass) owner).getDelegate();
063            if (owner instanceof KtLightMethod) return ((KtLightMethod) owner).getDelegate();
064            return owner;
065        }
066    
067        @NotNull
068        @Override
069        public PsiElement copy() {
070            return new KtLightTypeParameter(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        @Override
084        public String getText() {
085            return "";
086        }
087    
088        @Nullable
089        @Override
090        public String getName() {
091            return name;
092        }
093    
094        @Override
095        public PsiTypeParameterListOwner getOwner() {
096            return owner;
097        }
098    
099        @Override
100        public int getIndex() {
101            return index;
102        }
103    
104        @NotNull
105        @Override
106        public PsiAnnotation[] getAnnotations() {
107            return getDelegate().getAnnotations();
108        }
109    
110        @NotNull
111        @Override
112        public PsiAnnotation[] getApplicableAnnotations() {
113            return getDelegate().getApplicableAnnotations();
114        }
115    
116        @Override
117        public PsiAnnotation findAnnotation(@NotNull String qualifiedName) {
118            return getDelegate().findAnnotation(qualifiedName);
119        }
120    
121        @NotNull
122        @Override
123        public PsiAnnotation addAnnotation(@NotNull String qualifiedName) {
124            return getDelegate().addAnnotation(qualifiedName);
125        }
126    
127        @Override
128        public String toString() {
129            return "KotlinLightTypeParameter:" + getName();
130        }
131    
132        @NotNull
133        @Override
134        public PsiElement getNavigationElement() {
135            return getOrigin();
136        }
137    
138        @NotNull
139        @Override
140        public Language getLanguage() {
141            return KotlinLanguage.INSTANCE;
142        }
143    
144        @NotNull
145        @Override
146        public SearchScope getUseScope() {
147            return getOrigin().getUseScope();
148        }
149    
150        @Override
151        public boolean equals(Object obj) {
152            if (obj == this) return true;
153            return obj instanceof KtLightTypeParameter && getOrigin().equals(((KtLightTypeParameter) obj).getOrigin());
154        }
155    }