projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.83k stars 2.37k forks source link

@Builder on non-static inner classes does not work. #1302

Open rzwitserloot opened 7 years ago

rzwitserloot commented 7 years ago

Tests case coming up for more detail on this.

Probably correct solution:

Generate the builder method and the builderclass as a sibling to the inner; there does not appear to be anything else that's workable.

For now you just get an error that 'static' is not allowed here; that'll do fine until we get around to solving this properly.

bademux commented 7 years ago

Can you please prioritize this? meanwhile my hackfix

public class SearchClient {

  private final Service service;

  @Autowired
  public SearchClient(Service service) {
    this.service = service;
  }

  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  public class Requester {

    private final String source;

    public void sendRequest() {
      service.send(source);
    }
  }
  //hackfix for https://github.com/rzwitserloot/lombok/issues/1302
  @Builder
  private Requester createRequester(String source) {
    return new Requester(source);
  }

}
lnhrdt commented 7 years ago

For now you just get an error that 'static' is not allowed here; that'll do fine until we get around to solving this properly.

@rzwitserloot what exactly are you suggesting here? I cannot compile an inner class with a builder annotation unless that inner class is static. Compilation fails with java: modifier static not allowed here. Making any inner classes static has been my workaround for a while but now I'm trying to use these classes in a context where the static classes are causing an issue (FreeMarker templates). Any tips?

rspilker commented 7 years ago

<jargon mode="on">Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.</jargon>

lnhrdt commented 7 years ago

Thanks @rspilker for the jargon clarification. Using the correct terminology, do you have any advice for getting @Builder annotations working on inner classes? I'm only able to add them to outer classes and static nested classes.

catchsudheera commented 7 years ago

Probably correct solution:

Generate the builder method and the builderclass as a sibling to the inner

Hi @rzwitserloot ,

IMO, this might be little trickier than that. The Builder class being static inner class of the top most class(i.e being a sibling for the inner class), makes it's not able to instantiate "the inner class" within the build() method of it. (The compiler might throw "..cannot be referenced from a static context" like error). The only way the build() method the static inner builder of the inner class is, passing the reference of top most class to it(or new'ing it using the constructor). Even conceptually, building a inner class's object without wrapping class's object us just wrong.

Apart from that I have another concern regarding the inner classes builder static class being public.

Hope this helps, I just tried to fix this bug and only after realized that this would be more complex than I thought. cheers..!

kid1412621 commented 5 years ago

latest version doesn't support for nest class(static). same error like this: https://stackoverflow.com/questions/53342982/is-it-possible-to-add-builder-and-allargsconstructor-in-static-inner-classes

mjustin commented 3 years ago

The error message is definitely obtuse. How difficult would it be to spell out the issue in an error omitted by the annotation processor, even if a proper fix is not yet on the horizon? e.g. "Builder cannot be generated for non-static inner classes"