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?
The problem described in the question is a bug in the lombok-intellij-plugin. I reported the bug in github and Michail Plushnikov accepted the bug report and corrected the bug in lombok-intellij-plugin. See https://github.com/mplushnikov/lombok-intellij-plugin/issues/740 for details