projectlombok / lombok

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

[BUG] Using the Builder annotation, it is not possible to get the Builder by way of new #3352

Open NightJarJar opened 1 year ago

NightJarJar commented 1 year ago

I use the @Builder annotation in my project; when I try to get the Builder object using the new SomePojo.SomePojoBuilder() method, access is restricted image By decompiling the class file, you can see that the Builder internal class modifier in the compiled class file is public static. Usually, in Java, an internal class modified by public static can be accessed externally; used @Builder class image unused @Builder class image Since the compiled class file has the internal class modified as public static, why can't I get the builder by new in my code? Instead, you have to call the static method builder() to get the builder.

This is my own hand-written Builder, which can be instantiated by way of new; image

Is this a bug or is it by design?

janrieke commented 1 year ago

It's by design. There are two idioms for builder creation, and at least at the time of introduction of the @Builder annotation in Lombok, there was no clear preference in the Java community. So Lombok chose one (and IMO its the better one, because it's shorter and more readable especially when there are type parameters like with @SuperBuilder). And as there should not be two syntactical ways for the same task, Lombok does not support the other idiom.

janrieke commented 1 year ago

BTW: You can still customize your builder and manually implement a public constructor (it's just empty), and suppress the generation of the builder() method by @Builder(builderMethodName="").

sband commented 1 year ago

Hi, I am looking at the issues in this project so that I could contribute to this project. From the above discussion it seems that this bug could be closed ? Can I go ahead close this ?