Search in sources :

Example 6 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class CommonSupertypes method computeSupertypeProjections.

// constructor - type constructor of a supertype to be instantiated
// types - instantiations of constructor occurring as supertypes of classes we are trying to intersect
@NotNull
private static SimpleType computeSupertypeProjections(@NotNull TypeConstructor constructor, @NotNull Set<SimpleType> types, int recursionDepth, int maxDepth) {
    assert !types.isEmpty();
    if (types.size() == 1) {
        return types.iterator().next();
    }
    List<TypeParameterDescriptor> parameters = constructor.getParameters();
    List<TypeProjection> newProjections = new ArrayList<TypeProjection>(parameters.size());
    for (TypeParameterDescriptor parameterDescriptor : parameters) {
        Set<TypeProjection> typeProjections = new HashSet<TypeProjection>();
        for (KotlinType type : types) {
            typeProjections.add(type.getArguments().get(parameterDescriptor.getIndex()));
        }
        newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth));
    }
    boolean nullable = false;
    for (KotlinType type : types) {
        nullable |= type.isMarkedNullable();
    }
    ClassifierDescriptor classifier = constructor.getDeclarationDescriptor();
    MemberScope newScope;
    if (classifier instanceof ClassDescriptor) {
        newScope = ((ClassDescriptor) classifier).getMemberScope(newProjections);
    } else if (classifier instanceof TypeParameterDescriptor) {
        newScope = classifier.getDefaultType().getMemberScope();
    } else {
        newScope = ErrorUtils.createErrorScope("A scope for common supertype which is not a normal classifier", true);
    }
    return KotlinTypeFactory.simpleType(Annotations.Companion.getEMPTY(), constructor, newProjections, nullable, newScope);
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class FieldInfo method createForSingleton.

@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull KotlinTypeMapper typeMapper) {
    if (!classDescriptor.getKind().isSingleton() || DescriptorUtils.isEnumEntry(classDescriptor)) {
        throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
    }
    if (isNonCompanionObject(classDescriptor) || CompanionObjectMapping.INSTANCE.isMappedIntrinsicCompanionObject(classDescriptor)) {
        return createSingletonViaInstance(classDescriptor, typeMapper);
    }
    ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
    assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
    Type ownerType = typeMapper.mapType(ownerDescriptor);
    return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class BuiltInsReferenceResolverTest method getAllStandardDescriptors.

private static Collection<DeclarationDescriptor> getAllStandardDescriptors() {
    final List<DeclarationDescriptor> descriptors = new ArrayList<DeclarationDescriptor>();
    PackageFragmentDescriptor builtinsPackageFragment = DefaultBuiltIns.getInstance().getBuiltInsPackageFragment();
    for (DeclarationDescriptor packageMember : DescriptorUtils.getAllDescriptors(builtinsPackageFragment.getMemberScope())) {
        packageMember.acceptVoid(new DeclarationDescriptorVisitorEmptyBodies<Void, Void>() {

            @Override
            public Void visitClassDescriptor(ClassDescriptor descriptor, Void data) {
                descriptors.add(descriptor);
                for (DeclarationDescriptor classMember : DescriptorUtils.getAllDescriptors(descriptor.getDefaultType().getMemberScope())) {
                    classMember.acceptVoid(this);
                }
                return null;
            }

            @Override
            public Void visitDeclarationDescriptor(DeclarationDescriptor descriptor, Void data) {
                descriptors.add(descriptor);
                return null;
            }
        });
    }
    return descriptors;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) ArrayList(java.util.ArrayList) PackageFragmentDescriptor(org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor)

Example 9 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class TypeSubstitutorTest method getContextScope.

private LexicalScope getContextScope() throws IOException {
    // todo comments
    String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
    KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
    AnalysisResult analysisResult = JvmResolveUtil.analyze(ktFile, getEnvironment());
    ModuleDescriptor module = analysisResult.getModuleDescriptor();
    LexicalScope topLevelScope = analysisResult.getBindingContext().get(BindingContext.LEXICAL_SCOPE, ktFile);
    final ClassifierDescriptor contextClass = ScopeUtilsKt.findClassifier(topLevelScope, Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
    assert contextClass instanceof ClassDescriptor;
    LocalRedeclarationChecker redeclarationChecker = new ThrowingLocalRedeclarationChecker(new OverloadChecker(TypeSpecificityComparator.NONE.INSTANCE));
    LexicalScope typeParameters = new LexicalScopeImpl(topLevelScope, module, false, null, LexicalScopeKind.SYNTHETIC, redeclarationChecker, 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, LexicalScopeKind.SYNTHETIC, Arrays.asList(contextClass.getDefaultType().getMemberScope(), module.getBuiltIns().getBuiltInsPackageScope()));
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) Unit(kotlin.Unit) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) KtFile(org.jetbrains.kotlin.psi.KtFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 10 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class ResolveDescriptorsFromExternalLibraries method parseLibraryFileChunk.

private static boolean parseLibraryFileChunk(File jar, String libDescription, ZipInputStream zip, int classesPerChunk) throws IOException {
    Disposable junk = new Disposable() {

        @Override
        public void dispose() {
        }
    };
    KotlinCoreEnvironment environment;
    if (jar != null) {
        environment = KotlinCoreEnvironment.createForTests(junk, KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), jar), EnvironmentConfigFiles.JVM_CONFIG_FILES);
    } else {
        CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.FULL_JDK);
        environment = KotlinCoreEnvironment.createForTests(junk, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
        if (!findRtJar().equals(jar)) {
            throw new RuntimeException("rt.jar mismatch: " + jar + ", " + findRtJar());
        }
    }
    ModuleDescriptor module = JvmResolveUtil.analyze(environment).getModuleDescriptor();
    boolean hasErrors;
    try {
        hasErrors = false;
        for (int count = 0; count < classesPerChunk; ) {
            ZipEntry entry = zip.getNextEntry();
            if (entry == null) {
                break;
            }
            if (count == 0) {
                System.err.println("chunk from " + entry.getName());
            }
            System.err.println(entry.getName());
            String entryName = entry.getName();
            if (!entryName.endsWith(".class")) {
                continue;
            }
            if (entryName.matches("(.*/|)package-info\\.class")) {
                continue;
            }
            if (entryName.contains("$")) {
                continue;
            }
            String className = entryName.substring(0, entryName.length() - ".class".length()).replace("/", ".");
            try {
                ClassDescriptor clazz = DescriptorUtilsKt.resolveTopLevelClass(module, new FqName(className), NoLookupLocation.FROM_TEST);
                if (clazz == null) {
                    throw new IllegalStateException("class not found by name " + className + " in " + libDescription);
                }
                DescriptorUtils.getAllDescriptors(clazz.getDefaultType().getMemberScope());
            } catch (Exception e) {
                System.err.println("failed to resolve " + className);
                e.printStackTrace();
                //throw new RuntimeException("failed to resolve " + className + ": " + e, e);
                hasErrors = true;
            }
            ++count;
        }
    } finally {
        Disposer.dispose(junk);
    }
    return hasErrors;
}
Also used : Disposable(com.intellij.openapi.Disposable) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) FqName(org.jetbrains.kotlin.name.FqName) ZipEntry(java.util.zip.ZipEntry) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) CompilerConfiguration(org.jetbrains.kotlin.config.CompilerConfiguration)

Aggregations

ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)17 NotNull (org.jetbrains.annotations.NotNull)5 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)5 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)3 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)3 ArrayList (java.util.ArrayList)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)2 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)2 KtClass (org.jetbrains.kotlin.psi.KtClass)2 KtDeclaration (org.jetbrains.kotlin.psi.KtDeclaration)2 KtFile (org.jetbrains.kotlin.psi.KtFile)2 Type (org.jetbrains.org.objectweb.asm.Type)2 Disposable (com.intellij.openapi.Disposable)1 PsiMethod (com.intellij.psi.PsiMethod)1 File (java.io.File)1 ZipEntry (java.util.zip.ZipEntry)1 TestRunner (junit.textui.TestRunner)1 Unit (kotlin.Unit)1 Nullable (org.jetbrains.annotations.Nullable)1