Resolve type alias SAM constructors in synthetic scope

This commit is contained in:
Mikhail Zarechenskiy
2017-05-04 03:11:40 +03:00
parent 7541a3754d
commit e821b25288
10 changed files with 43 additions and 130 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 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.
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.resolve.calls.checkers.ReifiedTypeParameterSubstitut
import org.jetbrains.kotlin.resolve.checkers.HeaderImplDeclarationChecker
import org.jetbrains.kotlin.resolve.jvm.*
import org.jetbrains.kotlin.resolve.jvm.checkers.*
import org.jetbrains.kotlin.synthetic.JavaSyntheticConstructorsProvider
import org.jetbrains.kotlin.synthetic.JavaSyntheticScopes
import org.jetbrains.kotlin.types.DynamicTypesSettings
@@ -86,7 +85,6 @@ object JvmPlatformConfigurator : PlatformConfigurator(
container.useImpl<JavaSyntheticScopes>()
container.useImpl<InterfaceDefaultMethodCallChecker>()
container.useImpl<InlinePlatformCompatibilityChecker>()
container.useInstance(JavaSyntheticConstructorsProvider)
container.useInstance(JvmTypeSpecificityComparator)
}
}
@@ -1,56 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.synthetic
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptorImpl
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider
import java.lang.AssertionError
object JavaSyntheticConstructorsProvider : SyntheticConstructorsProvider {
override fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection<FunctionDescriptor> {
if (classifier is TypeAliasDescriptor) {
return getSyntheticTypeAliasConstructors(classifier, location)
}
return emptyList()
}
private fun getSyntheticTypeAliasConstructors(typeAliasDescriptor: TypeAliasDescriptor, location: LookupLocation): Collection<FunctionDescriptor> {
val classDescriptor = typeAliasDescriptor.classDescriptor
if (classDescriptor !is LazyJavaClassDescriptor || classDescriptor.functionTypeForSamInterface == null) return emptyList()
val containingDeclaration = classDescriptor.containingDeclaration
val outerScope = when (containingDeclaration) {
is ClassDescriptor ->
containingDeclaration.staticScope
is PackageFragmentDescriptor ->
containingDeclaration.getMemberScope()
else ->
throw AssertionError("Unexpected containing declaration for $classDescriptor: $containingDeclaration")
}
return outerScope.getContributedFunctions(classDescriptor.name, location)
.filterIsInstance<SamConstructorDescriptorImpl>()
.filter { it.baseDescriptorForSynthetic == classDescriptor }
.map { SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(typeAliasDescriptor, it) }
}
}
@@ -133,12 +133,33 @@ class SamAdapterFunctionsScope(
override fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>): Collection<PropertyDescriptor> = emptyList()
override fun getSyntheticStaticFunctions(scope: ResolutionScope, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
val samConstructor = scope.getContributedClassifier(name, location)?.let { samConstructorForClassifier(it) }
val classifier = scope.getContributedClassifier(name, location)
val samConstructor = classifier?.let { getSamConstructor(it) }
return scope.getContributedFunctions(name, location).mapNotNull { samAdapterForStaticFunction(it) } + listOfNotNull(samConstructor)
}
override fun getSyntheticStaticFunctions(scope: ResolutionScope): Collection<FunctionDescriptor> {
return scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS).mapNotNull { samAdapterForStaticFunction(it) }
val samConstructors =
scope.getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS)
.filterIsInstance<ClassifierDescriptor>()
.mapNotNull{ getSamConstructor(it) }
return scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS).mapNotNull { samAdapterForStaticFunction(it) } +
samConstructors
}
private fun getSamConstructor(classifier: ClassifierDescriptor): SamConstructorDescriptor? {
return when (classifier) {
is TypeAliasDescriptor -> getTypeAliasSamConstructor(classifier)
else -> samConstructorForClassifier(classifier)
}
}
private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? {
val classDescriptor = classifier.classDescriptor ?: return null
val samConstructor = samConstructorForClassifier(classDescriptor) ?: return null
return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(classifier, samConstructor)
}
private class MyFunctionDescriptor(