Reformat and cleanup in asJava/elements

This commit is contained in:
Nikolay Krasko
2019-06-28 14:53:07 +03:00
parent 353ca9752f
commit 9fd46c105d
13 changed files with 98 additions and 83 deletions
@@ -29,10 +29,10 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtFile
open class FakeFileForLightClass(
val ktFile: KtFile,
private val lightClass: () -> KtLightClass,
private val stub: () -> PsiClassHolderFileStub<*>,
private val packageFqName: FqName = ktFile.packageFqName
val ktFile: KtFile,
private val lightClass: () -> KtLightClass,
private val stub: () -> PsiClassHolderFileStub<*>,
private val packageFqName: FqName = ktFile.packageFqName
) : ClsFileImpl(ktFile.viewProvider) {
override fun getVirtualFile(): VirtualFile =
@@ -78,8 +78,7 @@ open class FakeFileForLightClass(
override fun setPackageName(packageName: String) {
if (lightClass() is KtLightClassForFacade) {
ktFile.packageDirective?.fqName = FqName(packageName)
}
else {
} else {
super.setPackageName(packageName)
}
}
@@ -26,7 +26,12 @@ import org.jetbrains.kotlin.idea.KotlinLanguage
class KotlinLightTypeParameterListBuilder(private val owner: PsiTypeParameterListOwner) :
LightTypeParameterListBuilder(owner.manager, KotlinLanguage.INSTANCE) {
override fun processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement?, place: PsiElement): Boolean {
override fun processDeclarations(
processor: PsiScopeProcessor,
state: ResolveState,
lastParent: PsiElement?,
place: PsiElement
): Boolean {
return typeParameters.all { processor.execute(it, state) }
}
@@ -23,7 +23,7 @@ import com.intellij.psi.impl.light.LightElement
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.KtElement
abstract class KtLightElementBase(private val parent: PsiElement): LightElement(parent.manager, KotlinLanguage.INSTANCE) {
abstract class KtLightElementBase(private val parent: PsiElement) : LightElement(parent.manager, KotlinLanguage.INSTANCE) {
override fun toString() = "${this.javaClass.simpleName} of $parent"
override fun getParent(): PsiElement = parent
@@ -27,13 +27,12 @@ import org.jetbrains.kotlin.asJava.classes.cannotModify
import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.psi.KtEnumEntry
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import java.lang.UnsupportedOperationException
sealed class KtLightFieldImpl<D : PsiField>(
override val lightMemberOrigin: LightMemberOrigin?,
computeRealDelegate: () -> D,
containingClass: KtLightClass,
dummyDelegate: PsiField?
override val lightMemberOrigin: LightMemberOrigin?,
computeRealDelegate: () -> D,
containingClass: KtLightClass,
dummyDelegate: PsiField?
) : KtLightMemberImpl<PsiField>(computeRealDelegate, lightMemberOrigin, containingClass, dummyDelegate), KtLightField {
override val clsDelegate: D
@@ -60,10 +59,10 @@ sealed class KtLightFieldImpl<D : PsiField>(
}
override fun equals(other: Any?): Boolean =
this === other ||
(other is KtLightFieldImpl<*> &&
this.name == other.name &&
this.containingClass == other.containingClass)
this === other ||
(other is KtLightFieldImpl<*> &&
this.name == other.name &&
this.containingClass == other.containingClass)
override fun hashCode() = 31 * containingClass.hashCode() + name.hashCode()
@@ -75,17 +74,16 @@ sealed class KtLightFieldImpl<D : PsiField>(
class KtLightEnumConstant(
origin: LightMemberOrigin?,
computeDelegate: () -> PsiEnumConstant,
containingClass: KtLightClass,
dummyDelegate: PsiField?
origin: LightMemberOrigin?,
computeDelegate: () -> PsiEnumConstant,
containingClass: KtLightClass,
dummyDelegate: PsiField?
) : KtLightFieldImpl<PsiEnumConstant>(origin, computeDelegate, containingClass, dummyDelegate), PsiEnumConstant {
private val initializingClass by lazyPub {
val kotlinEnumEntry = (lightMemberOrigin as? LightMemberOriginForDeclaration)?.originalElement as? KtEnumEntry
if (kotlinEnumEntry != null && kotlinEnumEntry.declarations.isNotEmpty()) {
KtLightClassForEnumEntry(kotlinEnumEntry, clsDelegate)
}
else null
} else null
}
// NOTE: we don't use "delegation by" because the compiler would generate method calls to ALL of PsiEnumConstant members,
@@ -102,7 +100,12 @@ sealed class KtLightFieldImpl<D : PsiField>(
override fun resolveMethodGenerics() = clsDelegate.resolveMethodGenerics()
}
class KtLightFieldForSourceDeclaration(origin: LightMemberOrigin?, computeDelegate: () -> PsiField, containingClass: KtLightClass, dummyDelegate: PsiField?) :
class KtLightFieldForSourceDeclaration(
origin: LightMemberOrigin?,
computeDelegate: () -> PsiField,
containingClass: KtLightClass,
dummyDelegate: PsiField?
) :
KtLightFieldImpl<PsiField>(origin, computeDelegate, containingClass, dummyDelegate),
KtLightFieldForSourceDeclarationSupport
@@ -113,10 +116,10 @@ sealed class KtLightFieldImpl<D : PsiField>(
}
fun lazy(
dummyDelegate: PsiField,
origin: LightMemberOriginForDeclaration?,
containingClass: KtLightClass,
computeRealDelegate: () -> PsiField
dummyDelegate: PsiField,
origin: LightMemberOriginForDeclaration?,
containingClass: KtLightClass,
computeRealDelegate: () -> PsiField
): KtLightField {
if (dummyDelegate is PsiEnumConstant) {
@Suppress("UNCHECKED_CAST")
@@ -35,8 +35,8 @@ open class KtLightIdentifier(
get() = when (ktDeclaration) {
is KtSecondaryConstructor -> ktDeclaration.getConstructorKeyword()
is KtPrimaryConstructor -> ktDeclaration.getConstructorKeyword()
?: ktDeclaration.valueParameterList
?: ktDeclaration.containingClassOrObject?.nameIdentifier
?: ktDeclaration.valueParameterList
?: ktDeclaration.containingClassOrObject?.nameIdentifier
else -> ktDeclaration?.nameIdentifier
}
@@ -33,10 +33,10 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.psi.psiUtil.hasBody
abstract class KtLightMemberImpl<out D : PsiMember>(
computeRealDelegate: () -> D,
override val lightMemberOrigin: LightMemberOrigin?,
private val containingClass: KtLightClass,
private val dummyDelegate: D?
computeRealDelegate: () -> D,
override val lightMemberOrigin: LightMemberOrigin?,
private val containingClass: KtLightClass,
private val dummyDelegate: D?
) : KtLightElementBase(containingClass), PsiMember, KtLightMember<D> {
override val clsDelegate by lazyPub(computeRealDelegate)
private val lightIdentifier by lazyPub { KtLightIdentifier(this, kotlinOrigin as? KtNamedDeclaration) }
@@ -73,8 +73,8 @@ abstract class KtLightMemberImpl<out D : PsiMember>(
if (lightMemberOrigin?.originalElement?.isEquivalentTo(another) == true) return true
val isEquivalentByOrigin =
another is KtLightMember<*> &&
lightMemberOrigin?.isEquivalentTo(another.lightMemberOrigin) == true
another is KtLightMember<*> &&
lightMemberOrigin?.isEquivalentTo(another.lightMemberOrigin) == true
return isEquivalentByOrigin || this == another
}
@@ -91,13 +91,14 @@ internal fun getMemberOrigin(member: PsiMember): LightMemberOriginForDeclaration
private val visibilityModifiers = arrayOf(PsiModifier.PRIVATE, PsiModifier.PACKAGE_LOCAL, PsiModifier.PROTECTED, PsiModifier.PUBLIC)
private class KtLightMemberModifierList(
owner: KtLightMember<*>, private val dummyDelegate: PsiModifierList?
owner: KtLightMember<*>, private val dummyDelegate: PsiModifierList?
) : KtLightModifierList<KtLightMember<*>>(owner) {
override fun hasModifierProperty(name: String) = when {
name == PsiModifier.ABSTRACT && isImplementationInInterface() -> false
// pretend this method behaves like a default method
name == PsiModifier.DEFAULT && isImplementationInInterface() -> true
name == PsiModifier.FINAL && ((owner.containingClass as? KtLightClassForSourceDeclaration)?.isPossiblyAffectedByAllOpen() ?: false) ->
name == PsiModifier.FINAL && ((owner.containingClass as? KtLightClassForSourceDeclaration)?.isPossiblyAffectedByAllOpen()
?: false) ->
clsDelegate.hasModifierProperty(name)
dummyDelegate != null -> {
when {
@@ -110,13 +111,13 @@ private class KtLightMemberModifierList(
}
override fun hasExplicitModifier(name: String) =
// kotlin methods can't be truly default atm, that way we can avoid being reported on by diagnostics, namely android lint
if (name == PsiModifier.DEFAULT) false else super.hasExplicitModifier(name)
// kotlin methods can't be truly default atm, that way we can avoid being reported on by diagnostics, namely android lint
if (name == PsiModifier.DEFAULT) false else super.hasExplicitModifier(name)
private fun isMethodOverride() = owner is KtLightMethod && owner.kotlinOrigin?.hasModifier(KtTokens.OVERRIDE_KEYWORD) ?: false
private fun isImplementationInInterface()
= owner.containingClass.isInterface && owner is KtLightMethod && owner.kotlinOrigin?.hasBody() ?: false
private fun isImplementationInInterface() =
owner.containingClass.isInterface && owner is KtLightMethod && owner.kotlinOrigin?.hasBody() ?: false
override fun copy() = KtLightMemberModifierList(owner, dummyDelegate)
}
@@ -40,10 +40,10 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade
open class KtLightMethodImpl protected constructor(
computeRealDelegate: () -> PsiMethod,
lightMemberOrigin: LightMemberOrigin?,
containingClass: KtLightClass,
private val dummyDelegate: PsiMethod? = null
computeRealDelegate: () -> PsiMethod,
lightMemberOrigin: LightMemberOrigin?,
containingClass: KtLightClass,
private val dummyDelegate: PsiMethod? = null
) : KtLightMemberImpl<PsiMethod>(computeRealDelegate, lightMemberOrigin, containingClass, dummyDelegate), KtLightMethod {
private val returnTypeElem by lazyPub {
val delegateTypeElement = clsDelegate.returnTypeElement as? ClsTypeElementImpl
@@ -84,8 +84,7 @@ open class KtLightMethodImpl protected constructor(
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitMethod(this)
}
else {
} else {
visitor.visitElement(this)
}
}
@@ -108,8 +107,7 @@ open class KtLightMethodImpl protected constructor(
val nameExpression = jvmNameAnnotation?.findAttributeValue("name")?.unwrapped as? KtStringTemplateExpression
if (nameExpression != null) {
nameExpression.replace(KtPsiFactory(this).createStringTemplate(name))
}
else {
} else {
val toRename = kotlinOrigin as? PsiNamedElement ?: cannotModify()
toRename.setName(newNameForOrigin)
}
@@ -136,7 +134,7 @@ open class KtLightMethodImpl protected constructor(
override fun getTypeParameterList() = typeParamsList
override fun getTypeParameters(): Array<PsiTypeParameter> =
typeParameterList?.typeParameters ?: PsiTypeParameter.EMPTY_ARRAY
typeParameterList?.typeParameters ?: PsiTypeParameter.EMPTY_ARRAY
override fun hasTypeParameters() = typeParameters.isNotEmpty()
@@ -151,7 +149,12 @@ open class KtLightMethodImpl protected constructor(
return Factory.create(clsDelegate, lightMemberOrigin?.copy(), containingClass)
}
override fun processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement?, place: PsiElement): Boolean {
override fun processDeclarations(
processor: PsiScopeProcessor,
state: ResolveState,
lastParent: PsiElement?,
place: PsiElement
): Boolean {
return typeParameters.all { processor.execute(it, state) }
}
@@ -163,14 +166,15 @@ open class KtLightMethodImpl protected constructor(
for source elements index is unique to each member
*/
override fun equals(other: Any?): Boolean =
this === other ||
(other is KtLightMethodImpl &&
this.name == other.name &&
this.containingClass == other.containingClass &&
this.lightMemberOrigin == other.lightMemberOrigin &&
this.memberIndex == other.memberIndex)
this === other ||
(other is KtLightMethodImpl &&
this.name == other.name &&
this.containingClass == other.containingClass &&
this.lightMemberOrigin == other.lightMemberOrigin &&
this.memberIndex == other.memberIndex)
override fun hashCode(): Int = ((getName().hashCode() * 31 + (lightMemberOrigin?.hashCode() ?: 0)) * 31 + containingClass.hashCode()) * 31 + (memberIndex?.hashCode() ?: 0)
override fun hashCode(): Int = ((name.hashCode() * 31 + (lightMemberOrigin?.hashCode()
?: 0)) * 31 + containingClass.hashCode()) * 31 + (memberIndex?.hashCode() ?: 0)
override fun getDefaultValue() = (clsDelegate as? PsiAnnotationMethod)?.defaultValue
@@ -182,8 +186,7 @@ open class KtLightMethodImpl protected constructor(
calculatingReturnType.set(true)
try {
return returnTypeElement?.type
}
finally {
} finally {
calculatingReturnType.set(false)
}
}
@@ -220,16 +223,16 @@ open class KtLightMethodImpl protected constructor(
}
fun create(
delegate: PsiMethod, origin: LightMemberOrigin?, containingClass: KtLightClass
delegate: PsiMethod, origin: LightMemberOrigin?, containingClass: KtLightClass
): KtLightMethodImpl {
return KtLightMethodImpl({ delegate}, origin, containingClass)
return KtLightMethodImpl({ delegate }, origin, containingClass)
}
fun lazy(
dummyDelegate: PsiMethod?,
containingClass: KtLightClass,
origin: LightMemberOriginForDeclaration?,
computeRealDelegate: () -> PsiMethod
dummyDelegate: PsiMethod?,
containingClass: KtLightClass,
origin: LightMemberOriginForDeclaration?,
computeRealDelegate: () -> PsiMethod
): KtLightMethodImpl {
return KtLightMethodImpl(computeRealDelegate, origin, containingClass, dummyDelegate)
}
@@ -249,7 +252,8 @@ open class KtLightMethodImpl protected constructor(
override fun getHierarchicalMethodSignature() = clsDelegate.hierarchicalMethodSignature
override fun findSuperMethodSignaturesIncludingStatic(checkAccess: Boolean) = clsDelegate.findSuperMethodSignaturesIncludingStatic(checkAccess)
override fun findSuperMethodSignaturesIncludingStatic(checkAccess: Boolean) =
clsDelegate.findSuperMethodSignaturesIncludingStatic(checkAccess)
override fun getBody() = null
@@ -28,7 +28,7 @@ class KtLightParameter(
private val index: Int,
method: KtLightMethod
) : LightParameter(dummyDelegate.name ?: "p$index", dummyDelegate.type, method, KotlinLanguage.INSTANCE),
KtLightDeclaration<KtParameter, PsiParameter> {
KtLightDeclaration<KtParameter, PsiParameter> {
private val lazyDelegate by lazyPub { clsDelegateProvider() ?: dummyDelegate }
@@ -106,8 +106,7 @@ class KtLightParameter(
if (another is KtLightParameter && kotlinOrigin != null) {
kotlinOrigin == another.kotlinOrigin && clsDelegate == another.clsDelegate
}
else {
} else {
null
}
})
@@ -25,9 +25,9 @@ import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFunction
class KtLightParameterList(
private val parent: KtLightMethod,
private val parametersCount: Int,
computeParameters: () -> List<PsiParameter>
private val parent: KtLightMethod,
private val parametersCount: Int,
computeParameters: () -> List<PsiParameter>
) : KtLightElementBase(parent), PsiParameterList {
override val kotlinOrigin: KtElement?
@@ -20,16 +20,13 @@ import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiJavaCodeReferenceElement
import com.intellij.psi.PsiReferenceList
import com.intellij.psi.PsiReferenceList.Role
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
import org.jetbrains.kotlin.asJava.classes.KtLightClass
import org.jetbrains.kotlin.asJava.classes.addSuperTypeEntry
import org.jetbrains.kotlin.asJava.classes.findEntry
import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtSuperTypeList
import org.jetbrains.kotlin.psi.KtSuperTypeListEntry
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
class KtLightPsiReferenceList(
override val clsDelegate: PsiReferenceList,
@@ -39,7 +39,8 @@ public class KtLightTypeParameter
public KtLightTypeParameter(
@NotNull PsiTypeParameterListOwner owner,
int index,
@NotNull String name) {
@NotNull String name
) {
super(owner.getManager(), KotlinLanguage.INSTANCE);
this.owner = owner;
this.index = index;
@@ -32,7 +32,13 @@ public class LightParameter extends LightVariableBuilder implements PsiParameter
this(name, type, declarationScope, language, type instanceof PsiEllipsisType);
}
public LightParameter(@NotNull String name, @NotNull PsiType type, @NotNull KtLightMethod declarationScope, Language language, boolean isVarArgs) {
public LightParameter(
@NotNull String name,
@NotNull PsiType type,
@NotNull KtLightMethod declarationScope,
Language language,
boolean isVarArgs
) {
super(declarationScope.getManager(), name, type, language);
myName = name;
myDeclarationScope = declarationScope;
@@ -52,7 +58,7 @@ public class LightParameter extends LightVariableBuilder implements PsiParameter
@Override
public void accept(@NotNull PsiElementVisitor visitor) {
if (visitor instanceof JavaElementVisitor) {
((JavaElementVisitor)visitor).visitParameter(this);
((JavaElementVisitor) visitor).visitParameter(this);
}
}
@@ -141,7 +141,7 @@ class KtLightAnnotationForSourceEntry(
override fun getParameterList(): PsiAnnotationParameterList = ktLightAnnotationParameterList
inner class KtLightAnnotationParameterList() : KtLightElementBase(this),
inner class KtLightAnnotationParameterList : KtLightElementBase(this),
PsiAnnotationParameterList {
override val kotlinOrigin: KtElement? get() = null