private RemovePartsFromPropertyFix(@NotNull JetProperty element) {
   this(
       element,
       element.getInitializer() != null,
       element.getGetter() != null && element.getGetter().getBodyExpression() != null,
       element.getSetter() != null && element.getSetter().getBodyExpression() != null);
 }
  public AlternativeFieldSignatureData(
      @NotNull JavaAnnotationResolver annotationResolver,
      @NotNull JavaFieldImpl field,
      @NotNull JetType originalReturnType,
      boolean isVar) {
    String signature = SignaturesUtil.getKotlinSignature(annotationResolver, field);

    if (signature == null) {
      setAnnotated(false);
      return;
    }

    setAnnotated(true);
    Project project = field.getPsi().getProject();
    JetProperty altPropertyDeclaration = JetPsiFactory.createProperty(project, signature);

    try {
      checkForSyntaxErrors(altPropertyDeclaration);
      checkFieldAnnotation(altPropertyDeclaration, field, isVar);
      altReturnType =
          computeReturnType(
              originalReturnType,
              altPropertyDeclaration.getTypeRef(),
              new HashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>());
    } catch (AlternativeSignatureMismatchException e) {
      setError(e.getMessage());
    }
  }
  @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);
    }
  }
Beispiel #4
0
  @Override
  public Icon getIcon(@NotNull PsiElement psiElement, int flags) {
    if (psiElement instanceof JetFile) {
      JetFile file = (JetFile) psiElement;
      JetClassOrObject mainClass = getMainClass(file);
      return mainClass != null ? getIcon(mainClass, flags) : JetIcons.FILE;
    }
    if (psiElement instanceof JetNamespaceHeader) {
      return (flags & Iconable.ICON_FLAG_OPEN) != 0
          ? PlatformIcons.PACKAGE_OPEN_ICON
          : PlatformIcons.PACKAGE_ICON;
    }
    if (psiElement instanceof JetNamedFunction) {
      return PsiTreeUtil.getParentOfType(psiElement, JetNamedDeclaration.class) instanceof JetClass
          ? PlatformIcons.METHOD_ICON
          : JetIcons.FUNCTION;
    }
    if (psiElement instanceof JetClass) {
      JetClass jetClass = (JetClass) psiElement;
      if (jetClass.isTrait()) {
        return JetIcons.TRAIT;
      }

      Icon icon =
          jetClass.hasModifier(JetTokens.ENUM_KEYWORD) ? PlatformIcons.ENUM_ICON : JetIcons.CLASS;
      if (jetClass instanceof JetEnumEntry) {
        JetEnumEntry enumEntry = (JetEnumEntry) jetClass;
        if (enumEntry.getPrimaryConstructorParameterList() == null) {
          icon = PlatformIcons.ENUM_ICON;
        }
      }
      return icon;
    }
    if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) {
      return JetIcons.OBJECT;
    }
    if (psiElement instanceof JetParameter) {
      JetParameter parameter = (JetParameter) psiElement;
      if (parameter.getValOrVarNode() != null) {
        JetParameterList parameterList =
            PsiTreeUtil.getParentOfType(psiElement, JetParameterList.class);
        if (parameterList != null && parameterList.getParent() instanceof JetClass) {
          return parameter.isMutable() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
        }
      }
      return JetIcons.PARAMETER;
    }
    if (psiElement instanceof JetProperty) {
      JetProperty property = (JetProperty) psiElement;
      return property.isVar() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
    }
    return null;
  }
Beispiel #5
0
  private void resolvePropertyDeclarationBodies() {

    // Member properties
    Set<JetProperty> processed = Sets.newHashSet();
    for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
      JetClass jetClass = entry.getKey();
      if (!context.completeAnalysisNeeded(jetClass)) continue;
      MutableClassDescriptor classDescriptor = entry.getValue();

      for (JetProperty property : jetClass.getProperties()) {
        final PropertyDescriptor propertyDescriptor = this.context.getProperties().get(property);
        assert propertyDescriptor != null;

        computeDeferredType(propertyDescriptor.getReturnType());

        JetExpression initializer = property.getInitializer();
        if (initializer != null) {
          ConstructorDescriptor primaryConstructor =
              classDescriptor.getUnsubstitutedPrimaryConstructor();
          if (primaryConstructor != null) {
            JetScope declaringScopeForPropertyInitializer =
                this.context.getDeclaringScopes().get(property);
            resolvePropertyInitializer(
                property, propertyDescriptor, initializer, declaringScopeForPropertyInitializer);
          }
        }

        resolvePropertyAccessors(property, propertyDescriptor);
        processed.add(property);
      }
    }

    // Top-level properties & properties of objects
    for (Map.Entry<JetProperty, PropertyDescriptor> entry :
        this.context.getProperties().entrySet()) {
      JetProperty property = entry.getKey();
      if (!context.completeAnalysisNeeded(property)) continue;
      if (processed.contains(property)) continue;

      final PropertyDescriptor propertyDescriptor = entry.getValue();

      computeDeferredType(propertyDescriptor.getReturnType());

      JetScope declaringScope = this.context.getDeclaringScopes().get(property);

      JetExpression initializer = property.getInitializer();
      if (initializer != null) {
        resolvePropertyInitializer(property, propertyDescriptor, initializer, declaringScope);
      }

      resolvePropertyAccessors(property, propertyDescriptor);
    }
  }
Beispiel #6
0
 private void resolvePropertyInitializer(
     JetProperty property,
     PropertyDescriptor propertyDescriptor,
     JetExpression initializer,
     JetScope scope) {
   // JetFlowInformationProvider flowInformationProvider =
   // context.getDescriptorResolver().computeFlowData(property, initializer); // TODO : flow JET-15
   JetType expectedTypeForInitializer =
       property.getPropertyTypeRef() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE;
   JetType type =
       expressionTypingServices.getType(
           descriptorResolver.getPropertyDeclarationInnerScope(
               scope,
               propertyDescriptor,
               propertyDescriptor.getTypeParameters(),
               propertyDescriptor.getReceiverParameter(),
               trace),
           initializer,
           expectedTypeForInitializer,
           trace);
   //
   //        JetType expectedType = propertyDescriptor.getInType();
   //        if (expectedType == null) {
   //            expectedType = propertyDescriptor.getType();
   //        }
   //        if (type != null && expectedType != null
   //            && !context.getSemanticServices().getTypeChecker().isSubtypeOf(type,
   // expectedType)) {
   ////            trace.report(TYPE_MISMATCH.on(initializer, expectedType, type));
   //        }
 }
Beispiel #7
0
  private void initializeProperty(
      @NotNull ExpressionCodegen codegen, @NotNull JetProperty property) {
    PropertyDescriptor propertyDescriptor =
        (PropertyDescriptor) bindingContext.get(VARIABLE, property);
    assert propertyDescriptor != null;

    JetExpression initializer = property.getDelegateExpressionOrInitializer();
    assert initializer != null
        : "shouldInitializeProperty must return false if initializer is null";

    JetType jetType = getPropertyOrDelegateType(property, propertyDescriptor);

    StackValue.Property propValue =
        codegen.intermediateValueForProperty(
            propertyDescriptor, true, null, MethodKind.INITIALIZER);

    if (!propValue.isStatic) {
      codegen.v.load(0, OBJECT_TYPE);
    }

    Type type = codegen.expressionType(initializer);
    if (jetType.isNullable()) {
      type = boxType(type);
    }
    codegen.gen(initializer, type);

    propValue.store(type, codegen.v);
  }
 private void checkProperty(JetProperty property, PropertyDescriptor propertyDescriptor) {
   for (JetPropertyAccessor accessor : property.getAccessors()) {
     PropertyAccessorDescriptor accessorDescriptor =
         accessor.isGetter() ? propertyDescriptor.getGetter() : propertyDescriptor.getSetter();
     assert accessorDescriptor != null;
     checkFunction(accessor, accessorDescriptor.getReturnType());
   }
 }
  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 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);
  }
Beispiel #11
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);
    }
  }
Beispiel #12
0
  protected void generatePropertyMetadataArrayFieldIfNeeded(@NotNull Type thisAsmType) {
    List<JetProperty> delegatedProperties = new ArrayList<JetProperty>();
    for (JetDeclaration declaration : ((JetDeclarationContainer) element).getDeclarations()) {
      if (declaration instanceof JetProperty) {
        JetProperty property = (JetProperty) declaration;
        if (property.getDelegate() != null) {
          delegatedProperties.add(property);
        }
      }
    }
    if (delegatedProperties.isEmpty()) return;

    v.newField(
        null,
        ACC_PRIVATE | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC,
        JvmAbi.PROPERTY_METADATA_ARRAY_NAME,
        "[" + PROPERTY_METADATA_TYPE,
        null,
        null);

    InstructionAdapter iv = createOrGetClInitCodegen().v;
    iv.iconst(delegatedProperties.size());
    iv.newarray(PROPERTY_METADATA_TYPE);

    for (int i = 0, size = delegatedProperties.size(); i < size; i++) {
      VariableDescriptor property =
          BindingContextUtils.getNotNull(bindingContext, VARIABLE, delegatedProperties.get(i));

      iv.dup();
      iv.iconst(i);
      iv.anew(PROPERTY_METADATA_IMPL_TYPE);
      iv.dup();
      iv.visitLdcInsn(property.getName().asString());
      iv.invokespecial(
          PROPERTY_METADATA_IMPL_TYPE.getInternalName(), "<init>", "(Ljava/lang/String;)V");
      iv.astore(PROPERTY_METADATA_IMPL_TYPE);
    }

    iv.putstatic(
        thisAsmType.getInternalName(),
        JvmAbi.PROPERTY_METADATA_ARRAY_NAME,
        "[" + PROPERTY_METADATA_TYPE);
  }
Beispiel #13
0
 @NotNull
 private JetType getPropertyOrDelegateType(
     @NotNull JetProperty property, @NotNull PropertyDescriptor descriptor) {
   JetExpression delegateExpression = property.getDelegateExpression();
   if (delegateExpression != null) {
     JetType delegateType = bindingContext.get(BindingContext.EXPRESSION_TYPE, delegateExpression);
     assert delegateType != null : "Type of delegate expression should be recorded";
     return delegateType;
   }
   return descriptor.getType();
 }
Beispiel #14
0
  private void generateBackingField(JetProperty p, PropertyDescriptor propertyDescriptor) {
    if (state.getBindingContext().get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor)) {
      DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
      if (CodegenUtil.isInterface(containingDeclaration)) return;

      Object value = null;
      final JetExpression initializer = p.getInitializer();
      if (initializer != null) {
        if (initializer instanceof JetConstantExpression) {
          CompileTimeConstant<?> compileTimeValue =
              state.getBindingContext().get(BindingContext.COMPILE_TIME_VALUE, initializer);
          value = compileTimeValue != null ? compileTimeValue.getValue() : null;
        }
      }
      int modifiers;
      if (kind == OwnerKind.NAMESPACE) {
        int access = JetTypeMapper.getAccessModifiers(propertyDescriptor, 0);
        modifiers = access | Opcodes.ACC_STATIC;
      } else {
        modifiers = JetTypeMapper.getAccessModifiers(propertyDescriptor, 0);
      }
      if (!propertyDescriptor.isVar()) {
        modifiers |= Opcodes.ACC_FINAL;
      }
      if (state.getInjector().getJetStandardLibrary().isVolatile(propertyDescriptor)) {
        modifiers |= Opcodes.ACC_VOLATILE;
      }
      Type type =
          state
              .getInjector()
              .getJetTypeMapper()
              .mapType(propertyDescriptor.getType(), MapTypeMode.VALUE);
      FieldVisitor fieldVisitor =
          v.newField(p, modifiers, p.getName(), type.getDescriptor(), null, value);
      AnnotationCodegen.forField(fieldVisitor, state.getInjector().getJetTypeMapper())
          .genAnnotations(propertyDescriptor);
    }
  }
Beispiel #15
0
  private boolean shouldInitializeProperty(@NotNull JetProperty property) {
    JetExpression initializer = property.getDelegateExpressionOrInitializer();
    if (initializer == null) return false;

    PropertyDescriptor propertyDescriptor =
        (PropertyDescriptor) bindingContext.get(VARIABLE, property);
    assert propertyDescriptor != null;

    CompileTimeConstant<?> compileTimeValue = propertyDescriptor.getCompileTimeInitializer();
    if (compileTimeValue == null) return true;

    // TODO: OPTIMIZATION: don't initialize static final fields

    Object value = compileTimeValue.getValue();
    JetType jetType = getPropertyOrDelegateType(property, propertyDescriptor);
    Type type = typeMapper.mapType(jetType);
    return !skipDefaultValue(propertyDescriptor, value, type);
  }
Beispiel #16
0
 private void generateGetter(JetProperty p, PropertyDescriptor propertyDescriptor) {
   final JetPropertyAccessor getter = p.getGetter();
   if (getter != null) {
     if (getter.getBodyExpression() != null) {
       JvmPropertyAccessorSignature signature =
           state.getInjector().getJetTypeMapper().mapGetterSignature(propertyDescriptor, kind);
       functionCodegen.generateMethod(
           getter,
           signature.getJvmMethodSignature(),
           true,
           signature.getPropertyTypeKotlinSignature(),
           propertyDescriptor.getGetter());
     } else if (isExternallyAccessible(propertyDescriptor)) {
       generateDefaultGetter(p);
     }
   } else if (isExternallyAccessible(propertyDescriptor)) {
     generateDefaultGetter(p);
   }
 }
Beispiel #17
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);
   }
 }
  private static void propertyAdditionalResolve(
      final ResolveSession resolveSession,
      final JetProperty jetProperty,
      DelegatingBindingTrace trace,
      JetFile file) {
    final JetScope propertyResolutionScope =
        resolveSession
            .getInjector()
            .getScopeProvider()
            .getResolutionScopeForDeclaration(jetProperty);

    BodyResolveContextForLazy bodyResolveContext =
        new BodyResolveContextForLazy(
            new Function<JetDeclaration, JetScope>() {
              @Override
              public JetScope apply(JetDeclaration declaration) {
                assert declaration.getParent() == jetProperty
                    : "Must be called only for property accessors, but called for " + declaration;
                return propertyResolutionScope;
              }
            });
    BodyResolver bodyResolver =
        createBodyResolver(
            trace, file, bodyResolveContext, resolveSession.getModuleConfiguration());
    PropertyDescriptor descriptor =
        (PropertyDescriptor) resolveSession.resolveToDescriptor(jetProperty);

    JetExpression propertyInitializer = jetProperty.getInitializer();

    if (propertyInitializer != null) {
      bodyResolver.resolvePropertyInitializer(
          jetProperty, descriptor, propertyInitializer, propertyResolutionScope);
    }

    bodyResolver.resolvePropertyAccessors(jetProperty, descriptor);
  }
Beispiel #19
0
  public static Icon getBaseIcon(PsiElement psiElement) {
    if (psiElement instanceof JetNamespaceHeader) {
      return PlatformIcons.PACKAGE_ICON;
    }

    if (psiElement instanceof KotlinLightClassForPackage) {
      return JetIcons.FILE;
    }

    if (psiElement instanceof KotlinLightClassForExplicitDeclaration) {
      psiElement = psiElement.getNavigationElement();
    }

    if (psiElement instanceof JetNamedFunction) {
      if (((JetFunction) psiElement).getReceiverTypeRef() != null) {
        return JetIcons.EXTENSION_FUNCTION;
      }

      if (PsiTreeUtil.getParentOfType(psiElement, JetNamedDeclaration.class) instanceof JetClass) {
        if (JetPsiUtil.isAbstract((JetFunction) psiElement)) {
          return PlatformIcons.ABSTRACT_METHOD_ICON;
        } else {
          return PlatformIcons.METHOD_ICON;
        }
      } else {
        return JetIcons.FUNCTION;
      }
    }
    if (psiElement instanceof JetClass) {
      JetClass jetClass = (JetClass) psiElement;
      if (jetClass.isTrait()) {
        return JetIcons.TRAIT;
      }

      Icon icon = jetClass.isEnum() ? PlatformIcons.ENUM_ICON : JetIcons.CLASS;
      if (jetClass instanceof JetEnumEntry) {
        JetEnumEntry enumEntry = (JetEnumEntry) jetClass;
        if (enumEntry.getPrimaryConstructorParameterList() == null) {
          icon = PlatformIcons.ENUM_ICON;
        }
      }
      return icon;
    }
    if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) {
      return JetIcons.OBJECT;
    }
    if (psiElement instanceof JetParameter) {
      JetParameter parameter = (JetParameter) psiElement;
      if (parameter.getValOrVarNode() != null) {
        JetParameterList parameterList =
            PsiTreeUtil.getParentOfType(psiElement, JetParameterList.class);
        if (parameterList != null && parameterList.getParent() instanceof JetClass) {
          return parameter.isMutable() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
        }
      }
      return JetIcons.PARAMETER;
    }
    if (psiElement instanceof JetProperty) {
      JetProperty property = (JetProperty) psiElement;
      return property.isVar() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL;
    }

    return null;
  }
 @Override
 public void visitProperty(JetProperty property) {
   nameStack.push(peekFromStack(nameStack) + '$' + property.getName());
   super.visitProperty(property);
   nameStack.pop();
 }