Refactor light elements

KtLightElement#delegate -> clsDelegate, KtLightElement#origin -> kotlinOrigin and make them properties
KtLightClassForDecompiledDeclaration stores KtClsFile
KtLightField stores LightMemberOrigin
This commit is contained in:
Pavel V. Talanov
2016-03-29 18:48:30 +03:00
parent 02543295f9
commit f40a04c5a2
46 changed files with 247 additions and 254 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -46,9 +46,15 @@ public class FakeLightClassForFileOfPackage extends AbstractLightClass implement
this.file = file;
}
@NotNull
@Override
public PsiClass getClsDelegate() {
return delegate;
}
@Nullable
@Override
public KtClassOrObject getOrigin() {
public KtClassOrObject getKotlinOrigin() {
return null;
}
@@ -24,20 +24,17 @@ import com.intellij.util.IncorrectOperationException
import org.jetbrains.kotlin.psi.KtAnnotationEntry
class KtLightAnnotation(
private val delegate: PsiAnnotation,
private val originalElement: KtAnnotationEntry,
override val clsDelegate: PsiAnnotation,
override val kotlinOrigin: KtAnnotationEntry,
private val owner: PsiAnnotationOwner
) : PsiAnnotation by delegate, KtLightElement<KtAnnotationEntry, PsiAnnotation> {
override fun getDelegate() = delegate
override fun getOrigin() = originalElement
) : PsiAnnotation by clsDelegate, KtLightElement<KtAnnotationEntry, PsiAnnotation> {
override fun getName() = null
override fun setName(newName: String) = throw IncorrectOperationException()
override fun getOwner() = owner
override fun getText() = originalElement.text ?: ""
override fun getTextRange() = originalElement.textRange ?: TextRange.EMPTY_RANGE
override fun getText() = kotlinOrigin.text ?: ""
override fun getTextRange() = kotlinOrigin.textRange ?: TextRange.EMPTY_RANGE
override fun getParent() = owner as? PsiElement
@@ -46,8 +43,8 @@ class KtLightAnnotation(
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
return originalElement == (other as KtLightAnnotation).originalElement
return kotlinOrigin == (other as KtLightAnnotation).kotlinOrigin
}
override fun hashCode() = originalElement.hashCode()
override fun hashCode() = kotlinOrigin.hashCode()
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -56,8 +56,6 @@ open class KtLightClassForExplicitDeclaration(
protected val classFqName: FqName, // FqName of (possibly inner) class
protected val classOrObject: KtClassOrObject)
: KtWrappingLightClass(classOrObject.manager), KtJavaMirrorMarker, StubBasedPsiElement<KotlinClassOrObjectStub<out KtClassOrObject>> {
private var delegate: PsiClass? = null
private val lightIdentifier = KtLightIdentifier(this, classOrObject)
private fun getLocalClassParent(): PsiElement? {
@@ -139,7 +137,7 @@ open class KtLightClassForExplicitDeclaration(
containingClass
}
override fun getOrigin(): KtClassOrObject = classOrObject
override val kotlinOrigin: KtClassOrObject = classOrObject
override fun getFqName(): FqName = classFqName
@@ -147,28 +145,21 @@ open class KtLightClassForExplicitDeclaration(
return KtLightClassForExplicitDeclaration(classFqName, classOrObject.copy() as KtClassOrObject)
}
override fun getDelegate(): PsiClass {
if (delegate == null) {
val javaFileStub = getJavaFileStub()
override val clsDelegate: PsiClass by lazy(LazyThreadSafetyMode.PUBLICATION) {
val javaFileStub = getJavaFileStub()
val psiClass = LightClassUtil.findClass(classFqName, javaFileStub)
if (psiClass == null) {
val outermostClassOrObject = getOutermostClassOrObject(classOrObject)
val ktFileText: String? = try {
outermostClassOrObject.containingFile.text
}
catch (e: Exception) {
"Can't get text for outermost class"
}
val stubFileText = DebugUtil.stubTreeToString(javaFileStub)
throw IllegalStateException("Class was not found $classFqName\nin $ktFileText\nstub: \n$stubFileText")
LightClassUtil.findClass(classFqName, javaFileStub) ?: run {
val outermostClassOrObject = getOutermostClassOrObject(classOrObject)
val ktFileText: String? = try {
outermostClassOrObject.containingFile.text
}
catch (e: Exception) {
"Can't get text for outermost class"
}
delegate = psiClass
}
return delegate!!
val stubFileText = DebugUtil.stubTreeToString(javaFileStub)
throw IllegalStateException("Class was not found $classFqName\nin $ktFileText\nstub: \n$stubFileText")
}
}
private fun getJavaFileStub(): PsiJavaFileStub = getLightClassData().javaFileStub
@@ -380,7 +371,7 @@ open class KtLightClassForExplicitDeclaration(
@Throws(IncorrectOperationException::class)
override fun setName(@NonNls name: String): PsiElement {
getOrigin().setName(name)
kotlinOrigin.setName(name)
return this
}
@@ -396,7 +387,7 @@ open class KtLightClassForExplicitDeclaration(
return result
}
override fun getUseScope(): SearchScope = getOrigin().useScope
override fun getUseScope(): SearchScope = kotlinOrigin.useScope
override fun getElementType(): IStubElementType<out StubElement<*>, *>? = classOrObject.elementType
override fun getStub(): KotlinClassOrObjectStub<out KtClassOrObject>? = classOrObject.stub
@@ -92,7 +92,7 @@ class KtLightClassForFacade private constructor(
stub = { lightClassDataCache.value.javaFileStub }
)
override fun getOrigin(): KtClassOrObject? = null
override val kotlinOrigin: KtClassOrObject? get() = null
override fun getFqName(): FqName = facadeClassFqName
@@ -146,11 +146,12 @@ class KtLightClassForFacade private constructor(
override fun copy() = KtLightClassForFacade(getManager(), facadeClassFqName, lightClassDataCache, files)
override fun getDelegate(): PsiClass {
val psiClass = LightClassUtil.findClass(facadeClassFqName, lightClassDataCache.value.javaFileStub)
?: throw IllegalStateException("Facade class $facadeClassFqName not found")
return psiClass
}
override val clsDelegate: PsiClass
get() {
val psiClass = LightClassUtil.findClass(facadeClassFqName, lightClassDataCache.value.javaFileStub)
?: throw IllegalStateException("Facade class $facadeClassFqName not found")
return psiClass
}
override fun getNavigationElement() = files.iterator().next()
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -22,9 +22,9 @@ import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtElement
interface KtLightElement<T : KtElement, D : PsiElement> : PsiNamedElement {
fun getOrigin(): T?
val kotlinOrigin: T?
fun getDelegate(): D
val clsDelegate: D
}
interface KtLightDeclaration<T: KtDeclaration, D: PsiElement>: KtLightElement<T, D>
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -31,109 +31,105 @@ interface KtLightField : PsiField, KtLightDeclaration<KtDeclaration, PsiField>
// Copied from com.intellij.psi.impl.light.LightField
sealed class KtLightFieldImpl(
private val origin: KtDeclaration?,
private val delegate: PsiField,
private val lightMemberOrigin: LightMemberOrigin?,
override val clsDelegate: PsiField,
private val containingClass: KtLightClass
) : LightElement(delegate.manager, KotlinLanguage.INSTANCE), KtLightField {
private val lightIdentifier = KtLightIdentifier(this, origin as? KtNamedDeclaration)
) : LightElement(clsDelegate.manager, KotlinLanguage.INSTANCE), KtLightField {
private val lightIdentifier by lazy { KtLightIdentifier(this, kotlinOrigin as? KtNamedDeclaration) }
@Throws(IncorrectOperationException::class)
override fun setInitializer(initializer: PsiExpression?) = throw IncorrectOperationException("Not supported")
override fun getUseScope() = origin?.useScope ?: super.getUseScope()
override fun getUseScope() = kotlinOrigin?.useScope ?: super.getUseScope()
override fun getName() = delegate.name
override fun getName() = clsDelegate.name
override fun getNameIdentifier() = lightIdentifier
override fun getDocComment() = delegate.docComment
override fun getDocComment() = clsDelegate.docComment
override fun isDeprecated() = delegate.isDeprecated
override fun isDeprecated() = clsDelegate.isDeprecated
override fun getContainingClass() = containingClass
override fun getType() = delegate.type
override fun getType() = clsDelegate.type
override fun getTypeElement() = delegate.typeElement
override fun getTypeElement() = clsDelegate.typeElement
override fun getInitializer() = delegate.initializer
override fun getInitializer() = clsDelegate.initializer
override fun hasInitializer() = delegate.hasInitializer()
override fun hasInitializer() = clsDelegate.hasInitializer()
@Throws(IncorrectOperationException::class)
override fun normalizeDeclaration() = throw IncorrectOperationException("Not supported")
override fun computeConstantValue() = delegate.computeConstantValue()
override fun computeConstantValue() = clsDelegate.computeConstantValue()
@Throws(IncorrectOperationException::class)
override fun setName(@NonNls name: String) = throw IncorrectOperationException("Not supported")
private val _modifierList by lazy { delegate.modifierList?.let { KtLightModifierList(it, this) } }
private val _modifierList by lazy { clsDelegate.modifierList?.let { KtLightModifierList(it, this) } }
override fun getModifierList() = _modifierList
override fun hasModifierProperty(@NonNls name: String) = delegate.hasModifierProperty(name)
override fun hasModifierProperty(@NonNls name: String) = clsDelegate.hasModifierProperty(name)
override fun getText() = origin?.text ?: ""
override fun getText() = kotlinOrigin?.text ?: ""
override fun getTextRange() = origin?.textRange ?: TextRange.EMPTY_RANGE
override fun getTextRange() = kotlinOrigin?.textRange ?: TextRange.EMPTY_RANGE
override fun isValid() = containingClass.isValid
override fun toString(): String = "${this.javaClass.simpleName}:$name"
override fun getOrigin() = origin
override val kotlinOrigin: KtDeclaration? get() = lightMemberOrigin?.originalElement
override fun getDelegate() = delegate
override fun getNavigationElement() = kotlinOrigin ?: super.getNavigationElement()
override fun getNavigationElement() = origin ?: super.getNavigationElement()
override fun isEquivalentTo(another: PsiElement?): Boolean {
if (another is KtLightField && origin == another.getOrigin() && delegate == another.getDelegate()) {
if (another is KtLightField && kotlinOrigin == another.kotlinOrigin && clsDelegate == another.clsDelegate) {
return true
}
return super.isEquivalentTo(another)
}
override fun isWritable() = getOrigin()?.isWritable ?: false
override fun isWritable() = kotlinOrigin?.isWritable ?: false
override fun copy() = Factory.create(origin?.copy() as? KtDeclaration, delegate, containingClass)
override fun copy() = Factory.create(lightMemberOrigin?.copy(), clsDelegate, containingClass)
class KtLightEnumConstant(
origin: KtEnumEntry?,
enumConstant: PsiEnumConstant,
origin: LightMemberOrigin?,
override val clsDelegate: PsiEnumConstant,
containingClass: KtLightClass,
private val initializingClass: PsiEnumConstantInitializer?
) : KtLightFieldImpl(origin, enumConstant, containingClass), PsiEnumConstant {
override fun getDelegate() = super.getDelegate() as PsiEnumConstant
) : KtLightFieldImpl(origin, clsDelegate, containingClass), PsiEnumConstant {
// NOTE: we don't use "delegation by" because the compiler would generate method calls to ALL of PsiEnumConstant members,
// but we need only members whose implementations are not present in KotlinLightField
override fun getArgumentList() = getDelegate().argumentList
override fun getArgumentList() = clsDelegate.argumentList
override fun getInitializingClass(): PsiEnumConstantInitializer? = initializingClass
override fun getOrCreateInitializingClass(): PsiEnumConstantInitializer =
initializingClass ?: throw UnsupportedOperationException("Can't create enum constant body: ${getDelegate().name}")
initializingClass ?: throw UnsupportedOperationException("Can't create enum constant body: ${clsDelegate.name}")
override fun resolveConstructor() = getDelegate().resolveConstructor()
override fun resolveMethod() = getDelegate().resolveMethod()
override fun resolveMethodGenerics() = getDelegate().resolveMethodGenerics()
override fun resolveConstructor() = clsDelegate.resolveConstructor()
override fun resolveMethod() = clsDelegate.resolveMethod()
override fun resolveMethodGenerics() = clsDelegate.resolveMethodGenerics()
}
class KtLightFieldForDeclaration(origin: KtDeclaration?, delegate: PsiField, containingClass: KtLightClass)
: KtLightFieldImpl(origin, delegate, containingClass)
class KtLightFieldForDeclaration(origin: LightMemberOrigin?, delegate: PsiField, containingClass: KtLightClass) :
KtLightFieldImpl(origin, delegate, containingClass)
companion object Factory {
fun create(origin: KtDeclaration?, delegate: PsiField, containingClass: KtLightClass): KtLightField {
fun create(origin: LightMemberOrigin?, delegate: PsiField, containingClass: KtLightClass): KtLightField {
when (delegate) {
is PsiEnumConstant -> {
val kotlinEnumEntry = origin as? KtEnumEntry
val kotlinEnumEntry = origin?.originalElement as? KtEnumEntry
val initializingClass = if (kotlinEnumEntry != null && kotlinEnumEntry.declarations.isNotEmpty()) {
val enumConstantFqName = FqName(containingClass.getFqName().asString() + "." + kotlinEnumEntry.name)
KtLightClassForEnumEntry(enumConstantFqName, kotlinEnumEntry, delegate)
}
else null
return KtLightEnumConstant(kotlinEnumEntry, delegate, containingClass, initializingClass)
return KtLightEnumConstant(origin, delegate, containingClass, initializingClass)
}
else -> return KtLightFieldForDeclaration(origin, delegate, containingClass)
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -32,22 +32,22 @@ interface KtLightMethod : PsiMethod, KtLightDeclaration<KtDeclaration, PsiMethod
}
sealed class KtLightMethodImpl(
private val delegate: PsiMethod,
override val clsDelegate: PsiMethod,
private val lightMethodOrigin: LightMemberOrigin?,
containingClass: KtLightClass
) : LightMethod(delegate.manager, delegate, containingClass), KtLightMethod {
private val origin = lightMethodOrigin?.originalElement as? KtDeclaration
) : LightMethod(clsDelegate.manager, clsDelegate, containingClass), KtLightMethod {
override val kotlinOrigin: KtDeclaration? = lightMethodOrigin?.originalElement as? KtDeclaration
private val lightIdentifier = KtLightIdentifier(this, origin as? KtNamedDeclaration)
private val lightIdentifier by lazy { KtLightIdentifier(this, kotlinOrigin as? KtNamedDeclaration) }
override fun getContainingClass(): KtLightClass = super.getContainingClass() as KtLightClass
private val paramsList: CachedValue<PsiParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(delegate.project)
val cacheManager = CachedValuesManager.getManager(clsDelegate.project)
cacheManager.createCachedValue<PsiParameterList>({
val parameterBuilder = LightParameterListBuilder(manager, KotlinLanguage.INSTANCE, this)
for ((index, parameter) in delegate.parameterList.parameters.withIndex()) {
for ((index, parameter) in clsDelegate.parameterList.parameters.withIndex()) {
parameterBuilder.addParameter(KtLightParameter(parameter, index, this))
}
@@ -56,13 +56,14 @@ sealed class KtLightMethodImpl(
}
private val typeParamsList: CachedValue<PsiTypeParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(delegate.project)
val cacheManager = CachedValuesManager.getManager(clsDelegate.project)
cacheManager.createCachedValue<PsiTypeParameterList>({
val origin = kotlinOrigin
val list = if (origin is KtClassOrObject) {
KotlinLightTypeParameterListBuilder(manager)
}
else if (origin == null) {
delegate.typeParameterList
clsDelegate.typeParameterList
}
else {
LightClassUtil.buildLightTypeParameterList(this@KtLightMethodImpl, origin)
@@ -71,13 +72,11 @@ sealed class KtLightMethodImpl(
}, false)
}
override fun getNavigationElement(): PsiElement = origin ?: super.getNavigationElement()
override fun getOriginalElement(): PsiElement = origin ?: super.getOriginalElement()
override fun getDelegate() = delegate
override fun getOrigin() = origin
override fun getNavigationElement(): PsiElement = kotlinOrigin?.navigationElement ?: super.getNavigationElement()
override fun getOriginalElement(): PsiElement = kotlinOrigin ?: super.getOriginalElement()
override fun getParent(): PsiElement? = containingClass
override fun getText() = origin?.text ?: ""
override fun getTextRange() = origin?.textRange ?: TextRange.EMPTY_RANGE
override fun getText() = kotlinOrigin?.text ?: ""
override fun getTextRange() = kotlinOrigin?.textRange ?: TextRange.EMPTY_RANGE
override val isDelegated: Boolean
get() = lightMethodOrigin?.originKind == JvmDeclarationOriginKind.DELEGATION
@@ -93,13 +92,13 @@ sealed class KtLightMethodImpl(
}
override fun setName(name: String): PsiElement? {
val toRename = origin as? PsiNamedElement ?: throwCanNotModify()
val toRename = kotlinOrigin as? PsiNamedElement ?: throwCanNotModify()
toRename.setName(name)
return this
}
override fun delete() {
origin?.let {
kotlinOrigin?.let {
if (it.isValid) {
it.delete()
}
@@ -110,7 +109,7 @@ sealed class KtLightMethodImpl(
throw IncorrectOperationException(JavaCoreBundle.message("psi.error.attempt.to.edit.class.file"))
}
private val _modifierList by lazy { KtLightModifierList(delegate.modifierList, this) }
private val _modifierList by lazy { KtLightModifierList(clsDelegate.modifierList, this) }
override fun getModifierList() = _modifierList
@@ -125,16 +124,16 @@ sealed class KtLightMethodImpl(
override fun getSignature(substitutor: PsiSubstitutor): MethodSignature {
if (substitutor == PsiSubstitutor.EMPTY) {
return delegate.getSignature(substitutor)
return clsDelegate.getSignature(substitutor)
}
return MethodSignatureBackedByPsiMethod.create(this, substitutor)
}
override fun copy(): PsiElement {
return Factory.create(delegate, lightMethodOrigin?.copy(), containingClass)
return Factory.create(clsDelegate, lightMethodOrigin?.copy(), containingClass)
}
override fun getUseScope() = origin?.useScope ?: super.getUseScope()
override fun getUseScope() = kotlinOrigin?.useScope ?: super.getUseScope()
override fun getLanguage() = KotlinLanguage.INSTANCE
@@ -143,7 +142,7 @@ sealed class KtLightMethodImpl(
}
override fun isEquivalentTo(another: PsiElement?): Boolean {
if (another is KtLightMethod && origin == another.getOrigin() && delegate == another.getDelegate()) {
if (another is KtLightMethod && kotlinOrigin == another.kotlinOrigin && clsDelegate == another.clsDelegate) {
return true
}
@@ -153,11 +152,11 @@ sealed class KtLightMethodImpl(
override fun equals(other: Any?): Boolean =
other is KtLightMethod &&
name == other.name &&
origin == other.getOrigin() &&
kotlinOrigin == other.kotlinOrigin &&
containingClass == other.containingClass &&
delegate == other.getDelegate()
clsDelegate == other.clsDelegate
override fun hashCode(): Int = ((name.hashCode() * 31 + (origin?.hashCode() ?: 0)) * 31 + containingClass.hashCode()) * 31 + delegate.hashCode()
override fun hashCode(): Int = ((name.hashCode() * 31 + (kotlinOrigin?.hashCode() ?: 0)) * 31 + containingClass.hashCode()) * 31 + clsDelegate.hashCode()
override fun toString(): String = "${this.javaClass.simpleName}:$name"
@@ -166,12 +165,11 @@ sealed class KtLightMethodImpl(
) : KtLightMethodImpl(delegate, origin, containingClass)
private class KtLightAnnotationMethod(
delegate: PsiAnnotationMethod,
override val clsDelegate: PsiAnnotationMethod,
origin: LightMemberOrigin?,
containingClass: KtLightClass
) : KtLightMethodImpl(delegate, origin, containingClass), PsiAnnotationMethod {
override fun getDefaultValue() = getDelegate().defaultValue
override fun getDelegate() = super.getDelegate() as PsiAnnotationMethod
) : KtLightMethodImpl(clsDelegate, origin, containingClass), PsiAnnotationMethod {
override fun getDefaultValue() = clsDelegate.defaultValue
}
companion object Factory {
@@ -187,13 +185,13 @@ sealed class KtLightMethodImpl(
}
fun KtLightMethod.isTraitFakeOverride(): Boolean {
val methodOrigin = this.getOrigin()
val methodOrigin = this.kotlinOrigin
if (!(methodOrigin is KtNamedFunction || methodOrigin is KtPropertyAccessor || methodOrigin is KtProperty)) {
return false
}
val parentOfMethodOrigin = PsiTreeUtil.getParentOfType(methodOrigin, KtClassOrObject::class.java)
val thisClassDeclaration = (this.containingClass as KtLightClass).getOrigin()
val thisClassDeclaration = (this.containingClass as KtLightClass).kotlinOrigin
// Method was generated from declaration in some other trait
return (parentOfMethodOrigin != null && thisClassDeclaration !== parentOfMethodOrigin && KtPsiUtil.isTrait(parentOfMethodOrigin))
@@ -69,7 +69,7 @@ internal fun computeAnnotations(lightElement: PsiModifierList,
val cacheManager = CachedValuesManager.getManager(lightElement.project)
return cacheManager.createCachedValue<Array<out PsiAnnotation>>(
{
val declaration = (lightElement.parent as? KtLightElement<*, *>)?.getOrigin() as? KtDeclaration
val declaration = (lightElement.parent as? KtLightElement<*, *>)?.kotlinOrigin as? KtDeclaration
val descriptor = declaration?.let { LightClassGenerationSupport.getInstance(lightElement.project).resolveToDescriptor(it) }
val ktAnnotations = descriptor?.annotations?.getAllAnnotations() ?: emptyList()
var nextIndex = 0
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -41,7 +41,7 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
private final PsiParameter delegate;
private final int index;
private final KtLightMethod method;
private final KtLightIdentifier lightIdentifier;
private KtLightIdentifier lightIdentifier = null;
public KtLightParameter(final PsiParameter delegate, int index, KtLightMethod method) {
super(getName(delegate, index), delegate.getType(), method, KotlinLanguage.INSTANCE);
@@ -56,8 +56,6 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
return delegate.getModifierList();
}
};
lightIdentifier = new KtLightIdentifier(this, getOrigin());
}
@NotNull
@@ -68,14 +66,14 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
@NotNull
@Override
public PsiParameter getDelegate() {
public PsiParameter getClsDelegate() {
return delegate;
}
@Nullable
@Override
public KtParameter getOrigin() {
KtDeclaration declaration = method.getOrigin();
public KtParameter getKotlinOrigin() {
KtDeclaration declaration = method.getKotlinOrigin();
if (declaration == null) return null;
int jetIndex = KtPsiUtilKt.isExtensionDeclaration(declaration) ? index - 1 : index;
@@ -103,13 +101,13 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
@NotNull
@Override
public PsiElement getNavigationElement() {
KtParameter origin = getOrigin();
KtParameter origin = getKotlinOrigin();
return origin != null ? origin : super.getNavigationElement();
}
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
KtParameter origin = getOrigin();
KtParameter origin = getKotlinOrigin();
if (origin != null) {
origin.setName(name);
}
@@ -118,7 +116,7 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
@Override
public PsiFile getContainingFile() {
KtDeclaration declaration = method.getOrigin();
KtDeclaration declaration = method.getKotlinOrigin();
return declaration != null ? declaration.getContainingFile() : super.getContainingFile();
}
@@ -131,7 +129,7 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
@NotNull
@Override
public SearchScope getUseScope() {
KtParameter origin = getOrigin();
KtParameter origin = getKotlinOrigin();
return origin != null ? origin.getUseScope() : GlobalSearchScope.EMPTY_SCOPE;
}
@@ -146,6 +144,9 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
@Override
public PsiIdentifier getNameIdentifier() {
if (lightIdentifier == null) {
lightIdentifier = new KtLightIdentifier(this, getKotlinOrigin());
}
return lightIdentifier;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -44,13 +44,19 @@ public class KtLightTypeParameter
@NotNull
@Override
public PsiTypeParameter getDelegate() {
public PsiTypeParameter getClsDelegate() {
return getOwnerDelegate().getTypeParameters()[index];
}
@NotNull
@Override
public KtTypeParameter getOrigin() {
public PsiClass getDelegate() {
return getClsDelegate();
}
@NotNull
@Override
public KtTypeParameter getKotlinOrigin() {
KtTypeParameterListOwner jetOwner = (KtTypeParameterListOwner) LightClassUtilsKt.getUnwrapped(owner);
assert (jetOwner != null) : "Invalid type parameter owner: " + owner;
@@ -59,8 +65,8 @@ public class KtLightTypeParameter
@NotNull
private PsiTypeParameterListOwner getOwnerDelegate() {
if (owner instanceof KtLightClass) return ((KtLightClass) owner).getDelegate();
if (owner instanceof KtLightMethod) return ((KtLightMethod) owner).getDelegate();
if (owner instanceof KtLightClass) return ((KtLightClass) owner).getClsDelegate();
if (owner instanceof KtLightMethod) return ((KtLightMethod) owner).getClsDelegate();
return owner;
}
@@ -104,24 +110,24 @@ public class KtLightTypeParameter
@NotNull
@Override
public PsiAnnotation[] getAnnotations() {
return getDelegate().getAnnotations();
return getClsDelegate().getAnnotations();
}
@NotNull
@Override
public PsiAnnotation[] getApplicableAnnotations() {
return getDelegate().getApplicableAnnotations();
return getClsDelegate().getApplicableAnnotations();
}
@Override
public PsiAnnotation findAnnotation(@NotNull String qualifiedName) {
return getDelegate().findAnnotation(qualifiedName);
return getClsDelegate().findAnnotation(qualifiedName);
}
@NotNull
@Override
public PsiAnnotation addAnnotation(@NotNull String qualifiedName) {
return getDelegate().addAnnotation(qualifiedName);
return getClsDelegate().addAnnotation(qualifiedName);
}
@Override
@@ -132,7 +138,7 @@ public class KtLightTypeParameter
@NotNull
@Override
public PsiElement getNavigationElement() {
return getOrigin();
return getKotlinOrigin();
}
@NotNull
@@ -144,12 +150,12 @@ public class KtLightTypeParameter
@NotNull
@Override
public SearchScope getUseScope() {
return getOrigin().getUseScope();
return getKotlinOrigin().getUseScope();
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
return obj instanceof KtLightTypeParameter && getOrigin().equals(((KtLightTypeParameter) obj).getOrigin());
return obj instanceof KtLightTypeParameter && getKotlinOrigin().equals(((KtLightTypeParameter) obj).getKotlinOrigin());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -31,7 +31,6 @@ import com.intellij.util.containers.ContainerUtil;
import kotlin.collections.ArraysKt;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.psi.KtClassOrObject;
import org.jetbrains.kotlin.psi.KtDeclaration;
@@ -47,13 +46,11 @@ public abstract class KtWrappingLightClass extends AbstractLightClass implements
super(manager, KotlinLanguage.INSTANCE);
}
@Nullable
@Override
public abstract KtClassOrObject getOrigin();
@NotNull
@Override
public abstract PsiClass getDelegate();
public PsiClass getDelegate() {
return getClsDelegate();
}
@Override
@NotNull
@@ -123,9 +120,7 @@ public abstract class KtWrappingLightClass extends AbstractLightClass implements
@Override
public PsiField fun(PsiField field) {
LightMemberOrigin origin = ClsWrapperStubPsiFactory.getMemberOrigin(field);
return KtLightFieldImpl.Factory.create(origin != null ? origin.getOriginalElement() : null,
field,
KtWrappingLightClass.this);
return KtLightFieldImpl.Factory.create(origin, field, KtWrappingLightClass.this);
}
});
}
@@ -161,7 +156,7 @@ public abstract class KtWrappingLightClass extends AbstractLightClass implements
@Override
public String getText() {
KtClassOrObject origin = getOrigin();
KtClassOrObject origin = getKotlinOrigin();
return origin == null ? "" : origin.getText();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -62,7 +62,7 @@ object LightClassUtil {
val outerPsiClass = getWrappingClass(companionObject)
if (outerPsiClass != null) {
for (fieldOfParent in outerPsiClass.fields) {
if ((fieldOfParent is KtLightElement<*, *>) && fieldOfParent.getOrigin() === companionObject) {
if ((fieldOfParent is KtLightElement<*, *>) && fieldOfParent.kotlinOrigin === companionObject) {
return fieldOfParent
}
}
@@ -84,7 +84,7 @@ object LightClassUtil {
var psiClass: PsiClass = getWrappingClass(declaration) ?: return null
if (psiClass is KtLightClass) {
val origin = psiClass.getOrigin()
val origin = psiClass.kotlinOrigin
if (origin is KtObjectDeclaration && origin.isCompanion()) {
val containingClass = PsiTreeUtil.getParentOfType(origin, KtClass::class.java)
if (containingClass != null) {
@@ -97,7 +97,7 @@ object LightClassUtil {
}
for (field in psiClass.fields) {
if (field is KtLightField && field.getOrigin() === declaration) {
if (field is KtLightField && field.kotlinOrigin === declaration) {
return field
}
}
@@ -126,7 +126,7 @@ object LightClassUtil {
val methods = SmartList<PsiMethod>()
for (method in psiClass.methods.asList()) {
try {
if (method is KtLightMethod && method.getOrigin() === declaration) {
if (method is KtLightMethod && method.kotlinOrigin === declaration) {
methods.add(method)
if (!collectAll) {
return methods
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 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.
@@ -105,7 +105,7 @@ fun KtTypeParameter.toPsiTypeParameters(): List<PsiTypeParameter> {
// Returns original declaration if given PsiElement is a Kotlin light element, and element itself otherwise
val PsiElement.unwrapped: PsiElement?
get() = if (this is KtLightElement<*, *>) getOrigin() else this
get() = if (this is KtLightElement<*, *>) kotlinOrigin else this
val PsiElement.namedUnwrappedElement: PsiNamedElement?
get() = unwrapped?.getNonStrictParentOfType<PsiNamedElement>()
@@ -131,5 +131,5 @@ fun FqName.defaultImplsChild() = child(DEFAULT_IMPLS_CLASS_NAME)
fun KtAnnotationEntry.toLightAnnotation(): PsiAnnotation? {
val ktDeclaration = getStrictParentOfType<KtModifierList>()?.parent as? KtDeclaration ?: return null
val lightElement = ktDeclaration.toLightElements().firstOrNull() as? PsiModifierListOwner ?: return null
return lightElement.modifierList?.annotations?.firstOrNull { it is KtLightAnnotation && it.getOrigin() == this }
}
return lightElement.modifierList?.annotations?.firstOrNull { it is KtLightAnnotation && it.kotlinOrigin == this }
}