After all that, actually, I think I found a much simpler way to fix this
problem, and it allows for arbitrary ordering of the builder chain while
still working properly. The old factory methods just need to add a generic
signature as well:

public static <B extends Builder<B>> MockAppender createAppender(...) {
  return MockAppender.<B>newBuilder()
    // ...
    .build();
}

Anyways, that was a rather amusing string of generics in the gist. Oh well.

On 6 January 2017 at 22:31, Matt Sicker <boa...@gmail.com> wrote:

> So I've noticed that although everything compiles correctly, IntelliJ has
> difficulty understanding the generics going on in the hierarchy of builder
> classes. If you use methods from the parent in a chain, it returns the
> wrong class, though this doesn't really matter to the compiled code due to
> type erasure.
>
> However, after much experimentation, I have figured out a rather monstrous
> line of generics that works properly. Now bear with me as we explore the
> dark depths of Java generics and fluent DSLs.
>
> https://gist.github.com/jvz/dd499ccddfb6ffc0336fe0308e32b344
>
> Yes, it looks absolutely ridiculous, but it avoids a lot of IDE problems
> and more explicitly lays out the expectations of the builders themselves.
> What do you think?
>
> --
> Matt Sicker <boa...@gmail.com>
>



-- 
Matt Sicker <boa...@gmail.com>

Reply via email to