Search in sources :

Example 6 with ClassifierDescriptor

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

the class MoveKotlinNestedClassesToUpperLevelDialog method getTargetContainerWithValidation.

@Nullable
private PsiElement getTargetContainerWithValidation() throws ConfigurationException {
    String className = getClassName();
    String parameterName = getParameterName();
    if (className != null && className.isEmpty())
        throw new ConfigurationException(RefactoringBundle.message("no.class.name.specified"));
    if (!KotlinNameSuggester.INSTANCE.isIdentifier(className))
        throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(className));
    if (passOuterClassCheckBox.isSelected()) {
        if (parameterName != null && parameterName.isEmpty())
            throw new ConfigurationException(RefactoringBundle.message("no.parameter.name.specified"));
        if (!KotlinNameSuggester.INSTANCE.isIdentifier(parameterName))
            throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(parameterName));
    }
    PsiElement targetContainer = getTargetContainer();
    if (targetContainer instanceof KtClassOrObject) {
        KtClassOrObject targetClass = (KtClassOrObject) targetContainer;
        for (KtDeclaration member : targetClass.getDeclarations()) {
            if (member instanceof KtClassOrObject && className != null && className.equals(member.getName())) {
                throw new ConfigurationException(RefactoringBundle.message("inner.class.exists", className, targetClass.getName()));
            }
        }
    }
    if (targetContainer instanceof PsiDirectory || targetContainer instanceof KtFile) {
        FqName targetPackageFqName = getTargetPackageFqName();
        if (targetPackageFqName == null)
            throw new ConfigurationException("No package corresponds to this directory");
        //noinspection ConstantConditions
        ClassifierDescriptor existingClass = DescriptorUtils.getContainingModule(innerClassDescriptor).getPackage(targetPackageFqName).getMemberScope().getContributedClassifier(Name.identifier(className), NoLookupLocation.FROM_IDE);
        if (existingClass != null)
            throw new ConfigurationException("Class " + className + " already exists in package " + targetPackageFqName);
        PsiDirectory targetDir = targetContainer instanceof PsiDirectory ? (PsiDirectory) targetContainer : targetContainer.getContainingFile().getContainingDirectory();
        String message = RefactoringMessageUtil.checkCanCreateFile(targetDir, className + ".kt");
        if (message != null)
            throw new ConfigurationException(message);
    }
    return targetContainer;
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) FqName(org.jetbrains.kotlin.name.FqName) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with ClassifierDescriptor

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

the class TypeReconstructionUtil method allStarProjectionsString.

@NotNull
private static String allStarProjectionsString(@NotNull TypeConstructor constructor) {
    int size = constructor.getParameters().size();
    assert size != 0 : "No projections possible for a nilary type constructor" + constructor;
    ClassifierDescriptor declarationDescriptor = constructor.getDeclarationDescriptor();
    assert declarationDescriptor != null : "No declaration descriptor for type constructor " + constructor;
    String name = declarationDescriptor.getName().asString();
    return getTypeNameAndStarProjectionsString(name, size);
}
Also used : ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ClassifierDescriptor

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

the class ReifiedTypeParameterSubstitutionChecker method check.

@Override
public void check(@NotNull ResolvedCall<?> resolvedCall, @NotNull PsiElement reportOn, @NotNull CallCheckerContext context) {
    Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
    for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
        TypeParameterDescriptor parameter = entry.getKey();
        KotlinType argument = entry.getValue();
        ClassifierDescriptor argumentDeclarationDescriptor = argument.getConstructor().getDeclarationDescriptor();
        if (!parameter.isReified() && !isTypeParameterOfKotlinArray(parameter)) {
            continue;
        }
        KtTypeProjection typeProjection = CollectionsKt.getOrNull(resolvedCall.getCall().getTypeArguments(), parameter.getIndex());
        PsiElement reportErrorOn = typeProjection != null ? typeProjection : reportOn;
        if (argumentDeclarationDescriptor instanceof TypeParameterDescriptor && !((TypeParameterDescriptor) argumentDeclarationDescriptor).isReified()) {
            context.getTrace().report(Errors.TYPE_PARAMETER_AS_REIFIED.on(reportErrorOn, (TypeParameterDescriptor) argumentDeclarationDescriptor));
        } else if (TypeUtilsKt.cannotBeReified(argument)) {
            context.getTrace().report(Errors.REIFIED_TYPE_FORBIDDEN_SUBSTITUTION.on(reportErrorOn, argument));
        }
    // REIFIED_TYPE_UNSAFE_SUBSTITUTION is temporary disabled because it seems too strict now (see KT-10847)
    //else if (TypeUtilsKt.unsafeAsReifiedArgument(argument) && !hasPureReifiableAnnotation(parameter)) {
    //    context.getTrace().report(Errors.REIFIED_TYPE_UNSAFE_SUBSTITUTION.on(reportErrorOn, argument));
    //}
    }
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) KtTypeProjection(org.jetbrains.kotlin.psi.KtTypeProjection) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement)

Example 9 with ClassifierDescriptor

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

the class ResolveSessionUtils method findClassByRelativePath.

@Nullable
public static ClassDescriptor findClassByRelativePath(@NotNull MemberScope packageScope, @NotNull FqName path) {
    if (path.isRoot())
        return null;
    MemberScope scope = packageScope;
    ClassifierDescriptor classifier = null;
    for (Name name : path.pathSegments()) {
        classifier = scope.getContributedClassifier(name, NoLookupLocation.WHEN_FIND_BY_FQNAME);
        if (!(classifier instanceof ClassDescriptor))
            return null;
        scope = ((ClassDescriptor) classifier).getUnsubstitutedInnerClassesScope();
    }
    return (ClassDescriptor) classifier;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)9 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)5 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)4 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 PsiElement (com.intellij.psi.PsiElement)1 File (java.io.File)1 Map (java.util.Map)1 Unit (kotlin.Unit)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)1 FqName (org.jetbrains.kotlin.name.FqName)1 FqNameUnsafe (org.jetbrains.kotlin.name.FqNameUnsafe)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 KtTypeProjection (org.jetbrains.kotlin.psi.KtTypeProjection)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1