Using light PsiTypeParameters instead of building a stub:

This is needed when Java resolved references to Kotlin classes: in order to resolve a reference it needs to know type parameters
This commit is contained in:
Andrey Breslav
2013-01-11 16:01:06 +04:00
parent 1a03850951
commit fb1f863098
2 changed files with 140 additions and 0 deletions
@@ -22,11 +22,13 @@ import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NullableLazyValue;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub;
import com.intellij.psi.impl.light.AbstractLightClass;
import com.intellij.psi.impl.light.LightModifierList;
import com.intellij.psi.impl.light.LightTypeParameterListBuilder;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.util.IncorrectOperationException;
@@ -67,6 +69,25 @@ public class KotlinLightClassForExplicitDeclaration extends AbstractLightClass i
@Nullable
private PsiModifierList modifierList;
private NullableLazyValue<PsiTypeParameterList> typeParameterList = new NullableLazyValue<PsiTypeParameterList>() {
@Nullable
@Override
protected PsiTypeParameterList compute() {
LightTypeParameterListBuilder builder = new LightTypeParameterListBuilder(getManager(), getLanguage());
if (classOrObject instanceof JetTypeParameterListOwner) {
JetTypeParameterListOwner typeParameterListOwner = (JetTypeParameterListOwner) classOrObject;
List<JetTypeParameter> parameters = typeParameterListOwner.getTypeParameters();
for (int i = 0; i < parameters.size(); i++) {
JetTypeParameter jetTypeParameter = parameters.get(i);
String name = jetTypeParameter.getName();
String safeName = name == null ? "__no_name__" : name;
builder.addParameter(new KotlinLightTypeParameter(KotlinLightClassForExplicitDeclaration.this, i, safeName));
}
}
return builder;
}
};
private KotlinLightClassForExplicitDeclaration(
@NotNull PsiManager manager,
@NotNull FqName name,
@@ -180,6 +201,19 @@ public class KotlinLightClassForExplicitDeclaration extends AbstractLightClass i
return super.getContainingClass();
}
@Nullable
@Override
public PsiTypeParameterList getTypeParameterList() {
return typeParameterList.getValue();
}
@NotNull
@Override
public PsiTypeParameter[] getTypeParameters() {
PsiTypeParameterList typeParameterList = getTypeParameterList();
return typeParameterList == null ? PsiTypeParameter.EMPTY_ARRAY : typeParameterList.getTypeParameters();
}
@Nullable
@Override
public String getName() {
@@ -0,0 +1,106 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.asJava;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.AbstractLightClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.JetLanguage;
public class KotlinLightTypeParameter extends AbstractLightClass implements PsiTypeParameter {
private final KotlinLightClassForExplicitDeclaration lightClass;
private final int index;
private final String name;
protected KotlinLightTypeParameter(
@NotNull KotlinLightClassForExplicitDeclaration lightClass,
int index,
@NotNull String name) {
super(lightClass.getManager(), JetLanguage.INSTANCE);
this.lightClass = lightClass;
this.index = index;
this.name = name;
}
@NotNull
@Override
public PsiTypeParameter getDelegate() {
return lightClass.getDelegate().getTypeParameters()[index];
}
@NotNull
@Override
public PsiElement copy() {
return new KotlinLightTypeParameter(lightClass, index, name);
}
@Override
public void accept(@NotNull final PsiElementVisitor visitor) {
if (visitor instanceof JavaElementVisitor) {
((JavaElementVisitor) visitor).visitTypeParameter(this);
}
else {
super.accept(visitor);
}
}
@Nullable
@Override
public String getName() {
return name;
}
@Override
public PsiTypeParameterListOwner getOwner() {
return lightClass;
}
@Override
public int getIndex() {
return index;
}
@NotNull
@Override
public PsiAnnotation[] getAnnotations() {
return getDelegate().getAnnotations();
}
@NotNull
@Override
public PsiAnnotation[] getApplicableAnnotations() {
return getDelegate().getApplicableAnnotations();
}
@Override
public PsiAnnotation findAnnotation(@NotNull final String qualifiedName) {
return getDelegate().findAnnotation(qualifiedName);
}
@NotNull
@Override
public PsiAnnotation addAnnotation(@NotNull final String qualifiedName) {
return getDelegate().addAnnotation(qualifiedName);
}
@Override
public String toString() {
return "KotlinLightTypeParameter:" + getName();
}
}