Intellij 警告:从不使用该方法的返回值

2022-09-02 00:42:25

我有一些代码似乎没问题,但是Intellij IDEA警告了它的许多方法,这些方法将return

从不使用方法的返回值


下面是实际代码,一个生成器类。

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);
    }

代码是否有问题,也许我错误地实例化了.build()方法?我的 StreamParser 构造函数的代码:

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;
}

有没有更好的方法来实现这一点?如果代码正常,导致此警告的原因是什么?

编辑:使用 StreamParserBuilder,调用 withX 函数:

 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

“从不使用方法的返回值”是来自检查的警告。生成此警告的原因是,此方法返回的值永远不会在调用站点上使用。可以通过在 中使用 对类进行批注或禁用检查来忽略警告。Java | Declaration redundancy | Method can be void@SuppressWarnings("UnusedReturnValue")Settings | Editor | Inspections


答案 2

在您的示例中,您并没有真正从生成器模式中受益。如果这是罕见的用例,我会让它成为,并忽略警告。但是,如果频繁,则直接向构建器包含条件赋值可能是有益的。所以你可以写:

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

这意味着要复制所有生成器方法。

或者你可以引入流利的介词:

streamBuilder.when(isSpanOneReplaced).setSpanLimit1(spanLimit1)

如果您的构建器是界面,则实现将非常简单。

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

您的方法可以通过粘贴以下方法调用(如果不是)并返回 oeiginal 生成器来返回代理。when()build()


推荐