Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to auto complete a builder in Intellij?

For example, I have a class with a builder with 5 params, instead of me manually selecting the params and populating them, is there a way to tell Intellij to do this:

MyClass myClass = MyClass.builder()
                   .param1()
                   .param2()
                   .param3()
                   .param4()
                   .param5()
                   .build();

Then I can just fill in the parameters myself. It'd be handy to make sure I haven't missed any.
Alternatively, can I set the autocomplete options to sort in the order they appear in the class?

like image 473
mal Avatar asked Dec 02 '18 12:12

mal


1 Answers

Even if there is such a plugin I believe it would not help you really (because you still need to manually click on every method call and provide parameter) and you should definitely not use IDE to protect you from not setting all mandatory parameters.

In this case you this either put all mandatory params to the constructor or provide a validation inside build method. Something like

public MyClass build() {
    if(param1 == null) {
        // handle param1 null value
    }
    if(param2 == null) {
       // etc
    }
    ...
}
like image 98
m.antkowicz Avatar answered Nov 17 '22 13:11

m.antkowicz