Lombok @Builder not working in lombok-1.16.18 java:cannot find symbol builderclass
Asked Answered
T

6

16
@Builder
public class ProcessorLombokBO {
    private String panel;
    private String motherBoard;
    private String ram;
    private String hardDisk;
}

public static void main(String[] args) {
    ProcessorLombokBO processorLombokBO =
                    new ProcessorLombokBO.ProcessorLombokBOBuilder()
                    .panel("Brown")
                    .hardDisk("SanDisk 256GB")
                    .ram("4GB")
                    .motherBoard("Intel")
                    .build();
    System.out.println(processorLombokBO);
}

I am trying to implemenent @Builder in a POJO, but it gives the below error when I execute the main method.

java: cannot find symbol symbol: class ProcessorLombokBOBuilder

Did I miss something?

Talebearer answered 8/1, 2018 at 12:33 Comment(4)
Where is the error, in your IDE? If so then are you sure your IDE is compatible with Lombok and you have required plugins/extensions?Quirinal
Lombok-plugin for IDE installed?Neonate
ya.. Using Intellij.. Other examples are working fine with lombokTalebearer
I had the same problem. Added lombok to the project (plugin was installed before), all lombok-related stuff worked instead of builder, which was not visible. After restart Android studio the issue disappearedHartford
U
8

I had this same issue and found that @Builder support was disabled in IntelliJ:

Intellij 2018.3 Lombok Plugin Settings

Uproot answered 2/1, 2019 at 21:59 Comment(4)
can you please tell where to find this setting, i can't find it in mine. Thank youCaviar
It was at: Preferences -> Languages & Frameworks -> Lombok plugin for me. FYI: I was able to quickly find this using the search feature in Preferences.Khanna
@DoctorParameter I can't find this setting - For Lombok, I only see "Enable lombok version warning" checkbox and nothing else.Canikin
I'm on IntelliJ v2020.2 and Lombok plugin 0.33-2020.2. Maybe the setting is gone or you need to update?Khanna
S
6

If you are using mapstruct-processor plugin in your Maven project, it will affect the builder method.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.2.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
Swampy answered 15/5, 2022 at 13:5 Comment(3)
Exactly my problem, thanks for the idea.Citric
Is that a solution ? I already have this annotationProcessorPaths element in the plugin and I'm still facing the OP issue.Bailment
Yes it is, for some reason, if you don't add this plugin, it would cause failure. Have you tried copying what my solution to your Maven? Maybe you forgot something somewhere. @BailmentSwampy
S
3

Edit: Try making your inner ProcessorLombokBO class static. You can't use Builder on an inner non-static class, as the compile error at your example suggests in my IDE (Eclipse)

The method builder cannot be declared static; static methods can only be declared in a static or top level type

The reason is that @Buildergenerates an inner static class (the ProcessorLombokBOBuilder class) and adds a static method builder(), which is by specification only possible if the outer class is also static, because else you need an enclosing instance to reference the Builder class, which contradicts the concept of a static type.

Original Post: Usually you instanciate your Builder via

ProcessorLombokBO.builder()
Sentient answered 8/1, 2018 at 12:48 Comment(6)
I have tried the same. I am getting this error java: cannot find symbol symbol: method builder()Talebearer
Thanks for your reply. The same code was working in eclipse. Things are not working only in Intellij. I dont know why.Talebearer
Can you post a complete example of your code? Is ProcessorLombokBO in a dedicated java file or is it an inner class in the same class as your main() function?Sentient
I have the same problem in 1.16.18 - the builder() method was not visible anywhere in the code. I've downgraded Lombok to 1.16.16 and builder() works as intended. What's strange is that after I've upgraded to 1.16.18 again, everything works - maybe its an Issue with some caching in Intellij?Bemean
That's odd. Maybe you just forgot to restart your ide after installing the plugin the first time? That's what happened to me once :-) Else, if you can reproduce that behaviour it could be interesting to the folks at the Lombok github pageSentient
As @Bemean proposed, I've just rebuild my pom.xml and everything back to normal.Rosin
T
3

Try adding the following entry to your POM, it helped in my case:

                       <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>0.2.0</version>
                        </path>
                    </annotationProcessorPaths>
Tatianatatianas answered 31/7, 2022 at 17:29 Comment(1)
I also had to have the lombok dependency.Bailment
B
1

For the record, following up on the solution provided by @markkush to whom credits should go, I could work around the same issue as the one posted by the OP. I also had to have the lombok Maven dependency and the following entries in the compiler plugin:

  <properties>
    <lombok.version>1.18.26</lombok.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>17</source>
          <target>17</target>
          <annotationProcessorPaths>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>1.5.5.Final</version>
            </path>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>${lombok.version}</version>
            </path>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok-mapstruct-binding</artifactId>
              <version>0.2.0</version>
            </path>
          </annotationProcessorPaths>
        </configuration>
      </plugin>
    </plugins>
  </build>
Bailment answered 1/5, 2023 at 15:49 Comment(0)
S
0

update plugin like this in pom.xml if you are using lombok and mapstruct

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <annotationProcessorPaths>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.18.32</version>
            </path>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>${org.mapstruct.version}</version>
            </path>
          </annotationProcessorPaths>
          <release>${java.version}</release>
        </configuration>
      </plugin>
Spume answered 25/6 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.