001 /*
002 * Copyright 2010-2013 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.jet.asJava;
018
019 import com.intellij.psi.*;
020 import com.intellij.psi.impl.light.AbstractLightClass;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.jet.lang.psi.JetTypeParameter;
024 import org.jetbrains.jet.lang.psi.JetTypeParameterListOwner;
025 import org.jetbrains.jet.plugin.JetLanguage;
026
027 public class KotlinLightTypeParameter
028 extends AbstractLightClass implements PsiTypeParameter, KotlinLightElement<JetTypeParameter, PsiTypeParameter> {
029 private final PsiTypeParameterListOwner owner;
030 private final int index;
031 private final String name;
032
033 protected KotlinLightTypeParameter(
034 @NotNull PsiTypeParameterListOwner owner,
035 int index,
036 @NotNull String name) {
037 super(owner.getManager(), JetLanguage.INSTANCE);
038 this.owner = owner;
039 this.index = index;
040 this.name = name;
041 }
042
043 @NotNull
044 @Override
045 public PsiTypeParameter getDelegate() {
046 return getOwnerDelegate().getTypeParameters()[index];
047 }
048
049 @NotNull
050 @Override
051 public JetTypeParameter getOrigin() {
052 JetTypeParameterListOwner jetOwner = (JetTypeParameterListOwner) AsJavaPackage.getUnwrapped(owner);
053 assert (jetOwner != null) : "Invalid type parameter owner: " + owner;
054
055 return jetOwner.getTypeParameters().get(index);
056 }
057
058 @NotNull
059 private PsiTypeParameterListOwner getOwnerDelegate() {
060 if (owner instanceof KotlinLightClass) return ((KotlinLightClass) owner).getDelegate();
061 if (owner instanceof KotlinLightMethod) return ((KotlinLightMethod) owner).getDelegate();
062 return owner;
063 }
064
065 @NotNull
066 @Override
067 public PsiElement copy() {
068 return new KotlinLightTypeParameter(owner, index, name);
069 }
070
071 @Override
072 public void accept(@NotNull PsiElementVisitor visitor) {
073 if (visitor instanceof JavaElementVisitor) {
074 ((JavaElementVisitor) visitor).visitTypeParameter(this);
075 }
076 else {
077 super.accept(visitor);
078 }
079 }
080
081 @Nullable
082 @Override
083 public String getName() {
084 return name;
085 }
086
087 @Override
088 public PsiTypeParameterListOwner getOwner() {
089 return owner;
090 }
091
092 @Override
093 public int getIndex() {
094 return index;
095 }
096
097 @NotNull
098 @Override
099 public PsiAnnotation[] getAnnotations() {
100 return getDelegate().getAnnotations();
101 }
102
103 @NotNull
104 @Override
105 public PsiAnnotation[] getApplicableAnnotations() {
106 return getDelegate().getApplicableAnnotations();
107 }
108
109 @Override
110 public PsiAnnotation findAnnotation(@NotNull String qualifiedName) {
111 return getDelegate().findAnnotation(qualifiedName);
112 }
113
114 @NotNull
115 @Override
116 public PsiAnnotation addAnnotation(@NotNull String qualifiedName) {
117 return getDelegate().addAnnotation(qualifiedName);
118 }
119
120 @Override
121 public String toString() {
122 return "KotlinLightTypeParameter:" + getName();
123 }
124
125 @NotNull
126 @Override
127 public PsiElement getNavigationElement() {
128 return getOrigin();
129 }
130 }