Esempio n. 1
0
 private static boolean isIllegalNestedClass(@NotNull DeclarationDescriptor descriptor) {
   if (!(descriptor instanceof ClassDescriptor)) return false;
   DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
   if (!(containingDeclaration instanceof ClassDescriptor)) return false;
   ClassDescriptor containingClass = (ClassDescriptor) containingDeclaration;
   return containingClass.isInner()
       || containingClass.getContainingDeclaration() instanceof FunctionDescriptor;
 }
Esempio n. 2
0
 // Returns a set of enum or sealed types of which supertypeOwner is an entry or a member
 @NotNull
 private static Set<TypeConstructor> getAllowedFinalSupertypes(
     @NotNull ClassDescriptor descriptor, @NotNull JetClassOrObject jetClass) {
   Set<TypeConstructor> parentEnumOrSealed;
   if (jetClass instanceof JetEnumEntry) {
     parentEnumOrSealed =
         Collections.singleton(
             ((ClassDescriptor) descriptor.getContainingDeclaration()).getTypeConstructor());
   } else {
     parentEnumOrSealed = Collections.emptySet();
     ClassDescriptor currentDescriptor = descriptor;
     while (currentDescriptor.getContainingDeclaration() instanceof ClassDescriptor) {
       currentDescriptor = (ClassDescriptor) currentDescriptor.getContainingDeclaration();
       if (currentDescriptor.getModality() == Modality.SEALED) {
         if (parentEnumOrSealed.isEmpty()) {
           parentEnumOrSealed = new HashSet<TypeConstructor>();
         }
         parentEnumOrSealed.add(currentDescriptor.getTypeConstructor());
       }
     }
   }
   return parentEnumOrSealed;
 }
Esempio n. 3
0
  private void checkEnumEntry(
      @NotNull JetEnumEntry enumEntry, @NotNull ClassDescriptor classDescriptor) {
    DeclarationDescriptor declaration = classDescriptor.getContainingDeclaration();
    assert DescriptorUtils.isEnumClass(declaration)
        : "Enum entry should be declared in enum class: " + classDescriptor;
    ClassDescriptor enumClass = (ClassDescriptor) declaration;

    if (enumEntryUsesDeprecatedSuperConstructor(enumEntry)) {
      trace.report(
          Errors.ENUM_ENTRY_USES_DEPRECATED_SUPER_CONSTRUCTOR.on(enumEntry, classDescriptor));
    }
    String neededDelimiter = enumEntryExpectedDelimiter(enumEntry);
    if (!neededDelimiter.isEmpty()) {
      trace.report(
          Errors.ENUM_ENTRY_USES_DEPRECATED_OR_NO_DELIMITER.on(
              enumEntry, classDescriptor, neededDelimiter));
    }
    if (enumEntryAfterEnumMember(enumEntry)) {
      trace.report(Errors.ENUM_ENTRY_AFTER_ENUM_MEMBER.on(enumEntry, classDescriptor));
    }

    List<JetDelegationSpecifier> delegationSpecifiers = enumEntry.getDelegationSpecifiers();
    ConstructorDescriptor constructor = enumClass.getUnsubstitutedPrimaryConstructor();
    if ((constructor == null || !constructor.getValueParameters().isEmpty())
        && delegationSpecifiers.isEmpty()) {
      trace.report(ENUM_ENTRY_SHOULD_BE_INITIALIZED.on(enumEntry, enumClass));
    }

    for (JetDelegationSpecifier delegationSpecifier : delegationSpecifiers) {
      JetTypeReference typeReference = delegationSpecifier.getTypeReference();
      if (typeReference != null) {
        JetType type = trace.getBindingContext().get(TYPE, typeReference);
        if (type != null) {
          JetType enumType = enumClass.getDefaultType();
          if (!type.getConstructor().equals(enumType.getConstructor())) {
            trace.report(ENUM_ENTRY_ILLEGAL_TYPE.on(typeReference, enumClass));
          }
        }
      }
    }
  }
Esempio n. 4
0
  @NotNull
  private static InnerModifierCheckResult checkIllegalInner(
      @NotNull DeclarationDescriptor descriptor) {
    if (!(descriptor instanceof ClassDescriptor)) return InnerModifierCheckResult.ILLEGAL_POSITION;
    ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;

    if (classDescriptor.getKind() != ClassKind.CLASS)
      return InnerModifierCheckResult.ILLEGAL_POSITION;

    DeclarationDescriptor containingDeclaration = classDescriptor.getContainingDeclaration();
    if (!(containingDeclaration instanceof ClassDescriptor))
      return InnerModifierCheckResult.ILLEGAL_POSITION;

    if (DescriptorUtils.isTrait(containingDeclaration)) {
      return InnerModifierCheckResult.IN_TRAIT;
    } else if (DescriptorUtils.isObject(containingDeclaration)) {
      return InnerModifierCheckResult.IN_OBJECT;
    } else {
      return InnerModifierCheckResult.ALLOWED;
    }
  }
Esempio n. 5
0
 /** @return true iff {@code descriptor}'s first non-class container is a package */
 public static boolean isTopLevelOrInnerClass(@NotNull ClassDescriptor descriptor) {
   DeclarationDescriptor containing = descriptor.getContainingDeclaration();
   return isTopLevelDeclaration(descriptor)
       || containing instanceof ClassDescriptor
           && isTopLevelOrInnerClass((ClassDescriptor) containing);
 }
Esempio n. 6
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));
        }
      }
    }
  }