private RemovePartsFromPropertyFix(@NotNull JetProperty element) {
   this(
       element,
       element.getInitializer() != null,
       element.getGetter() != null && element.getGetter().getBodyExpression() != null,
       element.getSetter() != null && element.getSetter().getBodyExpression() != null);
 }
  private static void checkFieldAnnotation(
      @NotNull JetProperty altProperty, @NotNull JavaFieldImpl field, boolean isVar) {
    if (!ComparatorUtil.equalsNullable(field.getName().asString(), altProperty.getName())) {
      throw new AlternativeSignatureMismatchException(
          "Field name mismatch, original: %s, alternative: %s",
          field.getName().asString(), altProperty.getName());
    }

    if (altProperty.getTypeRef() == null) {
      throw new AlternativeSignatureMismatchException(
          "Field annotation for shouldn't have type reference");
    }

    if (altProperty.getGetter() != null || altProperty.getSetter() != null) {
      throw new AlternativeSignatureMismatchException(
          "Field annotation for shouldn't have getters and setters");
    }

    if (altProperty.isVar() != isVar) {
      throw new AlternativeSignatureMismatchException("Wrong mutability in annotation for field");
    }

    if (altProperty.getInitializer() != null) {
      throw new AlternativeSignatureMismatchException(
          "Default value is not expected in annotation for field");
    }
  }
  @Override
  public void invoke(@NotNull Project project, Editor editor, JetFile file)
      throws IncorrectOperationException {
    JetType type = QuickFixUtil.getDeclarationReturnType(element);
    JetProperty newElement = (JetProperty) element.copy();
    JetPropertyAccessor getter = newElement.getGetter();
    if (removeGetter && getter != null) {
      newElement.deleteChildInternal(getter.getNode());
    }
    JetPropertyAccessor setter = newElement.getSetter();
    if (removeSetter && setter != null) {
      newElement.deleteChildInternal(setter.getNode());
    }
    JetExpression initializer = newElement.getInitializer();
    JetType typeToAdd = null;
    if (removeInitializer && initializer != null) {
      PsiElement nameIdentifier = newElement.getNameIdentifier();
      assert nameIdentifier != null;
      PsiElement nextSibling = nameIdentifier.getNextSibling();
      assert nextSibling != null;
      newElement.deleteChildRange(nextSibling, initializer);

      if (newElement.getTypeRef() == null && type != null) {
        typeToAdd = type;
      }
    }
    element = (JetProperty) element.replace(newElement);
    if (typeToAdd != null) {
      SpecifyTypeExplicitlyAction.addTypeAnnotation(project, editor, element, typeToAdd);
    }
  }
  @Override
  public JetTypeInfo visitProperty(JetProperty property, ExpressionTypingContext context) {
    JetTypeReference receiverTypeRef = property.getReceiverTypeRef();
    if (receiverTypeRef != null) {
      context.trace.report(LOCAL_EXTENSION_PROPERTY.on(receiverTypeRef));
    }

    JetPropertyAccessor getter = property.getGetter();
    if (getter != null) {
      context.trace.report(LOCAL_VARIABLE_WITH_GETTER.on(getter));
    }

    JetPropertyAccessor setter = property.getSetter();
    if (setter != null) {
      context.trace.report(LOCAL_VARIABLE_WITH_SETTER.on(setter));
    }

    VariableDescriptor propertyDescriptor =
        context
            .expressionTypingServices
            .getDescriptorResolver()
            .resolveLocalVariableDescriptor(
                scope.getContainingDeclaration(),
                scope,
                property,
                context.dataFlowInfo,
                context.trace);
    JetExpression initializer = property.getInitializer();
    DataFlowInfo dataFlowInfo = context.dataFlowInfo;
    if (initializer != null) {
      JetType outType = propertyDescriptor.getType();
      JetTypeInfo typeInfo =
          facade.getTypeInfo(initializer, context.replaceExpectedType(outType).replaceScope(scope));
      dataFlowInfo = typeInfo.getDataFlowInfo();
    }

    {
      VariableDescriptor olderVariable = scope.getLocalVariable(propertyDescriptor.getName());
      ExpressionTypingUtils.checkVariableShadowing(context, propertyDescriptor, olderVariable);
    }

    scope.addVariableDescriptor(propertyDescriptor);
    ModifiersChecker.create(context.trace).checkModifiersForLocalDeclaration(property);
    return DataFlowUtils.checkStatementType(property, context, dataFlowInfo);
  }
Esempio n. 5
0
  private void resolvePropertyAccessors(
      JetProperty property, PropertyDescriptor propertyDescriptor) {
    ObservableBindingTrace fieldAccessTrackingTrace = createFieldTrackingTrace(propertyDescriptor);

    JetPropertyAccessor getter = property.getGetter();
    PropertyGetterDescriptor getterDescriptor = propertyDescriptor.getGetter();
    if (getter != null && getterDescriptor != null) {
      JetScope accessorScope = makeScopeForPropertyAccessor(getter, propertyDescriptor);
      resolveFunctionBody(fieldAccessTrackingTrace, getter, getterDescriptor, accessorScope);
    }

    JetPropertyAccessor setter = property.getSetter();
    PropertySetterDescriptor setterDescriptor = propertyDescriptor.getSetter();
    if (setter != null && setterDescriptor != null) {
      JetScope accessorScope = makeScopeForPropertyAccessor(setter, propertyDescriptor);
      resolveFunctionBody(fieldAccessTrackingTrace, setter, setterDescriptor, accessorScope);
    }
  }
Esempio n. 6
0
 private void generateSetter(JetProperty p, PropertyDescriptor propertyDescriptor) {
   final JetPropertyAccessor setter = p.getSetter();
   if (setter != null) {
     if (setter.getBodyExpression() != null) {
       final PropertySetterDescriptor setterDescriptor = propertyDescriptor.getSetter();
       assert setterDescriptor != null;
       JvmPropertyAccessorSignature signature =
           state.getInjector().getJetTypeMapper().mapSetterSignature(propertyDescriptor, kind);
       functionCodegen.generateMethod(
           setter,
           signature.getJvmMethodSignature(),
           true,
           signature.getPropertyTypeKotlinSignature(),
           setterDescriptor);
     } else if (isExternallyAccessible(propertyDescriptor)) {
       generateDefaultSetter(p);
     }
   } else if (isExternallyAccessible(propertyDescriptor) && propertyDescriptor.isVar()) {
     generateDefaultSetter(p);
   }
 }