Refactoring. Remove FlexibleTypeFactory.DEFAULT and refactor flexible type creation by special fq-name for tests.

This commit is contained in:
Stanislav Erokhin
2016-04-22 18:40:23 +03:00
parent 0a4ad3f267
commit a1d052b8fa
7 changed files with 58 additions and 64 deletions
@@ -24,8 +24,8 @@ import org.jetbrains.kotlin.context.ModuleContext
import org.jetbrains.kotlin.descriptors.PackagePartProvider
import org.jetbrains.kotlin.frontend.di.configureModule
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.load.java.InternalFlexibleTypeTransformer
import org.jetbrains.kotlin.load.java.JavaClassFinderImpl
import org.jetbrains.kotlin.load.java.JavaFlexibleTypeFactoryProvider
import org.jetbrains.kotlin.load.java.components.*
import org.jetbrains.kotlin.load.java.lazy.ModuleClassResolver
import org.jetbrains.kotlin.load.java.lazy.SingleModuleClassResolver
@@ -62,7 +62,7 @@ fun StorageComponentContainer.configureJavaTopDownAnalysis(moduleContentScope: G
useInstance(SamConversionResolverImpl)
useImpl<JavaSourceElementFactoryImpl>()
useImpl<JavaLazyAnalyzerPostConstruct>()
useInstance(JavaFlexibleTypeFactoryProvider)
useInstance(InternalFlexibleTypeTransformer)
}
fun createContainerForLazyResolveWithJava(
@@ -0,0 +1,42 @@
/*
* 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.load.java
import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.TypeResolver.TypeTransformerForTests
import org.jetbrains.kotlin.types.KotlinType
object InternalFlexibleTypeTransformer : TypeTransformerForTests() {
// This is a "magic" classifier: when type resolver sees it in the code, e.g. ft<Foo, Foo?>, instead of creating a normal type,
// it creates a flexible type, e.g. (Foo..Foo?).
// This is used in tests and Evaluate Expression to have flexible types in the code,
// but normal users should not be referencing this classifier
@JvmField
val FLEXIBLE_TYPE_CLASSIFIER: ClassId = ClassId.topLevel(FqName("kotlin.internal.flexible.ft"))
override fun transformType(kotlinType: KotlinType): KotlinType? {
val descriptor = kotlinType.constructor.declarationDescriptor
if (descriptor != null && FLEXIBLE_TYPE_CLASSIFIER.asSingleFqName().toUnsafe() == DescriptorUtils.getFqName(descriptor)
&& kotlinType.arguments.size == 2) {
return FlexibleJavaClassifierTypeFactory.create(kotlinType.arguments[0].type, kotlinType.arguments[1].type)
}
return null
}
}
@@ -1,25 +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.load.java
import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver
import org.jetbrains.kotlin.resolve.TypeResolver.FlexibleTypeFactoryProvider
import org.jetbrains.kotlin.types.FlexibleTypeFactory
object JavaFlexibleTypeFactoryProvider : FlexibleTypeFactoryProvider() {
override val factory: FlexibleTypeFactory get() = LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory
}
@@ -50,7 +50,7 @@ class TypeResolver(
private val annotationResolver: AnnotationResolver,
private val qualifiedExpressionResolver: QualifiedExpressionResolver,
private val moduleDescriptor: ModuleDescriptor,
private val flexibleTypeFactoryProvider: FlexibleTypeFactoryProvider,
private val typeTransformerForTests: TypeTransformerForTests,
private val storageManager: StorageManager,
private val lazinessToken: TypeLazinessToken,
private val dynamicTypesSettings: DynamicTypesSettings,
@@ -58,8 +58,8 @@ class TypeResolver(
private val identifierChecker: IdentifierChecker
) {
open class FlexibleTypeFactoryProvider {
open val factory: FlexibleTypeFactory get() = FlexibleTypeFactory.DEFAULT
open class TypeTransformerForTests {
open fun transformType(kotlinType: KotlinType): KotlinType? = null
}
fun resolveType(scope: LexicalScope, typeReference: KtTypeReference, trace: BindingTrace, checkBounds: Boolean): KotlinType {
@@ -367,16 +367,12 @@ class TypeResolver(
" but ${collectedArgumentAsTypeProjections.size} instead of ${parameters.size} found in ${type.text}"
}
if (Flexibility.FLEXIBLE_TYPE_CLASSIFIER.asSingleFqName().toUnsafe() == DescriptorUtils.getFqName(classDescriptor)
&& parameters.size == 2) {
// We create flexible types by convention here
// This is not intended to be used in normal users' environments, only for tests and debugger etc
return type(flexibleTypeFactoryProvider.factory.create(arguments[0].type,
arguments[1].type)
)
}
val resultingType = KotlinTypeImpl.create(annotations, classDescriptor, false, arguments)
// We create flexible types by convention here
// This is not intended to be used in normal users' environments, only for tests and debugger etc
typeTransformerForTests.transformType(resultingType)?.let { return type(it) }
if (c.checkBounds) {
val substitutor = TypeSubstitutor.create(resultingType)
for (i in parameters.indices) {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* 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.
@@ -35,12 +35,12 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.asJava.DuplicateJvmSignatureUtilKt;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.diagnostics.*;
import org.jetbrains.kotlin.load.java.InternalFlexibleTypeTransformer;
import org.jetbrains.kotlin.psi.KtDeclaration;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.types.Flexibility;
import org.junit.Assert;
import java.io.File;
@@ -73,8 +73,8 @@ public abstract class BaseDiagnosticsTest
public static final String CHECK_TYPE_IMPORT = "import " + CHECK_TYPE_PACKAGE + ".*";
public static final String EXPLICIT_FLEXIBLE_TYPES_DIRECTIVE = "EXPLICIT_FLEXIBLE_TYPES";
public static final String EXPLICIT_FLEXIBLE_PACKAGE = Flexibility.Companion.getFLEXIBLE_TYPE_CLASSIFIER().getPackageFqName().asString();
public static final String EXPLICIT_FLEXIBLE_CLASS_NAME = Flexibility.Companion.getFLEXIBLE_TYPE_CLASSIFIER().getRelativeClassName().asString();
public static final String EXPLICIT_FLEXIBLE_PACKAGE = InternalFlexibleTypeTransformer.FLEXIBLE_TYPE_CLASSIFIER.getPackageFqName().asString();
public static final String EXPLICIT_FLEXIBLE_CLASS_NAME = InternalFlexibleTypeTransformer.FLEXIBLE_TYPE_CLASSIFIER.getRelativeClassName().asString();
private static final String EXPLICIT_FLEXIBLE_TYPES_DECLARATIONS
= "\npackage " + EXPLICIT_FLEXIBLE_PACKAGE +
"\npublic class " + EXPLICIT_FLEXIBLE_CLASS_NAME + "<L, U>";
@@ -16,34 +16,15 @@
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
interface FlexibleTypeFactory {
val id: String
fun create(lowerBound: KotlinType, upperBound: KotlinType): KotlinType
object DEFAULT : FlexibleTypeFactory {
override val id: String get() = "NONE"
override fun create(lowerBound: KotlinType, upperBound: KotlinType): KotlinType {
if (lowerBound == upperBound) return lowerBound
return object : DelegatingFlexibleType(lowerBound, upperBound, this@DEFAULT) {}
}
}
}
interface Flexibility : TypeCapability, SubtypingRepresentatives {
companion object {
// This is a "magic" classifier: when type resolver sees it in the code, e.g. ft<Foo, Foo?>, instead of creating a normal type,
// it creates a flexible type, e.g. (Foo..Foo?).
// This is used in tests and Evaluate Expression to have flexible types in the code,
// but normal users should not be referencing this classifier
val FLEXIBLE_TYPE_CLASSIFIER: ClassId = ClassId.topLevel(FqName("kotlin.internal.flexible.ft"))
}
// lowerBound is a subtype of upperBound
val lowerBound: KotlinType
val upperBound: KotlinType
@@ -25,11 +25,11 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.PropertyGetterDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.types.FlexibleTypeFactory
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeImpl
import org.jetbrains.kotlin.types.StarProjectionImpl
@@ -109,7 +109,7 @@ private fun genProperty(
override val resourceId = id
}
val flexibleType = FlexibleTypeFactory.DEFAULT.create(type, type.makeNullable())
val flexibleType = LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory.create(type, type.makeNullable())
property.setType(
flexibleType,
emptyList<TypeParameterDescriptor>(),