mkarneim / pojobuilder

A Java Code Generator for Pojo Builders
Other
334 stars 44 forks source link

Fields of a type with the same name as the optional type produce incorrect import #156

Closed Adrodoc closed 5 years ago

Adrodoc commented 5 years ago

In version 4.2.2 the following class:

@GeneratePojoBuilder(withOptionalProperties=java.util.Optional.class)
public class PojoWithAmbiguousOptionalImports {
  public com.google.common.base.Optional<String> field;
}

causes the generation of the following builder:

package net.karneim.pojobuilder.processor.with.ambiguousimports.optional;

import com.google.common.base.Optional;
import javax.annotation.processing.Generated;
import net.karneim.pojobuilder.GwtIncompatible;

@Generated("PojoBuilder")
public class PojoWithAmbiguousOptionalImportsBuilder
    implements Cloneable {
  protected PojoWithAmbiguousOptionalImportsBuilder self;
  protected java.util.Optional<? extends Optional<String>> value$field$com$google$common$base$Optional = Optional.empty();

  /**
   * Creates a new {@link PojoWithAmbiguousOptionalImportsBuilder}.
   */
  public PojoWithAmbiguousOptionalImportsBuilder() {
    self = (PojoWithAmbiguousOptionalImportsBuilder)this;
  }

  /**
   * Sets the default value for the {@link PojoWithAmbiguousOptionalImports#field} property.
   *
   * @param value the default value
   * @return this builder
   */
  public PojoWithAmbiguousOptionalImportsBuilder withField(Optional<String> value) {
    if (value == null) {
      this.value$field$com$google$common$base$Optional = null;
    } else {
      this.value$field$com$google$common$base$Optional = Optional.of(value);
    }
    return self;
  }

  /**
   * Optionally sets the default value for the {@link PojoWithAmbiguousOptionalImports#field} property.
   *
   * @param optionalValue the optional default value
   * @return this builder
   */
  public PojoWithAmbiguousOptionalImportsBuilder withField(java.util.Optional<? extends Optional<String>> optionalValue) {
    if (optionalValue == null || optionalValue.isPresent()) {
      this.value$field$com$google$common$base$Optional = optionalValue;
    }
    return self;
  }

  /**
   * Returns a clone of this builder.
   *
   * @return the clone
   */
  @Override
  @GwtIncompatible
  public Object clone() {
    try {
      PojoWithAmbiguousOptionalImportsBuilder result = (PojoWithAmbiguousOptionalImportsBuilder)super.clone();
      result.self = result;
      return result;
    } catch (CloneNotSupportedException e) {
      throw new InternalError(e.getMessage());
    }
  }

  /**
   * Returns a clone of this builder.
   *
   * @return the clone
   */
  @GwtIncompatible
  public PojoWithAmbiguousOptionalImportsBuilder but() {
    return (PojoWithAmbiguousOptionalImportsBuilder)clone();
  }

  /**
   * Creates a new {@link PojoWithAmbiguousOptionalImports} based on this builder's settings.
   *
   * @return the created PojoWithAmbiguousOptionalImports
   */
  public PojoWithAmbiguousOptionalImports build() {
    try {
      PojoWithAmbiguousOptionalImports result = new PojoWithAmbiguousOptionalImports();
      if (value$field$com$google$common$base$Optional == null) {
        result.field = (Optional<String>) null;
      } else if (value$field$com$google$common$base$Optional.isPresent()) {
        result.field = value$field$com$google$common$base$Optional.get();
      }
      return result;
    } catch (RuntimeException ex) {
      throw ex;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
}

This Builder contains compile errors, because Optional.empty() and Optional.of(value) both refer to the wrong optional type.