Beispiel #1
0
 @Nullable
 public static TypeParameterDescriptor getTypeParameterDescriptorOrNull(@NotNull KotlinType type) {
   if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) {
     return (TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor();
   }
   return null;
 }
Beispiel #2
0
 private static boolean lowerThanBound(
     KotlinTypeChecker typeChecker,
     KotlinType argument,
     TypeParameterDescriptor parameterDescriptor) {
   for (KotlinType bound : parameterDescriptor.getUpperBounds()) {
     if (typeChecker.isSubtypeOf(argument, bound)) {
       if (!argument.getConstructor().equals(bound.getConstructor())) {
         return true;
       }
     }
   }
   return false;
 }
Beispiel #3
0
 @Nullable
 public static ClassDescriptor getClassDescriptor(@NotNull KotlinType type) {
   DeclarationDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor();
   if (declarationDescriptor instanceof ClassDescriptor) {
     return (ClassDescriptor) declarationDescriptor;
   }
   return null;
 }
Beispiel #4
0
  public static boolean isExtensionFunctionType(@NotNull KotlinType type) {
    if (isExactExtensionFunctionType(type)) return true;

    for (KotlinType superType : type.getConstructor().getSupertypes()) {
      if (isExtensionFunctionType(superType)) return true;
    }

    return false;
  }
Beispiel #5
0
 public static boolean isSubtypeOfClass(
     @NotNull KotlinType type, @NotNull DeclarationDescriptor superClass) {
   if (isSameClass(type, superClass)) return true;
   for (KotlinType superType : type.getConstructor().getSupertypes()) {
     if (isSubtypeOfClass(superType, superClass)) {
       return true;
     }
   }
   return false;
 }
Beispiel #6
0
 private static boolean isConstructedFromGivenClass(
     @NotNull KotlinType type, @NotNull FqNameUnsafe fqName) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return descriptor != null
       &&
       /* quick check to avoid creation of full FqName instance */ descriptor
           .getName()
           .equals(fqName.shortName())
       && fqName.equals(getFqName(descriptor));
 }
Beispiel #7
0
 public static boolean dependsOnTypeConstructors(
     @NotNull KotlinType type, @NotNull Collection<TypeConstructor> typeParameterConstructors) {
   if (typeParameterConstructors.contains(type.getConstructor())) return true;
   for (TypeProjection typeProjection : type.getArguments()) {
     if (!typeProjection.isStarProjection()
         && dependsOnTypeConstructors(typeProjection.getType(), typeParameterConstructors)) {
       return true;
     }
   }
   return false;
 }
Beispiel #8
0
  public static boolean hasNullableSuperType(@NotNull KotlinType type) {
    if (type.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) {
      // A class/trait cannot have a nullable supertype
      return false;
    }

    for (KotlinType supertype : getImmediateSupertypes(type)) {
      if (supertype.isMarkedNullable()) return true;
      if (hasNullableSuperType(supertype)) return true;
    }

    return false;
  }
Beispiel #9
0
 @NotNull
 public static List<KotlinType> getImmediateSupertypes(@NotNull KotlinType type) {
   TypeSubstitutor substitutor = TypeSubstitutor.create(type);
   Collection<KotlinType> originalSupertypes = type.getConstructor().getSupertypes();
   List<KotlinType> result = new ArrayList<KotlinType>(originalSupertypes.size());
   for (KotlinType supertype : originalSupertypes) {
     KotlinType substitutedType = createSubstitutedSupertype(type, supertype, substitutor);
     if (substitutedType != null) {
       result.add(substitutedType);
     }
   }
   return result;
 }
Beispiel #10
0
 private static boolean isSameClass(
     @NotNull KotlinType type, @NotNull DeclarationDescriptor other) {
   DeclarationDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   if (descriptor != null) {
     DeclarationDescriptor originalDescriptor = descriptor.getOriginal();
     if (originalDescriptor instanceof ClassifierDescriptor
         && other instanceof ClassifierDescriptor
         && ((ClassifierDescriptor) other)
             .getTypeConstructor()
             .equals(((ClassifierDescriptor) originalDescriptor).getTypeConstructor())) {
       return true;
     }
   }
   return false;
 }
Beispiel #11
0
 public static boolean isPrimitiveType(@NotNull KotlinType type) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return !type.isMarkedNullable()
       && descriptor instanceof ClassDescriptor
       && isPrimitiveClass((ClassDescriptor) descriptor);
 }
Beispiel #12
0
 public static boolean isPrimitiveArray(@NotNull KotlinType type) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return descriptor != null && getPrimitiveTypeByArrayClassFqName(getFqName(descriptor)) != null;
 }
Beispiel #13
0
 @NotNull
 public static ClassDescriptor getClassDescriptorForType(@NotNull KotlinType type) {
   return getClassDescriptorForTypeConstructor(type.getConstructor());
 }
Beispiel #14
0
  public static boolean canHaveSubtypes(KotlinTypeChecker typeChecker, @NotNull KotlinType type) {
    if (type.isMarkedNullable()) {
      return true;
    }
    if (!type.getConstructor().isFinal()) {
      return true;
    }

    List<TypeParameterDescriptor> parameters = type.getConstructor().getParameters();
    List<TypeProjection> arguments = type.getArguments();
    for (int i = 0, parametersSize = parameters.size(); i < parametersSize; i++) {
      TypeParameterDescriptor parameterDescriptor = parameters.get(i);
      TypeProjection typeProjection = arguments.get(i);
      if (typeProjection.isStarProjection()) return true;

      Variance projectionKind = typeProjection.getProjectionKind();
      KotlinType argument = typeProjection.getType();

      switch (parameterDescriptor.getVariance()) {
        case INVARIANT:
          switch (projectionKind) {
            case INVARIANT:
              if (lowerThanBound(typeChecker, argument, parameterDescriptor)
                  || canHaveSubtypes(typeChecker, argument)) {
                return true;
              }
              break;
            case IN_VARIANCE:
              if (lowerThanBound(typeChecker, argument, parameterDescriptor)) {
                return true;
              }
              break;
            case OUT_VARIANCE:
              if (canHaveSubtypes(typeChecker, argument)) {
                return true;
              }
              break;
          }
          break;
        case IN_VARIANCE:
          if (projectionKind != Variance.OUT_VARIANCE) {
            if (lowerThanBound(typeChecker, argument, parameterDescriptor)) {
              return true;
            }
          } else {
            if (canHaveSubtypes(typeChecker, argument)) {
              return true;
            }
          }
          break;
        case OUT_VARIANCE:
          if (projectionKind != Variance.IN_VARIANCE) {
            if (canHaveSubtypes(typeChecker, argument)) {
              return true;
            }
          } else {
            if (lowerThanBound(typeChecker, argument, parameterDescriptor)) {
              return true;
            }
          }
          break;
      }
    }
    return false;
  }
Beispiel #15
0
 public static boolean isExactFunctionOrExtensionFunctionType(@NotNull KotlinType type) {
   ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
   return descriptor != null && isNumberedFunctionClassFqName(getFqName(descriptor));
 }
Beispiel #16
0
 @Override
 @NotNull
 public TypeConstructor getConstructor() {
   return delegate.getConstructor();
 }
Beispiel #17
0
 public static boolean isDontCarePlaceholder(@Nullable KotlinType type) {
   return type != null && type.getConstructor() == DONT_CARE.getConstructor();
 }