mkarneim / pojobuilder

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

Fields using static inner classes with same name generate overlapping imports #154

Closed chrismanning closed 5 years ago

chrismanning commented 5 years ago

E.g.

src
├── main
│   ├── java
│   │   └── a
│   │       ├── A.java
│   │       └── b
│   │           ├── B.java
│   │           └── C.java

where classes B and C contain a static inner class Inner. Example project: https://github.com/chrismanning/pojobuilder-static-inner-class

Similar to #150 but not quite the same and this still occurs in 4.2.2

Adrodoc commented 5 years ago

Can confirm for 4.2.2. The builder generated for A in the example project looks like this:

package a;

import a.b.B.Inner;
import a.b.C.Inner;
import java.util.List;
import javax.annotation.Generated;
import net.karneim.pojobuilder.GwtIncompatible;

@Generated("PojoBuilder")
public class ABuilder
    implements Cloneable {
  protected ABuilder self;
  protected List<Inner> value$bs$java$util$List;
  protected boolean isSet$bs$java$util$List;
  protected List<Inner> value$cs$java$util$List;
  protected boolean isSet$cs$java$util$List;

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

  /**
   * Sets the default value for the {@link A#bs} property.
   *
   * @param value the default value
   * @return this builder
   */
  public ABuilder withBs(List<Inner> value) {
    this.value$bs$java$util$List = value;
    this.isSet$bs$java$util$List = true;
    return self;
  }

  /**
   * Sets the default value for the {@link A#cs} property.
   *
   * @param value the default value
   * @return this builder
   */
  public ABuilder withCs(List<Inner> value) {
    this.value$cs$java$util$List = value;
    this.isSet$cs$java$util$List = true;
    return self;
  }

  /**
   * Returns a clone of this builder.
   *
   * @return the clone
   */
  @Override
  @GwtIncompatible
  public Object clone() {
    try {
      ABuilder result = (ABuilder)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 ABuilder but() {
    return (ABuilder)clone();
  }

  /**
   * Creates a new {@link A} based on this builder's settings.
   *
   * @return the created A
   */
  public A build() {
    try {
      A result = new A();
      if (isSet$bs$java$util$List) {
        result.bs = value$bs$java$util$List;
      }
      if (isSet$cs$java$util$List) {
        result.cs = value$cs$java$util$List;
      }
      return result;
    } catch (RuntimeException ex) {
      throw ex;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
}

Class A:

package a;

import java.util.List;
import a.b.B;
import a.b.C;
import net.karneim.pojobuilder.GeneratePojoBuilder;

@GeneratePojoBuilder
public class A {
  List<B.Inner> bs;
  List<C.Inner> cs;
}