예제 #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 generateGetVetoableSupportMethod(final TYPE_TYPE type) {
   if (type.hasMethod(VETOABLE_CHANGE_SUPPORT_METHOD_NAME)) return;
   type.editor()
       .injectMethod(
           MethodDecl(Type(VetoableChangeSupport.class), VETOABLE_CHANGE_SUPPORT_METHOD_NAME)
               .makePrivate() //
               .withStatement(
                   If(Equal(Field(VETOABLE_CHANGE_SUPPORT_FIELD_NAME), Null()))
                       .Then(
                           Block() //
                               .withStatement(
                                   Synchronized(
                                           Field(VETOABLE_CHANGE_SUPPORT_FIELD_NAME + "Lock")) //
                                       .withStatement(
                                           If(Equal(
                                                   Field(VETOABLE_CHANGE_SUPPORT_FIELD_NAME),
                                                   Null()))
                                               .Then(
                                                   Block() //
                                                       .withStatement(
                                                           Assign(
                                                               Field(
                                                                   VETOABLE_CHANGE_SUPPORT_FIELD_NAME),
                                                               New(Type(
                                                                       VetoableChangeSupport
                                                                           .class))
                                                                   .withArgument(This())))))))) //
               .withStatement(Return(Field(VETOABLE_CHANGE_SUPPORT_FIELD_NAME))));
 }
  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);
  }
 private void generateVetoableChangeListenerMethod(final String methodName, final TYPE_TYPE type) {
   if (type.hasMethod(methodName, Type(VetoableChangeListener.class))) return;
   type.editor()
       .injectMethod(
           MethodDecl(Type("void"), methodName)
               .makePublic()
               .withArgument(Arg(Type(VetoableChangeListener.class), LISTENER_ARG_NAME)) //
               .withStatement(
                   Call(Call(VETOABLE_CHANGE_SUPPORT_METHOD_NAME), methodName)
                       .withArgument(Name(LISTENER_ARG_NAME))));
 }
 private void generateFirePropertyChangeMethod(final TYPE_TYPE type) {
   if (type.hasMethod(
       FIRE_PROPERTY_CHANGE_METHOD_NAME,
       Type(String.class),
       Type(Object.class),
       Type(Object.class))) return;
   type.editor()
       .injectMethod(
           MethodDecl(Type("void"), FIRE_PROPERTY_CHANGE_METHOD_NAME)
               .makePublic() //
               .withArgument(Arg(Type(String.class), PROPERTY_NAME_ARG_NAME))
               .withArgument(Arg(Type(Object.class), OLD_VALUE_ARG_NAME))
               .withArgument(Arg(Type(Object.class), NEW_VALUE_ARG_NAME)) //
               .withStatement(
                   Call(
                           Call(PROPERTY_CHANGE_SUPPORT_METHOD_NAME),
                           FIRE_PROPERTY_CHANGE_METHOD_NAME) //
                       .withArgument(Name(PROPERTY_NAME_ARG_NAME))
                       .withArgument(Name(OLD_VALUE_ARG_NAME))
                       .withArgument(Name(NEW_VALUE_ARG_NAME))));
 }