Reformat SyntheticJavaPropertiesScope.kt

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