Example #1
0
 public void resolvePropertyInitializer(
     @NotNull DataFlowInfo outerDataFlowInfo,
     @NotNull JetProperty property,
     @NotNull PropertyDescriptor propertyDescriptor,
     @NotNull JetExpression initializer,
     @NotNull LexicalScope scope) {
   LexicalScope propertyDeclarationInnerScope =
       JetScopeUtils.getPropertyDeclarationInnerScopeForInitializer(
           propertyDescriptor, scope, propertyDescriptor.getTypeParameters(), null, trace);
   JetType expectedTypeForInitializer =
       property.getTypeReference() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE;
   if (propertyDescriptor.getCompileTimeInitializer() == null) {
     expressionTypingServices.getType(
         propertyDeclarationInnerScope,
         initializer,
         expectedTypeForInitializer,
         outerDataFlowInfo,
         trace);
   }
 }
Example #2
0
  private void checkPropertyInitializer(
      @NotNull JetProperty property, @NotNull PropertyDescriptor propertyDescriptor) {
    JetPropertyAccessor getter = property.getGetter();
    JetPropertyAccessor setter = property.getSetter();
    boolean hasAccessorImplementation =
        (getter != null && getter.hasBody()) || (setter != null && setter.hasBody());

    if (propertyDescriptor.getModality() == Modality.ABSTRACT) {
      if (!property.hasDelegateExpressionOrInitializer() && property.getTypeReference() == null) {
        trace.report(PROPERTY_WITH_NO_TYPE_NO_INITIALIZER.on(property));
      }
      return;
    }
    DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
    boolean inTrait =
        containingDeclaration instanceof ClassDescriptor
            && ((ClassDescriptor) containingDeclaration).getKind() == ClassKind.INTERFACE;
    JetExpression initializer = property.getInitializer();
    JetPropertyDelegate delegate = property.getDelegate();
    boolean backingFieldRequired =
        Boolean.TRUE.equals(
            trace
                .getBindingContext()
                .get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor));

    if (inTrait && backingFieldRequired && hasAccessorImplementation) {
      trace.report(BACKING_FIELD_IN_TRAIT.on(property));
    }

    if (initializer == null && delegate == null) {
      boolean error = false;
      if (backingFieldRequired
          && !inTrait
          && Boolean.TRUE.equals(
              trace.getBindingContext().get(BindingContext.IS_UNINITIALIZED, propertyDescriptor))) {
        if (!(containingDeclaration instanceof ClassDescriptor) || hasAccessorImplementation) {
          error = true;
          trace.report(MUST_BE_INITIALIZED.on(property));
        } else {
          error = true;
          trace.report(MUST_BE_INITIALIZED_OR_BE_ABSTRACT.on(property));
        }
      }
      if (!error && property.getTypeReference() == null) {
        trace.report(PROPERTY_WITH_NO_TYPE_NO_INITIALIZER.on(property));
      }
      if (inTrait && property.hasModifier(JetTokens.FINAL_KEYWORD) && backingFieldRequired) {
        trace.report(FINAL_PROPERTY_IN_TRAIT.on(property));
      }
      return;
    }

    if (inTrait) {
      if (delegate != null) {
        trace.report(DELEGATED_PROPERTY_IN_TRAIT.on(delegate));
      } else {
        trace.report(PROPERTY_INITIALIZER_IN_TRAIT.on(initializer));
      }
    } else if (delegate == null) {
      if (!backingFieldRequired) {
        trace.report(PROPERTY_INITIALIZER_NO_BACKING_FIELD.on(initializer));
      } else if (property.getReceiverTypeReference() != null) {
        trace.report(EXTENSION_PROPERTY_WITH_BACKING_FIELD.on(initializer));
      }
    }
  }