From 5e6a3673c8a1fa3b05ef05758761c7d819c02f4d Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 3 Jul 2018 14:16:46 +0300 Subject: [PATCH] Reformat SyntheticJavaPropertiesScope.kt --- .../synthetic/JavaSyntheticPropertiesScope.kt | 220 ++++++++++-------- 1 file changed, 125 insertions(+), 95 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt index 1c852d81faf..620d809ffe7 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt @@ -59,30 +59,34 @@ interface SyntheticJavaPropertyDescriptor : PropertyDescriptor { val originalGetterOrSetter = getterOrSetter.original return syntheticScopes.collectSyntheticExtensionProperties(listOf(classDescriptorOwner.defaultType)) - .filterIsInstance() - .firstOrNull { originalGetterOrSetter == it.getMethod || originalGetterOrSetter == it.setMethod } + .filterIsInstance() + .firstOrNull { originalGetterOrSetter == it.getMethod || originalGetterOrSetter == it.setMethod } } fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, syntheticScope: SyntheticScope) = - findByGetterOrSetter(getterOrSetter, - object : SyntheticScopes { - override val scopes: Collection = listOf(syntheticScope) - }) + findByGetterOrSetter(getterOrSetter, + object : SyntheticScopes { + override val scopes: Collection = listOf(syntheticScope) + }) - fun propertyNameByGetMethodName(methodName: Name): Name? - = org.jetbrains.kotlin.load.java.propertyNameByGetMethodName(methodName) + fun propertyNameByGetMethodName(methodName: Name): Name? = org.jetbrains.kotlin.load.java.propertyNameByGetMethodName(methodName) - fun propertyNameBySetMethodName(methodName: Name, withIsPrefix: Boolean): Name? - = org.jetbrains.kotlin.load.java.propertyNameBySetMethodName(methodName, withIsPrefix) + fun propertyNameBySetMethodName(methodName: Name, withIsPrefix: Boolean): Name? = + org.jetbrains.kotlin.load.java.propertyNameBySetMethodName(methodName, withIsPrefix) } } class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val lookupTracker: LookupTracker) : SyntheticScope { - private val syntheticPropertyInClass = storageManager.createMemoizedFunction, SyntheticPropertyHolder> { pair -> - syntheticPropertyInClassNotCached(pair.first, pair.second) - } + private val syntheticPropertyInClass = + storageManager.createMemoizedFunction, SyntheticPropertyHolder> { pair -> + syntheticPropertyInClassNotCached(pair.first, pair.second) + } - private fun getSyntheticPropertyAndRecordLookups(classifier: ClassDescriptor, name: Name, location: LookupLocation): PropertyDescriptor? { + private fun getSyntheticPropertyAndRecordLookups( + classifier: ClassDescriptor, + name: Name, + location: LookupLocation + ): PropertyDescriptor? { val (descriptor, lookedNames) = syntheticPropertyInClass(Pair(classifier, name)) if (location !is NoLookupLocation) { @@ -119,15 +123,15 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l val possibleGetMethodNames = possibleGetMethodNames(name) val getMethod = possibleGetMethodNames - .flatMap { memberScope.getContributedFunctions(it, NoLookupLocation.FROM_SYNTHETIC_SCOPE) } - .singleOrNull { - it.hasJavaOriginInHierarchy() && isGoodGetMethod(it) - } ?: return result(null, possibleGetMethodNames) + .flatMap { memberScope.getContributedFunctions(it, NoLookupLocation.FROM_SYNTHETIC_SCOPE) } + .singleOrNull { + it.hasJavaOriginInHierarchy() && isGoodGetMethod(it) + } ?: return result(null, possibleGetMethodNames) val setMethodName = setMethodName(getMethod.name) val setMethod = memberScope.getContributedFunctions(setMethodName, NoLookupLocation.FROM_SYNTHETIC_SCOPE) - .singleOrNull { isGoodSetMethod(it, getMethod) } + .singleOrNull { isGoodSetMethod(it, getMethod) } val propertyType = getMethod.returnType!! @@ -140,8 +144,8 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l if (returnType.isUnit()) return false return descriptor.valueParameters.isEmpty() - && descriptor.typeParameters.isEmpty() - && descriptor.visibility.isVisibleOutside() + && descriptor.typeParameters.isEmpty() + && descriptor.visibility.isVisibleOutside() } private fun isGoodSetMethod(descriptor: FunctionDescriptor, getMethod: FunctionDescriptor): Boolean { @@ -150,14 +154,14 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l if (!TypeUtils.equalTypes(parameter.type, propertyType)) { if (!propertyType.isSubtypeOf(parameter.type)) return false if (descriptor.findOverridden { - val baseProperty = SyntheticJavaPropertyDescriptor.findByGetterOrSetter(it, this) - baseProperty?.getMethod?.name == getMethod.name - } == null) return false + val baseProperty = SyntheticJavaPropertyDescriptor.findByGetterOrSetter(it, this) + baseProperty?.getMethod?.name == getMethod.name + } == null) return false } return parameter.varargElementType == null - && descriptor.typeParameters.isEmpty() - && descriptor.visibility.isVisibleOutside() + && descriptor.typeParameters.isEmpty() + && descriptor.visibility.isVisibleOutside() } private fun FunctionDescriptor.findOverridden(condition: (FunctionDescriptor) -> Boolean): FunctionDescriptor? { @@ -168,7 +172,11 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l return null } - override fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation): Collection { + override fun getSyntheticExtensionProperties( + receiverTypes: Collection, + name: Name, + location: LookupLocation + ): Collection { var result: SmartList? = null val processedTypes: MutableSet? = if (receiverTypes.size > 1) HashSet() else null for (type in receiverTypes) { @@ -181,22 +189,25 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l } } - override fun getSyntheticStaticFunctions(scope: ResolutionScope, name: Name, location: LookupLocation): Collection - = emptyList() + override fun getSyntheticStaticFunctions(scope: ResolutionScope, name: Name, location: LookupLocation): Collection = + emptyList() - override fun getSyntheticConstructors(scope: ResolutionScope, name: Name, location: LookupLocation): Collection - = emptyList() + override fun getSyntheticConstructors(scope: ResolutionScope, name: Name, location: LookupLocation): Collection = + emptyList() - override fun getSyntheticStaticFunctions(scope: ResolutionScope): Collection - = emptyList() + override fun getSyntheticStaticFunctions(scope: ResolutionScope): Collection = emptyList() - override fun getSyntheticConstructors(scope: ResolutionScope): Collection - = emptyList() + override fun getSyntheticConstructors(scope: ResolutionScope): Collection = emptyList() - override fun getSyntheticConstructor(constructor: ConstructorDescriptor): ConstructorDescriptor? - = null + override fun getSyntheticConstructor(constructor: ConstructorDescriptor): ConstructorDescriptor? = null - private fun collectSyntheticPropertiesByName(result: SmartList?, type: TypeConstructor, name: Name, processedTypes: MutableSet?, location: LookupLocation): SmartList? { + private fun collectSyntheticPropertiesByName( + result: SmartList?, + type: TypeConstructor, + name: Name, + processedTypes: MutableSet?, + location: LookupLocation + ): SmartList? { if (processedTypes != null && !processedTypes.add(type)) return result @Suppress("NAME_SHADOWING") @@ -205,8 +216,7 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l val classifier = type.declarationDescriptor if (classifier is ClassDescriptor) { result = result.add(getSyntheticPropertyAndRecordLookups(classifier, name, location)) - } - else { + } else { type.supertypes.forEach { result = collectSyntheticPropertiesByName(result, it.constructor, name, processedTypes, location) } } @@ -220,7 +230,10 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l return result } - private fun MutableList.collectSyntheticProperties(type: TypeConstructor, processedTypes: MutableSet) { + private fun MutableList.collectSyntheticProperties( + type: TypeConstructor, + processedTypes: MutableSet + ) { if (!processedTypes.add(type)) return val classifier = type.declarationDescriptor @@ -231,8 +244,7 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l addIfNotNull(syntheticPropertyInClass(Pair(classifier, propertyName)).descriptor) } } - } - else { + } else { type.supertypes.forEach { collectSyntheticProperties(it.constructor, processedTypes) } } } @@ -244,7 +256,12 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l return list } - override fun getSyntheticMemberFunctions(receiverTypes: Collection, name: Name, location: LookupLocation): Collection = emptyList() + override fun getSyntheticMemberFunctions( + receiverTypes: Collection, + name: Name, + location: LookupLocation + ): Collection = emptyList() + override fun getSyntheticMemberFunctions(receiverTypes: Collection): Collection = emptyList() private data class SyntheticPropertyHolder(val descriptor: PropertyDescriptor?, val lookedNames: List) { @@ -254,19 +271,19 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l } private class MyPropertyDescriptor( - containingDeclaration: DeclarationDescriptor, - original: PropertyDescriptor?, - annotations: Annotations, - modality: Modality, - visibility: Visibility, - isVar: Boolean, - name: Name, - kind: CallableMemberDescriptor.Kind, - source: SourceElement + containingDeclaration: DeclarationDescriptor, + original: PropertyDescriptor?, + annotations: Annotations, + modality: Modality, + visibility: Visibility, + isVar: Boolean, + name: Name, + kind: CallableMemberDescriptor.Kind, + source: SourceElement ) : SyntheticJavaPropertyDescriptor, PropertyDescriptorImpl( - containingDeclaration, original, annotations, modality, visibility, isVar, name, kind, source, - /* lateInit = */ false, /* isConst = */ false, /* isExpect = */ false, /* isActual = */ false, /* isExternal = */ false, - /* isDelegated = */ false + containingDeclaration, original, annotations, modality, visibility, isVar, name, kind, source, + /* lateInit = */ false, /* isConst = */ false, /* isExpect = */ false, /* isActual = */ false, /* isExternal = */ false, + /* isDelegated = */ false ) { override var getMethod: FunctionDescriptor by Delegates.notNull() @@ -276,51 +293,64 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l private set companion object { - fun create(ownerClass: ClassDescriptor, getMethod: FunctionDescriptor, setMethod: FunctionDescriptor?, name: Name, type: KotlinType): MyPropertyDescriptor { + fun create( + ownerClass: ClassDescriptor, + getMethod: FunctionDescriptor, + setMethod: FunctionDescriptor?, + name: Name, + type: KotlinType + ): MyPropertyDescriptor { val visibility = syntheticVisibility(getMethod, isUsedForExtension = true) - val descriptor = MyPropertyDescriptor(DescriptorUtils.getContainingModule(ownerClass), - null, - Annotations.EMPTY, - Modality.FINAL, - visibility, - setMethod != null, - name, - CallableMemberDescriptor.Kind.SYNTHESIZED, - SourceElement.NO_SOURCE) + val descriptor = MyPropertyDescriptor( + DescriptorUtils.getContainingModule(ownerClass), + null, + Annotations.EMPTY, + Modality.FINAL, + visibility, + setMethod != null, + name, + CallableMemberDescriptor.Kind.SYNTHESIZED, + SourceElement.NO_SOURCE + ) descriptor.getMethod = getMethod descriptor.setMethod = setMethod val classTypeParams = ownerClass.typeConstructor.parameters val typeParameters = ArrayList(classTypeParams.size) - val typeSubstitutor = DescriptorSubstitutor.substituteTypeParameters(classTypeParams, TypeSubstitution.EMPTY, descriptor, typeParameters) + val typeSubstitutor = + DescriptorSubstitutor.substituteTypeParameters(classTypeParams, TypeSubstitution.EMPTY, descriptor, typeParameters) val propertyType = typeSubstitutor.safeSubstitute(type, Variance.INVARIANT) val receiverType = typeSubstitutor.safeSubstitute(ownerClass.defaultType, Variance.INVARIANT) descriptor.setType(propertyType, typeParameters, null, receiverType) - val getter = PropertyGetterDescriptorImpl(descriptor, - getMethod.annotations, - Modality.FINAL, - visibility, - false, - getMethod.isExternal, - false, - CallableMemberDescriptor.Kind.SYNTHESIZED, - null, - SourceElement.NO_SOURCE) + val getter = PropertyGetterDescriptorImpl( + descriptor, + getMethod.annotations, + Modality.FINAL, + visibility, + false, + getMethod.isExternal, + false, + CallableMemberDescriptor.Kind.SYNTHESIZED, + null, + SourceElement.NO_SOURCE + ) getter.initialize(null) val setter = if (setMethod != null) - PropertySetterDescriptorImpl(descriptor, - setMethod.annotations, - Modality.FINAL, - syntheticVisibility(setMethod, isUsedForExtension = true), - false, - setMethod.isExternal, - false, - CallableMemberDescriptor.Kind.SYNTHESIZED, - null, - SourceElement.NO_SOURCE) + PropertySetterDescriptorImpl( + descriptor, + setMethod.annotations, + Modality.FINAL, + syntheticVisibility(setMethod, isUsedForExtension = true), + false, + setMethod.isExternal, + false, + CallableMemberDescriptor.Kind.SYNTHESIZED, + null, + SourceElement.NO_SOURCE + ) else null setter?.initializeDefault() @@ -332,12 +362,12 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l } override fun createSubstitutedCopy( - newOwner: DeclarationDescriptor, - newModality: Modality, - newVisibility: Visibility, - original: PropertyDescriptor?, - kind: CallableMemberDescriptor.Kind, - newName: Name + newOwner: DeclarationDescriptor, + newModality: Modality, + newVisibility: Visibility, + original: PropertyDescriptor?, + kind: CallableMemberDescriptor.Kind, + newName: Name ): PropertyDescriptorImpl { return MyPropertyDescriptor(newOwner, this, annotations, newModality, newVisibility, isVar, newName, kind, source).apply { getMethod = this@MyPropertyDescriptor.getMethod @@ -357,8 +387,8 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l } val classParametersSubstitutor = TypeConstructorSubstitution.createByConstructorsMap( - substitutionMap, - approximateCapturedTypes = true + substitutionMap, + approximateCapturedTypes = true ).buildSubstitutor() descriptor.getMethod = getMethod.substitute(classParametersSubstitutor) ?: return null