From 85d949a04a1ec116ff07f56aae673208002970b4 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 16 Feb 2022 22:11:39 +0700 Subject: [PATCH] [light classes] simplify hashCode functions ^KTIJ-21151 --- .idea/dictionaries/dimonchik0036.xml | 7 +++ .../classes/KtLightClassForFacadeImpl.kt | 48 +++++-------------- .../asJava/classes/KtLightClassForScript.kt | 21 ++------ .../KtLightClassForSourceDeclaration.kt | 4 +- .../classes/ultraLightMembersCreator.kt | 6 +-- .../asJava/classes/ultraLightMethods.kt | 18 ++++--- .../asJava/classes/ultraLightParameters.kt | 25 +++++----- .../asJava/elements/KtLightFieldImpl.kt | 24 +++------- .../asJava/elements/KtLightMethodImpl.kt | 7 ++- 9 files changed, 59 insertions(+), 101 deletions(-) create mode 100644 .idea/dictionaries/dimonchik0036.xml diff --git a/.idea/dictionaries/dimonchik0036.xml b/.idea/dictionaries/dimonchik0036.xml new file mode 100644 index 00000000000..1c9cabd83fb --- /dev/null +++ b/.idea/dictionaries/dimonchik0036.xml @@ -0,0 +1,7 @@ + + + + ktij + + + \ No newline at end of file diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForFacadeImpl.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForFacadeImpl.kt index f7c5a41ccd0..60c0ec74421 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForFacadeImpl.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForFacadeImpl.kt @@ -1,17 +1,6 @@ /* - * 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. - * 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. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.asJava.classes @@ -63,24 +52,20 @@ open class KtLightClassForFacadeImpl constructor( private val firstFileInFacade by lazyPub { files.iterator().next() } - private val hashCode: Int = - computeHashCode() - - private val packageFqName: FqName = - facadeClassFqName.parent() - private val modifierList: PsiModifierList = LightModifierList(manager, KotlinLanguage.INSTANCE, PsiModifier.PUBLIC, PsiModifier.FINAL) private val implementsList: LightEmptyImplementsList = LightEmptyImplementsList(manager) - private val packageClsFile = FakeFileForLightClass( - firstFileInFacade, - lightClass = { this }, - stub = { javaFileStub }, - packageFqName = packageFqName - ) + private val packageClsFile by lazyPub { + FakeFileForLightClass( + firstFileInFacade, + lightClass = { this }, + stub = { javaFileStub }, + packageFqName = facadeClassFqName.parent() + ) + } override fun getParent(): PsiElement = containingFile @@ -210,14 +195,7 @@ open class KtLightClassForFacadeImpl constructor( return arrayOf(PsiType.getJavaLangObject(manager, resolveScope)) } - override fun hashCode() = hashCode - - private fun computeHashCode(): Int { - var result = manager.hashCode() - result = 31 * result + files.hashCode() - result = 31 * result + facadeClassFqName.hashCode() - return result - } + override fun hashCode() = facadeClassFqName.hashCode() override fun equals(other: Any?): Boolean { if (other == null || this::class.java != other::class.java) { @@ -227,10 +205,8 @@ open class KtLightClassForFacadeImpl constructor( val lightClass = other as KtLightClassForFacadeImpl if (this === other) return true - if (this.hashCode != lightClass.hashCode) return false - if (manager != lightClass.manager) return false - if (files != lightClass.files) return false if (facadeClassFqName != lightClass.facadeClassFqName) return false + if (files != lightClass.files) return false return true } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForScript.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForScript.kt index 7d55e527c14..9a140e4b9b6 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForScript.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForScript.kt @@ -1,11 +1,10 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.asJava.classes -import com.intellij.openapi.util.Comparing import com.intellij.openapi.util.Key import com.intellij.psi.* import com.intellij.psi.impl.PsiSuperMethodImplUtil @@ -43,10 +42,6 @@ open class KtLightClassForScript(val script: KtScript) : KtLazyLightClass(script protected open val javaFileStub: PsiJavaFileStub? get() = getLightClassDataHolder().javaFileStub - private val hashCode: Int = computeHashCode() - - private val packageFqName: FqName = script.fqName.parent() - private val modifierList: PsiModifierList = LightModifierList(manager, KotlinLanguage.INSTANCE, PsiModifier.PUBLIC) private val scriptImplementsList: LightEmptyImplementsList = LightEmptyImplementsList(manager) @@ -62,13 +57,13 @@ open class KtLightClassForScript(val script: KtScript) : KtLazyLightClass(script script.containingKtFile, lightClass = { this }, stub = { javaFileStub }, - packageFqName = packageFqName, + packageFqName = fqName.parent(), ) } override val kotlinOrigin: KtClassOrObject? get() = null - val fqName: FqName = script.fqName + val fqName: FqName get() = script.fqName override fun getModifierList() = modifierList @@ -164,13 +159,7 @@ open class KtLightClassForScript(val script: KtScript) : KtLazyLightClass(script override fun getScope(): PsiElement = parent - override fun hashCode() = hashCode - - private fun computeHashCode(): Int { - var result = manager.hashCode() - result = 31 * result + script.hashCode() - return result - } + override fun hashCode() = script.hashCode() override fun equals(other: Any?): Boolean { if (other == null || this::class.java != other::class.java) { @@ -180,8 +169,6 @@ open class KtLightClassForScript(val script: KtScript) : KtLazyLightClass(script val lightClass = other as? KtLightClassForScript ?: return false if (this === other) return true - if (this.hashCode != lightClass.hashCode) return false - if (manager != lightClass.manager) return false if (script != lightClass.script) return false return true diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForSourceDeclaration.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForSourceDeclaration.kt index 778e5efcb3e..64fd436fda4 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForSourceDeclaration.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassForSourceDeclaration.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -168,8 +168,8 @@ abstract class KtLightClassForSourceDeclaration( val aClass = other as KtLightClassForSourceDeclaration - if (classOrObject != aClass.classOrObject) return false if (jvmDefaultMode != aClass.jvmDefaultMode) return false + if (classOrObject != aClass.classOrObject) return false return true } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMembersCreator.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMembersCreator.kt index 7abe10e0026..e422ce8a36b 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMembersCreator.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMembersCreator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -35,7 +35,7 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.isPublishedApi import org.jetbrains.kotlin.resolve.inline.isInlineOnly -import org.jetbrains.kotlin.resolve.jvm.annotations.* +import org.jetbrains.kotlin.resolve.jvm.annotations.hasJvmSyntheticAnnotation import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind internal class UltraLightMembersCreator( @@ -160,7 +160,7 @@ internal class UltraLightMembersCreator( other.psiMethod == psiMethod && other.expression == expression - override fun hashCode(): Int = psiMethod.hashCode() * 31 + expression.hashCode() + override fun hashCode(): Int = psiMethod.hashCode() override fun toString(): String = "KtUltraLightAnnotationMethod(method=$psiMethod, expression=$expression" diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt index ad65a96ffac..724290442b6 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -137,12 +137,10 @@ internal abstract class KtUltraLightMethod( override fun equals(other: Any?): Boolean = other === this || other is KtUltraLightMethod && other.methodIndex == methodIndex && - other.delegate == delegate && - super.equals(other) + super.equals(other) && + other.delegate == delegate - override fun hashCode(): Int = super.hashCode() - .times(31).plus(delegate.hashCode()) - .times(31).plus(methodIndex.hashCode()) + override fun hashCode(): Int = super.hashCode().times(31).plus(methodIndex.hashCode()) override fun isDeprecated(): Boolean = _deprecated } @@ -196,10 +194,10 @@ internal class KtUltraLightMethodForSourceDeclaration( override val checkNeedToErasureParametersTypes: Boolean by lazyPub { computeCheckNeedToErasureParametersTypes(methodDescriptor) } - override fun equals(other: Any?): Boolean = - other is KtUltraLightMethodForSourceDeclaration && - other.forceToSkipNullabilityAnnotation == forceToSkipNullabilityAnnotation && - super.equals(other) + override fun equals(other: Any?): Boolean = other === this || + other is KtUltraLightMethodForSourceDeclaration && + other.forceToSkipNullabilityAnnotation == forceToSkipNullabilityAnnotation && + super.equals(other) override fun hashCode(): Int = super.hashCode() * 31 + forceToSkipNullabilityAnnotation.hashCode() } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt index d26a2711ffa..4fdaa50fb3d 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -17,15 +17,15 @@ import org.jetbrains.kotlin.asJava.elements.* import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.isSuspendFunctionType import org.jetbrains.kotlin.codegen.AsmUtil -import org.jetbrains.kotlin.codegen.coroutines.SUSPEND_FUNCTION_COMPLETION_PARAMETER_NAME -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.load.kotlin.TypeMappingMode -import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.codegen.AsmUtil.LABELED_THIS_PARAMETER import org.jetbrains.kotlin.codegen.AsmUtil.RECEIVER_PARAMETER_NAME +import org.jetbrains.kotlin.codegen.coroutines.SUSPEND_FUNCTION_COMPLETION_PARAMETER_NAME +import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget +import org.jetbrains.kotlin.load.kotlin.TypeMappingMode import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.isPrivate +import org.jetbrains.kotlin.types.KotlinType internal class KtUltraLightSuspendContinuationParameter( private val ktFunction: KtFunction, @@ -57,8 +57,9 @@ internal class KtUltraLightSuspendContinuationParameter( override fun getType(): PsiType = psiType - override fun equals(other: Any?): Boolean = - other is KtUltraLightSuspendContinuationParameter && other.ktFunction === this.ktFunction + override fun equals(other: Any?): Boolean = other === this || + other is KtUltraLightSuspendContinuationParameter && + other.ktFunction === this.ktFunction override fun isVarArgs(): Boolean = false override fun hashCode(): Int = name.hashCode() @@ -80,7 +81,7 @@ internal abstract class KtUltraLightParameter( override val kotlinOrigin: KtParameter?, protected val support: KtUltraLightSupport, private val ultraLightMethod: KtUltraLightMethod -) : org.jetbrains.kotlin.asJava.elements.LightParameter( +) : LightParameter( name, PsiType.NULL, ultraLightMethod, @@ -129,10 +130,12 @@ internal abstract class KtUltraLightParameter( override fun getContainingFile(): PsiFile = method.containingFile override fun getParent(): PsiElement = method.parameterList - override fun equals(other: Any?): Boolean = - other is KtUltraLightParameter && other.kotlinOrigin == this.kotlinOrigin + override fun equals(other: Any?): Boolean = other === this || + other is KtUltraLightParameter && + other.javaClass == this.javaClass && + other.ultraLightMethod == this.ultraLightMethod - override fun hashCode(): Int = kotlinOrigin.hashCode() + override fun hashCode(): Int = name.hashCode() abstract override fun isVarArgs(): Boolean } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightFieldImpl.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightFieldImpl.kt index 778fe9002bc..4f1c9655b4e 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightFieldImpl.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightFieldImpl.kt @@ -1,17 +1,6 @@ /* - * 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. - * 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. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.asJava.elements @@ -58,11 +47,10 @@ sealed class KtLightFieldImpl( return this } - override fun equals(other: Any?): Boolean = - this === other || - (other is KtLightFieldImpl<*> && - this.name == other.name && - this.containingClass == other.containingClass) + override fun equals(other: Any?): Boolean = this === other || + other is KtLightFieldImpl<*> && + this.name == other.name && + this.containingClass == other.containingClass override fun hashCode() = 31 * containingClass.hashCode() + name.hashCode() diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethodImpl.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethodImpl.kt index 1cc2f93684b..6b021987730 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethodImpl.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightMethodImpl.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -153,13 +153,12 @@ open class KtLightMethodImpl protected constructor( override fun equals(other: Any?): Boolean = other === this || other is KtLightMethodImpl && other.javaClass == javaClass && + other.memberIndex == memberIndex && other.containingClass == containingClass && other.lightMemberOrigin == lightMemberOrigin && - other.dummyDelegate == dummyDelegate && - other.memberIndex == memberIndex + other.dummyDelegate == dummyDelegate override fun hashCode(): Int = name.hashCode() - .times(31).plus(lightMemberOrigin.hashCode()) .times(31).plus(containingClass.hashCode()) .times(31).plus(memberIndex.hashCode())