Refactoring: move check for Any member into builtins

This commit is contained in:
Nikolay Krasko
2015-10-28 15:30:23 +03:00
parent 925b0e567e
commit 625907e49d
2 changed files with 9 additions and 4 deletions
@@ -986,6 +986,10 @@ public abstract class KotlinBuiltIns {
return KotlinTypeChecker.DEFAULT.isSubtypeOf(type, getBooleanType()); return KotlinTypeChecker.DEFAULT.isSubtypeOf(type, getBooleanType());
} }
public boolean isMemberOfAny(@NotNull DeclarationDescriptor descriptor) {
return descriptor.getContainingDeclaration() == getAny();
}
public static boolean isString(@Nullable KotlinType type) { public static boolean isString(@Nullable KotlinType type) {
return type != null && isNotNullConstructedFromGivenClass(type, FQ_NAMES.string); return type != null && isNotNullConstructedFromGivenClass(type, FQ_NAMES.string);
} }
@@ -169,8 +169,6 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
} }
} }
private fun FunctionDescriptor.isMemberOfAny() = containingDeclaration == builtIns.any
private fun generateFunctionSkeleton(descriptor: FunctionDescriptor, project: Project): KtNamedFunction { private fun generateFunctionSkeleton(descriptor: FunctionDescriptor, project: Project): KtNamedFunction {
return OverrideMemberChooserObject return OverrideMemberChooserObject
.create(project, descriptor, descriptor, OverrideMemberChooserObject.BodyType.EMPTY) .create(project, descriptor, descriptor, OverrideMemberChooserObject.BodyType.EMPTY)
@@ -196,7 +194,9 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
val bodyText = StringBuilder().apply { val bodyText = StringBuilder().apply {
append("if (this === $paramName) return true\n") append("if (this === $paramName) return true\n")
append("if ($isNotInstanceCondition) return false\n") append("if ($isNotInstanceCondition) return false\n")
if (!superEquals.isMemberOfAny()) {
val builtIns = superEquals.builtIns
if (!builtIns.isMemberOfAny(superEquals)) {
append("if (!super.equals($paramName)) return false\n") append("if (!super.equals($paramName)) return false\n")
} }
@@ -249,10 +249,11 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
val superHashCode = classDescriptor.getSuperClassOrAny().findDeclaredHashCode(true)!! val superHashCode = classDescriptor.getSuperClassOrAny().findDeclaredHashCode(true)!!
val hashCodeFun = generateFunctionSkeleton(superHashCode, project) val hashCodeFun = generateFunctionSkeleton(superHashCode, project)
val builtins = superHashCode.builtIns
val propertyIterator = variablesForHashCode.iterator() val propertyIterator = variablesForHashCode.iterator()
val initialValue = when { val initialValue = when {
!superHashCode.isMemberOfAny() -> "super.hashCode()" !builtins.isMemberOfAny(superHashCode) -> "super.hashCode()"
propertyIterator.hasNext() -> propertyIterator.next().genVariableHashCode(false) propertyIterator.hasNext() -> propertyIterator.next().genVariableHashCode(false)
else -> "0" else -> "0"
} }