Exemplo n.º 1
0
 @Override
 public Boolean computeValue(
     SlicedMap map,
     PropertyDescriptor propertyDescriptor,
     Boolean backingFieldRequired,
     boolean valueNotFound) {
   if (propertyDescriptor.getKind() != CallableMemberDescriptor.Kind.DECLARATION) {
     return false;
   }
   backingFieldRequired = valueNotFound ? false : backingFieldRequired;
   assert backingFieldRequired != null;
   // TODO: user BindingContextAccessors
   PsiElement declarationPsiElement =
       map.get(BindingContextUtils.DESCRIPTOR_TO_DECLARATION, propertyDescriptor);
   if (declarationPsiElement instanceof JetParameter) {
     JetParameter jetParameter = (JetParameter) declarationPsiElement;
     return jetParameter.getValOrVarNode() != null
         || backingFieldRequired; // this part is unused because we do not allow access to
                                  // constructor parameters in member bodies
   }
   if (propertyDescriptor.getModality() == Modality.ABSTRACT) return false;
   PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
   PropertySetterDescriptor setter = propertyDescriptor.getSetter();
   if (getter == null) {
     return true;
   } else if (propertyDescriptor.isVar() && setter == null) {
     return true;
   } else if (setter != null
       && !setter.hasBody()
       && setter.getModality() != Modality.ABSTRACT) {
     return true;
   } else if (!getter.hasBody() && getter.getModality() != Modality.ABSTRACT) {
     return true;
   }
   return backingFieldRequired;
 }
Exemplo n.º 2
0
  public void generateDefaultSetter(
      PropertyDescriptor propertyDescriptor, int flags, PsiElement origin) {
    if (propertyDescriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
      throw new IllegalStateException("must not generate code for fake overrides");
    }

    if (kind == OwnerKind.TRAIT_IMPL) {
      return;
    }

    if (kind == OwnerKind.NAMESPACE) {
      flags |= Opcodes.ACC_STATIC;
    }

    PsiElement psiElement =
        state
            .getBindingContext()
            .get(
                BindingContext.DESCRIPTOR_TO_DECLARATION,
                propertyDescriptor.getContainingDeclaration());
    boolean isTrait = psiElement instanceof JetClass && ((JetClass) psiElement).isTrait();
    if (isTrait && !(kind instanceof OwnerKind.DelegateKind)) flags |= Opcodes.ACC_ABSTRACT;

    if (propertyDescriptor.getModality() == Modality.FINAL) {
      flags |= Opcodes.ACC_FINAL;
    }

    JvmPropertyAccessorSignature signature =
        state.getInjector().getJetTypeMapper().mapSetterSignature(propertyDescriptor, kind);
    final String descriptor = signature.getJvmMethodSignature().getAsmMethod().getDescriptor();
    MethodVisitor mv =
        v.newMethod(
            origin, flags, setterName(propertyDescriptor.getName()), descriptor, null, null);
    generateJetPropertyAnnotation(
        mv,
        signature.getPropertyTypeKotlinSignature(),
        signature.getJvmMethodSignature().getKotlinTypeParameter());

    if (propertyDescriptor.getSetter() != null) {
      assert !propertyDescriptor.getSetter().hasBody();
      AnnotationCodegen.forMethod(mv, state.getInjector().getJetTypeMapper())
          .genAnnotations(propertyDescriptor.getSetter());
    }

    if (v.generateCode() != ClassBuilder.Mode.SIGNATURES
        && (!isTrait || kind instanceof OwnerKind.DelegateKind)) {
      if (propertyDescriptor.getModality() != Modality.ABSTRACT) {
        mv.visitCode();
        if (v.generateCode() == ClassBuilder.Mode.STUBS) {
          StubCodegen.generateStubThrow(mv);
        } else {
          InstructionAdapter iv = new InstructionAdapter(mv);
          final Type type =
              state
                  .getInjector()
                  .getJetTypeMapper()
                  .mapType(propertyDescriptor.getType(), MapTypeMode.VALUE);
          int paramCode = 0;
          if (kind != OwnerKind.NAMESPACE) {
            iv.load(0, JetTypeMapper.TYPE_OBJECT);
            paramCode = 1;
          }

          if ((kind instanceof OwnerKind.DelegateKind)
              != (propertyDescriptor.getKind() == FunctionDescriptor.Kind.DELEGATION)) {
            throw new IllegalStateException("mismatching kind in " + propertyDescriptor);
          }

          if (kind instanceof OwnerKind.DelegateKind) {
            OwnerKind.DelegateKind dk = (OwnerKind.DelegateKind) kind;
            iv.load(0, JetTypeMapper.TYPE_OBJECT);
            dk.getDelegate().put(JetTypeMapper.TYPE_OBJECT, iv);

            iv.load(paramCode, type);
            iv.invokeinterface(
                dk.getOwnerClass(), setterName(propertyDescriptor.getName()), descriptor);
          } else {
            iv.load(paramCode, type);
            iv.visitFieldInsn(
                kind == OwnerKind.NAMESPACE ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD,
                state.getInjector().getJetTypeMapper().getOwner(propertyDescriptor, kind),
                propertyDescriptor.getName(),
                type.getDescriptor());
          }

          iv.visitInsn(Opcodes.RETURN);
        }
      }
      FunctionCodegen.endVisit(mv, "setter", origin);
    }
  }