Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rzwitserloot opened this issue Feb 16, 2017 · 7 comments
Open

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

rzwitserloot opened this issue Feb 16, 2017 · 7 comments
Labels
accepted The issue/enhancement is valid, sensible, and explained in sufficient detail low-priority

Comments

@rzwitserloot
Copy link
Collaborator

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.

@rzwitserloot rzwitserloot added accepted The issue/enhancement is valid, sensible, and explained in sufficient detail low-priority labels Feb 16, 2017
rzwitserloot added a commit that referenced this issue Feb 16, 2017
…inner classes (or constructors of non-static inner classes) doesn't work.
@bademux
Copy link

bademux commented Feb 23, 2017

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
Copy link

lnhrdt commented Mar 13, 2017

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
Copy link
Collaborator

rspilker commented Mar 13, 2017

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

@lnhrdt
Copy link

lnhrdt commented Mar 15, 2017

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.

teosarca added a commit to metasfresh/metasfresh-webui-api-legacy that referenced this issue Jun 30, 2017
@catchsudheera
Copy link

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
Copy link

kid1412621 commented Feb 19, 2019

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
Copy link

mjustin commented Jul 22, 2021

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted The issue/enhancement is valid, sensible, and explained in sufficient detail low-priority
Projects
None yet
Development

No branches or pull requests

7 participants