Exemplo 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);
 }
Exemplo n.º 2
0
 public void handle(
     final AccessLevel level,
     final Class<? extends java.lang.annotation.Annotation> annotationType) {
   LOMBOK_NODE_TYPE mayBeField = annotationNode.up();
   if (mayBeField == null) return;
   TYPE_TYPE type = typeOf(annotationNode, ast);
   List<FIELD_TYPE> fields = new ArrayList<FIELD_TYPE>();
   if (mayBeField.getKind() == Kind.FIELD) {
     for (LOMBOK_NODE_TYPE node : annotationNode.upFromAnnotationToFields()) {
       fields.add(fieldOf(node, ast));
     }
   } else if (mayBeField.getKind() == Kind.TYPE) {
     for (FIELD_TYPE field : type.fields()) {
       if (!field.annotations(SETTER_PATTERN).isEmpty()) continue;
       if (field.name().startsWith("$")) continue;
       if (field.isFinal()) continue;
       if (field.isStatic()) continue;
       fields.add(field);
     }
   } else {
     annotationNode.addError(canBeUsedOnClassAndFieldOnly(annotationType));
     return;
   }
   generateSetter(type, fields, level);
 }
Exemplo n.º 3
0
 private void generateSetter(
     final TYPE_TYPE type, final List<FIELD_TYPE> fields, final AccessLevel level) {
   for (FIELD_TYPE field : fields) {
     String propertyNameFieldName = "PROP_" + camelCaseToConstant(field.name());
     generatePropertyNameConstant(type, field, propertyNameFieldName);
     generateSetter(type, field, level, propertyNameFieldName);
   }
 }
  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);
  }
Exemplo n.º 5
0
 private void generatePropertyNameConstant(
     final TYPE_TYPE type, final FIELD_TYPE field, final String propertyNameFieldName) {
   String propertyName = field.name();
   if (type.hasField(propertyNameFieldName)) return;
   type.injectField(
       FieldDecl(Type(String.class), propertyNameFieldName)
           .makePublic()
           .makeStatic()
           .makeFinal() //
           .withInitialization(String(propertyName)));
 }