永远不会使用该方法的返回值

永远不会使用该方法的返回值

本文介绍了Build Pattern Intellij警告:永远不会使用该方法的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个简单的构建器模式-下面的代码。代码会执行并运行,但是builder类中的每个 with ..方法都会显示警告,提示从未使用过该方法的返回值

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

代码是否存在问题,也许我已经实例化了.build ()方法不正确?我的StreamParser构造函数的代码:

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?

编辑:StreamParserBuilder的用法,调用withX函数:

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


推荐答案

从未使用过方法的返回值 Java发出警告声明冗余|方法可以作废检查。生成此警告的原因是,从未在调用站点使用此方法返回的值。可以通过使用 @SuppressWarnings( UnusedReturnValue)注释类或在 Settings |中禁用检查来忽略警告。编辑器检查

"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.

这篇关于Build Pattern Intellij警告:永远不会使用该方法的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:38