rigd-loxia / builder-generator

Yet another builder generator. Primary style of this generator will be fluent wither, although other styles will be supported in the future. There is support for builder chaining.
Apache License 2.0
2 stars 2 forks source link

Multiple collections of same see also annotated class #28

Open Zegveld opened 11 months ago

Zegveld commented 11 months ago

Given the following situation the resulting code does not compile because of overlap between own and inherited methods.

@SeeAlso( ExtendedField.class )
@Builder
class Field {
// fields
}

@Builder
class ExtendedField extends Field {
// fields
}

@Builder
class Listings {
  List<Field> fields;
  List<Field> otherFields;
}

currently generated code:

class ListingsBuilder {
  List<FieldBuilder> fieldsBuilders = new ArrayList<>();
  List<FieldBuilder> otherFieldsBuilders = new ArrayList<>();

  FieldBuilder<ListingsBuilder<PARENT>> addFields() {
    FieldBuilder<ListingsBuilder<PARENT>> builder = new FieldBuilder(this);
    fieldsBuilders.add(builder);
    return builder;
  }

  FieldBuilder<ListingsBuilder<PARENT>> addOtherFields() {
    FieldBuilder<ListingsBuilder<PARENT>> builder = new FieldBuilder(this);
    otherFieldsBuilders.add(builder);
    return builder;
  }

  // problem with the following 2 methods:
  ExtendedFieldBuilder<ListingsBuilder<PARENT>> addExtendedField() {
    ExtendedFieldBuilder<ListingsBuilder<PARENT>> builder = new ExtendedFieldBuilder(this);
    fieldsBuilders.add(builder);
    return builder;
  }

  ExtendedFieldBuilder<ListingsBuilder<PARENT>> addExtendedField() {
    ExtendedFieldBuilder<ListingsBuilder<PARENT>> builder = new ExtendedFieldBuilder(this);
    otherFieldsBuilders.add(builder);
    return builder;
  }
}
Zegveld commented 11 months ago

Possible solutions:

  1. Add an option to disable see also behavior
  2. Add an option to make see also behavior explicit. For example: instead of addExtendedField() in the Parent, this would become something like: addExtendedFieldToFields()
  3. Always make the behavior explicit by adding To<collectionForSeeAlso>
  4. Perhaps another option is available.
Zegveld commented 11 months ago

Thinking about going for option 3. Using a list of example methods to determine what reads better.

Using classes:

@SeeAlso( Child.class )
class Person {
// fields
}

class Child extends Person {
// fields
}

class Parent {
  Collection<Person> children;
}

in the ParentBuilder, besides the PersonBuilder<...> addChildren() method how would the see also annotated fields called:

  1. ChildBuilder<...> addChildInChildren() parentBuilder.addChildInChildren()/* fill child fields */.end() // back to parentBuilder
  2. ChildBuilder<...> addChildToChildren() parentBuilder.addChildToChildren()/* fill child fields */.end() // back to parentBuilder
  3. ChildBuilder<...> addChildInsideChildren() parentBuilder.addChildInsideChildren()/* fill child fields */.end() // back to parentBuilder
  4. ChildBuilder<...> addChildToChildrenCollection() parentBuilder.addChildToChildrenCollection()/* fill child fields */.end() // back to parentBuilder