Minor. removed usage WritableScopeImpl from tests
This commit is contained in:
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.util.collectionUtils.getFirstMatch
|
||||
import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
|
||||
public class LexicalChainedScope(
|
||||
public class LexicalChainedScope @JvmOverloads constructor(
|
||||
parent: LexicalScope,
|
||||
override val ownerDescriptor: DeclarationDescriptor,
|
||||
override val isOwnerDescriptorAccessibleByLabel: Boolean,
|
||||
|
||||
+25
-4
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.cli.common.output.outputUtils.writeAllTo
|
||||
import org.jetbrains.kotlin.codegen.GenerationUtils
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||
import org.jetbrains.kotlin.jvm.compiler.ExpectedLoadErrorsUtil
|
||||
import org.jetbrains.kotlin.jvm.compiler.LoadDescriptorUtil
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
|
||||
@@ -30,6 +31,7 @@ import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
import org.jetbrains.kotlin.load.kotlin.reflect.ReflectKotlinClass
|
||||
import org.jetbrains.kotlin.load.kotlin.reflect.RuntimeModuleData
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
|
||||
import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy
|
||||
@@ -47,6 +49,7 @@ import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
import org.jetbrains.kotlin.utils.sure
|
||||
import java.io.File
|
||||
import java.net.URLClassLoader
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdir() {
|
||||
@@ -187,10 +190,7 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi
|
||||
private val scope: KtScope
|
||||
|
||||
init {
|
||||
val writableScope = WritableScopeImpl(KtScope.Empty, this, RedeclarationHandler.THROW_EXCEPTION, "runtime descriptor loader test")
|
||||
classes.forEach { writableScope.addClassifierDescriptor(it) }
|
||||
writableScope.changeLockLevel(LexicalWritableScope.LockLevel.READING)
|
||||
scope = ChainedScope(this, "synthetic package view for test", writableScope, *packageScopes.toTypedArray())
|
||||
scope = ChainedScope(this, "synthetic package view for test", ScopeWithClassifiers(classes, this), *packageScopes.toTypedArray())
|
||||
}
|
||||
|
||||
override val fqName: FqName
|
||||
@@ -209,4 +209,25 @@ public abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdi
|
||||
override val fragments: Nothing
|
||||
get() = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
private class ScopeWithClassifiers(classifiers: List<ClassifierDescriptor>, ownerDescriptor: DeclarationDescriptor)
|
||||
: SimpleKtScope(ownerDescriptor, "runtime descriptor loader test") {
|
||||
|
||||
private val classifierMap = HashMap<Name, ClassifierDescriptor>()
|
||||
val redeclarationHandler = RedeclarationHandler.THROW_EXCEPTION
|
||||
|
||||
init {
|
||||
for (classifier in classifiers) {
|
||||
classifierMap.put(classifier.name, classifier)?.let {
|
||||
redeclarationHandler.handleRedeclaration(it, classifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = classifierMap[name]
|
||||
|
||||
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = classifierMap.values
|
||||
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> = classifierMap.values
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.types;
|
||||
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.context.ContextKt;
|
||||
@@ -30,6 +33,7 @@ import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
|
||||
import org.jetbrains.kotlin.resolve.lazy.ResolveSession;
|
||||
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory;
|
||||
import org.jetbrains.kotlin.resolve.scopes.*;
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
import org.jetbrains.kotlin.test.KotlinLiteFixture;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
@@ -65,7 +69,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
|
||||
private final ModuleDescriptorImpl root = KotlinTestUtils.createEmptyModule("<test_root>");
|
||||
private DescriptorResolver descriptorResolver;
|
||||
private FunctionDescriptorResolver functionDescriptorResolver;
|
||||
private KtScope scope;
|
||||
private LexicalScope scope;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
ContainerForTests containerForTests = InjectionKt.createContainerForTests(getProject(), root);
|
||||
@@ -79,20 +83,24 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
|
||||
descriptorResolver = null;
|
||||
}
|
||||
|
||||
private KtScope createScope(KtScope libraryScope) {
|
||||
@NotNull
|
||||
private LexicalScope createScope(@NotNull KtScope libraryScope) {
|
||||
KtFile file = KtPsiFactoryKt
|
||||
.KtPsiFactory(getProject()).createFile("abstract class C { abstract fun foo(); abstract val a: Int }");
|
||||
List<KtDeclaration> declarations = file.getDeclarations();
|
||||
KtDeclaration aClass = declarations.get(0);
|
||||
assert aClass instanceof KtClass;
|
||||
AnalysisResult bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(file);
|
||||
DeclarationDescriptor classDescriptor = bindingContext.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, aClass);
|
||||
WritableScopeImpl scope = new WritableScopeImpl(
|
||||
libraryScope, root, RedeclarationHandler.DO_NOTHING, "JetDefaultModalityModifiersTest");
|
||||
assert classDescriptor instanceof ClassifierDescriptor;
|
||||
scope.addClassifierDescriptor((ClassifierDescriptor) classDescriptor);
|
||||
scope.changeLockLevel(LexicalWritableScope.LockLevel.READING);
|
||||
return scope;
|
||||
final DeclarationDescriptor classDescriptor = bindingContext.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, aClass);
|
||||
return new LexicalScopeImpl(ScopeUtilsKt.memberScopeAsImportingScope(libraryScope), root, false, null,
|
||||
"JetDefaultModalityModifiersTest", RedeclarationHandler.DO_NOTHING,
|
||||
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
|
||||
@Override
|
||||
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
|
||||
handler.addClassifierDescriptor((ClassifierDescriptor) classDescriptor);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ClassDescriptorWithResolutionScopes createClassDescriptor(ClassKind kind, KtClass aClass) {
|
||||
@@ -123,7 +131,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
|
||||
List<KtDeclaration> declarations = aClass.getDeclarations();
|
||||
KtNamedFunction function = (KtNamedFunction) declarations.get(0);
|
||||
SimpleFunctionDescriptor functionDescriptor =
|
||||
functionDescriptorResolver.resolveFunctionDescriptor(classDescriptor, TypeTestUtilsKt.asLexicalScope(scope), function,
|
||||
functionDescriptorResolver.resolveFunctionDescriptor(classDescriptor, scope, function,
|
||||
KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY);
|
||||
|
||||
assertEquals(expectedFunctionModality, functionDescriptor.getModality());
|
||||
@@ -136,7 +144,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
|
||||
List<KtDeclaration> declarations = aClass.getDeclarations();
|
||||
KtProperty property = (KtProperty) declarations.get(0);
|
||||
PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(
|
||||
classDescriptor, TypeTestUtilsKt.asLexicalScope(scope), property, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY);
|
||||
classDescriptor, scope, property, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY);
|
||||
|
||||
assertEquals(expectedPropertyModality, propertyDescriptor.getModality());
|
||||
}
|
||||
@@ -149,7 +157,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture {
|
||||
List<KtDeclaration> declarations = aClass.getDeclarations();
|
||||
KtProperty property = (KtProperty) declarations.get(0);
|
||||
PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePropertyDescriptor(
|
||||
classDescriptor, TypeTestUtilsKt.asLexicalScope(scope), property, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY);
|
||||
classDescriptor, scope, property, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY);
|
||||
PropertyAccessorDescriptor propertyAccessor = isGetter
|
||||
? propertyDescriptor.getGetter()
|
||||
: propertyDescriptor.getSetter();
|
||||
|
||||
@@ -21,8 +21,11 @@ import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.Function;
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
|
||||
@@ -38,11 +41,13 @@ import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
|
||||
import org.jetbrains.kotlin.psi.KtTypeReference;
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.kotlin.resolve.AnalyzingUtils;
|
||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||
import org.jetbrains.kotlin.resolve.BindingTrace;
|
||||
import org.jetbrains.kotlin.resolve.BindingTraceContext;
|
||||
import org.jetbrains.kotlin.resolve.lazy.KotlinTestWithEnvironment;
|
||||
import org.jetbrains.kotlin.resolve.lazy.LazyResolveTestUtil;
|
||||
import org.jetbrains.kotlin.resolve.scopes.*;
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.tests.di.ContainerForTests;
|
||||
@@ -55,7 +60,7 @@ import java.util.Map;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
|
||||
private KtScope scope;
|
||||
private LexicalScope scope;
|
||||
private ContainerForTests container;
|
||||
|
||||
@Override
|
||||
@@ -78,26 +83,32 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
private KtScope getContextScope() throws IOException {
|
||||
private LexicalScope getContextScope() throws IOException {
|
||||
// todo comments
|
||||
String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
|
||||
KtFile jetFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
|
||||
ModuleDescriptor module = LazyResolveTestUtil.resolveLazily(Collections.singletonList(jetFile), getEnvironment());
|
||||
KtScope topLevelDeclarations = module.getPackage(FqName.ROOT).getMemberScope();
|
||||
ClassifierDescriptor contextClass = topLevelDeclarations.getClassifier(Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
|
||||
BindingTrace trace = new CliLightClassGenerationSupport.CliBindingTrace();
|
||||
ModuleDescriptor module = LazyResolveTestUtil.resolve(getProject(), trace, Collections.singletonList(jetFile), getEnvironment());
|
||||
|
||||
LexicalScope topLevelScope = trace.get(BindingContext.LEXICAL_SCOPE, jetFile);
|
||||
final ClassifierDescriptor contextClass = ScopeUtilsKt.findClassifier(topLevelScope, Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
|
||||
assert contextClass instanceof ClassDescriptor;
|
||||
WritableScopeImpl typeParameters = new WritableScopeImpl(KtScope.Empty.INSTANCE$, module, RedeclarationHandler.THROW_EXCEPTION,
|
||||
"Type parameter scope");
|
||||
for (TypeParameterDescriptor parameterDescriptor : contextClass.getTypeConstructor().getParameters()) {
|
||||
typeParameters.addClassifierDescriptor(parameterDescriptor);
|
||||
}
|
||||
typeParameters.changeLockLevel(LexicalWritableScope.LockLevel.READING);
|
||||
return new ChainedScope(module,
|
||||
"TypeSubstitutorTest::getContextScope()",
|
||||
topLevelDeclarations,
|
||||
typeParameters,
|
||||
contextClass.getDefaultType().getMemberScope(),
|
||||
module.getBuiltIns().getBuiltInsPackageScope());
|
||||
LexicalScope typeParameters = new LexicalScopeImpl(topLevelScope, module, false, null, "Type parameter scope",
|
||||
RedeclarationHandler.THROW_EXCEPTION,
|
||||
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
|
||||
@Override
|
||||
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
|
||||
for (TypeParameterDescriptor parameterDescriptor : contextClass.getTypeConstructor().getParameters()) {
|
||||
handler.addClassifierDescriptor(parameterDescriptor);
|
||||
}
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
});
|
||||
return new LexicalChainedScope(typeParameters, module, false, null, "TypeSubstitutorTest::getContextScope()",
|
||||
new KtScope[] {
|
||||
contextClass.getDefaultType().getMemberScope(),
|
||||
module.getBuiltIns().getBuiltInsPackageScope()
|
||||
});
|
||||
}
|
||||
|
||||
private void doTest(@Nullable String expectedTypeStr, String initialTypeStr, Pair<String, String>... substitutionStrs) {
|
||||
@@ -123,7 +134,7 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
|
||||
String typeParameterName = pair.first;
|
||||
String replacementProjectionString = pair.second;
|
||||
|
||||
ClassifierDescriptor classifier = scope.getClassifier(Name.identifier(typeParameterName), NoLookupLocation.FROM_TEST);
|
||||
ClassifierDescriptor classifier = ScopeUtilsKt.findClassifier(scope, Name.identifier(typeParameterName), NoLookupLocation.FROM_TEST);
|
||||
assertNotNull("No type parameter named " + typeParameterName, classifier);
|
||||
assertTrue(typeParameterName + " is not a type parameter: " + classifier, classifier instanceof TypeParameterDescriptor);
|
||||
|
||||
@@ -140,7 +151,7 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
|
||||
KtTypeReference jetTypeReference = KtPsiFactoryKt.KtPsiFactory(getProject()).createType(typeStr);
|
||||
AnalyzingUtils.checkForSyntacticErrors(jetTypeReference);
|
||||
BindingTrace trace = new BindingTraceContext();
|
||||
KotlinType type = container.getTypeResolver().resolveType(TypeTestUtilsKt.asLexicalScope(scope), jetTypeReference, trace, true);
|
||||
KotlinType type = container.getTypeResolver().resolveType(scope, jetTypeReference, trace, true);
|
||||
if (!trace.getBindingContext().getDiagnostics().isEmpty()) {
|
||||
fail("Errors:\n" + StringUtil.join(
|
||||
trace.getBindingContext().getDiagnostics(),
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.types;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import kotlin.Unit;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
@@ -33,6 +35,7 @@ import org.jetbrains.kotlin.psi.KtTypeProjection;
|
||||
import org.jetbrains.kotlin.psi.KtTypeReference;
|
||||
import org.jetbrains.kotlin.resolve.TypeResolver;
|
||||
import org.jetbrains.kotlin.resolve.scopes.*;
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
import org.jetbrains.kotlin.test.KotlinLiteFixture;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
@@ -198,18 +201,23 @@ public class TypeUnifierTest extends KotlinLiteFixture {
|
||||
}
|
||||
|
||||
private TypeProjection makeTypeProjection(KtScope scope, String typeStr) {
|
||||
WritableScopeImpl withX =
|
||||
new WritableScopeImpl(scope, scope.getContainingDeclaration(), RedeclarationHandler.DO_NOTHING, "With X");
|
||||
withX.addClassifierDescriptor(x);
|
||||
withX.addClassifierDescriptor(y);
|
||||
withX.changeLockLevel(LexicalWritableScope.LockLevel.READING);
|
||||
LexicalScope withX = new LexicalScopeImpl(ScopeUtilsKt.memberScopeAsImportingScope(scope), scope.getContainingDeclaration(),
|
||||
false, null, "With X", RedeclarationHandler.DO_NOTHING,
|
||||
new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
|
||||
@Override
|
||||
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
|
||||
handler.addClassifierDescriptor(x);
|
||||
handler.addClassifierDescriptor(y);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
});
|
||||
|
||||
KtTypeProjection projection = KtPsiFactoryKt
|
||||
.KtPsiFactory(getProject()).createTypeArguments("<" + typeStr + ">").getArguments().get(0);
|
||||
|
||||
KtTypeReference typeReference = projection.getTypeReference();
|
||||
assert typeReference != null;
|
||||
KotlinType type = typeResolver.resolveType(TypeTestUtilsKt.asLexicalScope(withX), typeReference, KotlinTestUtils.DUMMY_TRACE, true);
|
||||
KotlinType type = typeResolver.resolveType(withX, typeReference, KotlinTestUtils.DUMMY_TRACE, true);
|
||||
|
||||
return new TypeProjectionImpl(getProjectionKind(typeStr, projection), type);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ abstract class KtScopeImpl : KtScope {
|
||||
abstract override fun printScopeStructure(p: Printer)
|
||||
}
|
||||
|
||||
class SimpleKtScope(val ownerDescriptor: DeclarationDescriptor, val debugName: String): KtScopeImpl() {
|
||||
open class SimpleKtScope(val ownerDescriptor: DeclarationDescriptor, val debugName: String): KtScopeImpl() {
|
||||
override fun printScopeStructure(p: Printer) {
|
||||
p.println(javaClass.getSimpleName(), ": ", debugName, " for ", ownerDescriptor, " {")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user