Build Pattern Intellij warning: Return value of the method is never used

北慕城南 提交于 2019-12-10 12:40:52

问题


I've implemented a simple builder pattern - code below. The code executes and runs but every 'with..' method in the builder class displays a warning with 'Return value of the method is never used'

public static class StreamParserBuilder{
    //optional - have defaults:
    private long spanLimit1 = 2000L;
    private long spanLimit2 = 100000L;
    private long spanLimit3 = 3000000L;
    private String[] coordinates = {"L1", "R2"};
    private String outputDirectory = System.getProperty("user.dir");
    private boolean isLastSteam = false;

    //required from the builder.
    private String[] args;
    private String inputFile;
    private String streamData;
    private boolean isPaired;

    public StreamParserBuilder(String[] args, String inputFile, String streamData, boolean isPaired){
        this.args = args;
        this.inputFile = inputFile;
        this.streamData = streamData;
        this.isPaired = isPaired;
    }

    public StreamParserBuilder withSpanLimit1(long spanLimit1){
        this.spanLimit1 = spanLimit1;
        return this;
    }

    public StreamParserBuilder withSpanLimit2(long spanLimit2){
        this.spanLimit2 = spanLimit2;
        return this;
    }

    public StreamParserBuilder withSpanLimit3(long spanLimit3){
        this.spanLimit3 = spanLimit3;
        return this;
    }

    public StreamParserBuilder withCoordinates(String[] coordinates){
        this.coordinates = coordinates;
        return this;
    }

    public StreamParserBuilder withOutputDirectory(String outputDirectory){
        this.outputDirectory = outputDirectory;
        return this;
    }

    public StreamParserBuilder isLastStream(boolean isLastSteam){
        this.isLastSteam = isLastSteam;
        return this;
    }

    public StreamParser build(){
        return new StreamParser(this);
    }

Is there an issue with the code, maybe i've instantiated the .build() method incorrectly? The code for my StreamParser constructor:

private StreamParser(StreamParserBuilder streamParserBuilder){
    this.args = streamParserBuilder.args;
    this.inputFile = streamParserBuilder.inputFile;
    this.streamData = streamParserBuilder.streamData;
    this.spanLimit1 = streamParserBuilder.spanLimit1;
    this.spanLimit2 = streamParserBuilder.spanLimit2;
    this.spanLimit3 = streamParserBuilder.spanLimit3;
    this.coordinates = streamParserBuilder.coordinates;
    this.outputDirectory = streamParserBuilder.outputDirectory;
    this.isLastStream = streamParserBuilder.isLastSteam;
    this.isPaired = streamParserBuilder.isPaired;
}

Is there a better way to implement this? If the code is okay, what causes this warning?

Edit: Usage of the StreamParserBuilder, calling the withX functions:

 StreamParserBuilder streamBuilder = new StreamParserBuilder(args, inputFile, stream, isPaired);
        if (isSpanOneReplaced) streamBuilder.withSpanLimit1(spanLimit1);
        if (isSpanTwoReplaced) streamBuilder.withSpanLimit2(spanLimit2);
        if (isSpanThreeReplaced) streamBuilder.withSpanLimit3(spanLimit3);
        if (areCoordinatesReplaced) streamBuilder.withCoordinates(coordinates);
        if (isOutputDirectoryReplaced) streamBuilder.withOutputDirectory(outputDirectory);
        if (streamCount == streamData.size()) streamBuilder.isLastStream(true);
        StreamParser streamParser = streamBuilder.build();

回答1:


"Return value of the method is never used" is a warning from the Java | Declaration redundancy | Method can be void inspection. This warning is generated because the value returned by this method is never used at the call site. It's possible to ignore the warning by annotating the class with @SuppressWarnings("UnusedReturnValue") or disabling the inspection in Settings | Editor | Inspections.




回答2:


Better design would be if StreamParser doesn't know anything about its builder. I mean, constructor StreamParser doesn't take builder as parameter. Better usage is:

StreamParserBuilder builder = new StreamParserBuilder(params)
                                    .withSpanLimit1(3)
                                    .withSpanLimit2(4);
StreamParser parser = builder.build();
// after that you can continue building object and create another parser
builder.withSpanLimit3(4);
StreamParser anotherParser = builder.build();



回答3:


The reason the with methods of a builder return this; is so method calls can be chained:

StreamParserBuilder streamBuilder = new StreamParserBuilder(args, inputFile, stream, isPaired)
    .withSpanLimit1(spanLimit1)
    .withSpanLimit2(spanLimit2)
    .withSpanLimit3(spanLimit3);

In your case, you're ignoring the returned value in your calling code:

// returns StreamBuilderParses, which you're ignoring
if (isSpanOneReplaced) streamBuilder.withSpanLimit1(spanLimit1);



回答4:


In your example, you don't really benefit from the builder pattern. If it's rare use case, I'd just let it be, and ignore the warnings. But if it's frequent, it might be beneficial to include conditional assignment directly to your builder. So you may write either:

streamBuilder.conditionallyWithSpanLimit1(isSpanOneReplaced, spanLimit1)
    .conditionallyWithSpanLimit2(isSpanTwoReplaced, spanLimit2)
    // etc.

That would mean to duplicate all builder methods.

Or you can introduce fluent preposition:

streamBuilder.when(isSpanOneReplaced).setSpanLimit1(spanLimit1)

Implementation of this would be extremely simple, if your builder is interface.

public interface Builder {
    Builder setSpanLimit1(int value);
    Builder when(boolean condition);
    Object build();
}

Your when() method can return a proxy bypasing following method call, if it's not build() and returning back the oeiginal builder.



来源:https://stackoverflow.com/questions/48168078/build-pattern-intellij-warning-return-value-of-the-method-is-never-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!