Home » Java » java – Why Intellij Idea does not delombok builder correctly-Exceptionshub

java – Why Intellij Idea does not delombok builder correctly-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I struggle with a lombok generated builder in Intellij Idea. The minimal example that shows the problem I could create is

import java.io.IOException;
import lombok.AllArgsConstructor;
import lombok.Builder;

class Base { }

@Builder
@AllArgsConstructor
class Scratch extends Base {
    String attr;
    String attr2;

    public Scratch(Base b) throws IOException {
        throw new IOException();
    }

    public static void main(String[] args) {
        Scratch.builder().attr("1").attr2("2").build(); // Idea shows an error here
    }
}

Idea shows an error in the line with the call build() and complains that Unhandled exception: java.io.IOException. But the code is compilable and runnable from command line. The java compiler in idea and command line is the same version of java 8. When I tried to “delombok” the code in idea then I see the idea generated ScratchBuilder.build is:

    public Scratch build() throws IOException {
        return new Scratch(attr, attr2);
    }

It is strange, there is a throws in the build method although the generated all argument constructor does not throw anything. If I simply remove the throws clause from the build method the Idea stops complaining.

Why does idea think that there should be a throws in the build method?

How to&Answers:

Can you initialize it and then assign to Builder, like this;

Scratch sc = Scratch.builder().attr("1").attr2("2").build();