Esempio n. 1
0
 private void generateSetter(
     final TYPE_TYPE type,
     final FIELD_TYPE field,
     final AccessLevel level,
     final String propertyNameFieldName) {
   String fieldName = field.name();
   boolean isBoolean = field.isOfType("boolean");
   String setterName = TransformationsUtil.toSetterName(fieldName, isBoolean);
   if (type.hasMethod(setterName)) return;
   String oldValueName = OLD_VALUE_VARIABLE_NAME;
   List<lombok.ast.Annotation> nonNulls = field.annotations(TransformationsUtil.NON_NULL_PATTERN);
   MethodDecl methodDecl =
       MethodDecl(Type("void"), setterName)
           .withAccessLevel(level)
           .withArgument(Arg(field.type(), fieldName).withAnnotations(nonNulls));
   if (!nonNulls.isEmpty() && !field.isPrimitive()) {
     methodDecl.withStatement(
         If(Equal(Name(fieldName), Null()))
             .Then(Throw(New(Type(NullPointerException.class)).withArgument(String(fieldName)))));
   }
   methodDecl
       .withStatement(
           LocalDecl(field.type(), oldValueName)
               .makeFinal()
               .withInitialization(Field(fieldName))) //
       .withStatement(Assign(Field(fieldName), Name(fieldName))) //
       .withStatement(
           Call(Call(PROPERTY_CHANGE_SUPPORT_METHOD_NAME), FIRE_PROPERTY_CHANGE_METHOD_NAME) //
               .withArgument(Name(propertyNameFieldName))
               .withArgument(Name(oldValueName))
               .withArgument(Field(fieldName)));
   type.injectMethod(methodDecl);
 }
  private void generateSetter(
      final TYPE_TYPE type,
      final FIELD_TYPE field,
      final AccessLevel level,
      final boolean vetoable,
      final boolean throwVetoException,
      final String propertyNameFieldName) {
    String fieldName = field.filteredName();
    boolean isBoolean = field.isOfType("boolean");
    String setterName =
        toSetterName(field.getAnnotationValue(Accessors.class), field.name(), isBoolean);
    if (type.hasMethod(setterName, field.type())) return;
    String oldValueName = OLD_VALUE_VARIABLE_NAME;
    List<lombok.ast.Annotation> nonNulls = field.annotations(TransformationsUtil.NON_NULL_PATTERN);
    MethodDecl methodDecl =
        MethodDecl(Type("void"), setterName)
            .withAccessLevel(level)
            .withArgument(Arg(field.type(), fieldName).withAnnotations(nonNulls));
    if (!nonNulls.isEmpty() && !field.isPrimitive()) {
      methodDecl.withStatement(
          If(Equal(Name(fieldName), Null()))
              .Then(Throw(New(Type(NullPointerException.class)).withArgument(String(fieldName)))));
    }

    methodDecl.withStatement(
        LocalDecl(field.type(), oldValueName).makeFinal().withInitialization(Field(fieldName)));

    if (vetoable) {
      if (throwVetoException) {
        methodDecl.withThrownException(Type(PropertyVetoException.class));
        methodDecl.withStatement(
            Call(FIRE_VETOABLE_CHANGE_METHOD_NAME) //
                .withArgument(Name(propertyNameFieldName))
                .withArgument(Name(oldValueName))
                .withArgument(Name(fieldName)));
      } else {
        methodDecl.withStatement(
            Try(Block()
                    .withStatement(
                        Call(FIRE_VETOABLE_CHANGE_METHOD_NAME) //
                            .withArgument(Name(propertyNameFieldName))
                            .withArgument(Name(oldValueName))
                            .withArgument(Name(fieldName)))) //
                .Catch(
                    Arg(Type(PropertyVetoException.class), E_VALUE_VARIABLE_NAME),
                    Block().withStatement(Return())));
      }
    }

    methodDecl
        .withStatement(Assign(Field(fieldName), Name(fieldName))) //
        .withStatement(
            Call(FIRE_PROPERTY_CHANGE_METHOD_NAME) //
                .withArgument(Name(propertyNameFieldName))
                .withArgument(Name(oldValueName))
                .withArgument(Name(fieldName)));
    type.editor().injectMethod(methodDecl);
  }