예제 #1
0
 @NotNull
 private static FqNameUnsafe getFqNameUnsafe(@NotNull DeclarationDescriptor descriptor) {
   DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
   assert containingDeclaration != null
       : "Not package/module descriptor doesn't have containing declaration: " + descriptor;
   return getFqName(containingDeclaration).child(descriptor.getName());
 }
예제 #2
0
 @NotNull
 public static FqName getFqNameFromTopLevelClass(@NotNull DeclarationDescriptor descriptor) {
   DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
   Name name = descriptor.getName();
   if (!(containingDeclaration instanceof ClassDescriptor)) {
     return FqName.topLevel(name);
   }
   return getFqNameFromTopLevelClass(containingDeclaration).child(name);
 }
예제 #3
0
 /** Descriptor may be local itself or have a local ancestor */
 public static boolean isLocal(@NotNull DeclarationDescriptor descriptor) {
   DeclarationDescriptor current = descriptor;
   while (current != null) {
     if (isAnonymousObject(current) || isDescriptorWithLocalVisibility(current)) {
       return true;
     }
     current = current.getContainingDeclaration();
   }
   return false;
 }
예제 #4
0
 @Nullable
 public static ClassDescriptor getContainingClass(@NotNull DeclarationDescriptor descriptor) {
   DeclarationDescriptor containing = descriptor.getContainingDeclaration();
   while (containing != null) {
     if (containing instanceof ClassDescriptor && !isCompanionObject(containing)) {
       return (ClassDescriptor) containing;
     }
     containing = containing.getContainingDeclaration();
   }
   return null;
 }
예제 #5
0
 public static boolean isAncestor(
     @Nullable DeclarationDescriptor ancestor,
     @NotNull DeclarationDescriptor declarationDescriptor,
     boolean strict) {
   if (ancestor == null) return false;
   DeclarationDescriptor descriptor =
       strict ? declarationDescriptor.getContainingDeclaration() : declarationDescriptor;
   while (descriptor != null) {
     if (ancestor == descriptor) return true;
     descriptor = descriptor.getContainingDeclaration();
   }
   return false;
 }
예제 #6
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;
 }
예제 #7
0
 @Nullable
 @SuppressWarnings("unchecked")
 public static <D extends DeclarationDescriptor> D getParentOfType(
     @Nullable DeclarationDescriptor descriptor, @NotNull Class<D> aClass, boolean strict) {
   if (descriptor == null) return null;
   if (strict) {
     descriptor = descriptor.getContainingDeclaration();
   }
   while (descriptor != null) {
     if (aClass.isInstance(descriptor)) {
       return (D) descriptor;
     }
     descriptor = descriptor.getContainingDeclaration();
   }
   return null;
 }
예제 #8
0
  private static boolean containsAnnotation(
      DeclarationDescriptor descriptor, FqName annotationClassFqName) {
    DeclarationDescriptor original = descriptor.getOriginal();
    Annotations annotations = original.getAnnotations();

    if (annotations.findAnnotation(annotationClassFqName) != null) return true;

    AnnotationUseSiteTarget associatedUseSiteTarget =
        AnnotationUseSiteTarget.Companion.getAssociatedUseSiteTarget(descriptor);
    if (associatedUseSiteTarget != null) {
      if (Annotations.Companion.findUseSiteTargetedAnnotation(
              annotations, associatedUseSiteTarget, annotationClassFqName)
          != null) {
        return true;
      }
    }

    return false;
  }
예제 #9
0
 @Nullable
 public static ModuleDescriptor getContainingModuleOrNull(
     @NotNull DeclarationDescriptor descriptor) {
   while (descriptor != null) {
     if (descriptor instanceof ModuleDescriptor) {
       return (ModuleDescriptor) descriptor;
     }
     if (descriptor instanceof PackageViewDescriptor) {
       return ((PackageViewDescriptor) descriptor).getModule();
     }
     //noinspection ConstantConditions
     descriptor = descriptor.getContainingDeclaration();
   }
   return null;
 }
예제 #10
0
 public boolean isMemberOfAny(@NotNull DeclarationDescriptor descriptor) {
   return descriptor.getContainingDeclaration() == getAny();
 }
예제 #11
0
 /**
  * @return true if descriptor is a class inside another class and does not have access to the
  *     outer class
  */
 public static boolean isStaticNestedClass(@NotNull DeclarationDescriptor descriptor) {
   DeclarationDescriptor containing = descriptor.getContainingDeclaration();
   return descriptor instanceof ClassDescriptor
       && containing instanceof ClassDescriptor
       && !((ClassDescriptor) descriptor).isInner();
 }
예제 #12
0
 public static boolean isAnonymousObject(@NotNull DeclarationDescriptor descriptor) {
   return isClass(descriptor) && descriptor.getName().equals(SpecialNames.NO_NAME_PROVIDED);
 }
예제 #13
0
 public static boolean isTopLevelDeclaration(@Nullable DeclarationDescriptor descriptor) {
   return descriptor != null
       && descriptor.getContainingDeclaration() instanceof PackageFragmentDescriptor;
 }
예제 #14
0
 static boolean containsAnnotation(
     DeclarationDescriptor descriptor, FqName annotationClassFqName) {
   return descriptor.getOriginal().getAnnotations().findAnnotation(annotationClassFqName) != null;
 }
예제 #15
0
  private void checkSupertypeList(
      @NotNull ClassDescriptor supertypeOwner,
      @NotNull Map<JetTypeReference, JetType> supertypes,
      @NotNull JetClassOrObject jetClass) {
    Set<TypeConstructor> allowedFinalSupertypes =
        getAllowedFinalSupertypes(supertypeOwner, jetClass);
    Set<TypeConstructor> typeConstructors = Sets.newHashSet();
    boolean classAppeared = false;
    for (Map.Entry<JetTypeReference, JetType> entry : supertypes.entrySet()) {
      JetTypeReference typeReference = entry.getKey();
      JetType supertype = entry.getValue();

      boolean addSupertype = true;

      ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
      if (classDescriptor != null) {
        if (ErrorUtils.isError(classDescriptor)) continue;

        if (classDescriptor.getKind() != ClassKind.INTERFACE) {
          if (supertypeOwner.getKind() == ClassKind.ENUM_CLASS) {
            trace.report(CLASS_IN_SUPERTYPE_FOR_ENUM.on(typeReference));
            addSupertype = false;
          } else if (supertypeOwner.getKind() == ClassKind.INTERFACE
              && !classAppeared
              && !TypesPackage.isDynamic(supertype) /* avoid duplicate diagnostics */) {
            trace.report(TRAIT_WITH_SUPERCLASS.on(typeReference));
            addSupertype = false;
          }

          if (classAppeared) {
            trace.report(MANY_CLASSES_IN_SUPERTYPE_LIST.on(typeReference));
          } else {
            classAppeared = true;
          }
        }
      } else {
        trace.report(SUPERTYPE_NOT_A_CLASS_OR_TRAIT.on(typeReference));
      }

      TypeConstructor constructor = supertype.getConstructor();
      if (addSupertype && !typeConstructors.add(constructor)) {
        trace.report(SUPERTYPE_APPEARS_TWICE.on(typeReference));
      }

      if (DescriptorUtils.isSingleton(classDescriptor)) {
        trace.report(SINGLETON_IN_SUPERTYPE.on(typeReference));
      } else if (constructor.isFinal() && !allowedFinalSupertypes.contains(constructor)) {
        if (classDescriptor.getModality() == Modality.SEALED) {
          DeclarationDescriptor containingDescriptor = supertypeOwner.getContainingDeclaration();
          while (containingDescriptor != null && containingDescriptor != classDescriptor) {
            containingDescriptor = containingDescriptor.getContainingDeclaration();
          }
          if (containingDescriptor == null) {
            trace.report(SEALED_SUPERTYPE.on(typeReference));
          } else {
            trace.report(SEALED_SUPERTYPE_IN_LOCAL_CLASS.on(typeReference));
          }
        } else {
          trace.report(FINAL_SUPERTYPE.on(typeReference));
        }
      }
    }
  }