首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Intellij警告:从未使用过方法的返回值

Intellij警告:从未使用过方法的返回值
EN

Stack Overflow用户
提问于 2018-01-09 12:17:29
回答 5查看 17.1K关注 0票数 22

我有一些看起来不错的代码,但是Intellij警告说,它的许多方法都是return所看重的

方法的返回值从未被使用过。

下面是一个构建器类的实际代码。

代码语言:javascript
运行
复制
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构造函数的代码:

代码语言:javascript
运行
复制
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函数:

代码语言:javascript
运行
复制
 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();
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-01-09 20:23:35

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

票数 46
EN

Stack Overflow用户

发布于 2018-04-11 16:28:49

构建器with方法return this;的原因是方法调用可以链接起来:

代码语言:javascript
运行
复制
StreamParserBuilder streamBuilder = new StreamParserBuilder(args, inputFile, stream, isPaired)
    .withSpanLimit1(spanLimit1)
    .withSpanLimit2(spanLimit2)
    .withSpanLimit3(spanLimit3);

在您的示例中,您忽略了调用代码中的返回值:

代码语言:javascript
运行
复制
// returns StreamBuilderParses, which you're ignoring
if (isSpanOneReplaced) streamBuilder.withSpanLimit1(spanLimit1);
票数 1
EN

Stack Overflow用户

发布于 2018-08-10 12:27:21

在您的示例中,您并不真正受益于构建器模式。如果这是罕见的用例,我只会听之任之,而忽略警告。但是,如果这种情况经常发生,那么直接将条件赋值包含到您的构建器中可能是有益的。所以你可以写:

代码语言:javascript
运行
复制
streamBuilder.conditionallyWithSpanLimit1(isSpanOneReplaced, spanLimit1)
    .conditionallyWithSpanLimit2(isSpanTwoReplaced, spanLimit2)
    // etc.

这意味着复制所有构建器方法。

或者你可以介绍一个流利的介词:

代码语言:javascript
运行
复制
streamBuilder.when(isSpanOneReplaced).setSpanLimit1(spanLimit1)

如果您的构建器是接口,那么实现这一点将是非常简单的。

代码语言:javascript
运行
复制
public interface Builder {
    Builder setSpanLimit1(int value);
    Builder when(boolean condition);
    Object build();
}

您的when()方法可以返回一个代理,如果它不是build()并返回oeiginal构建器,则可以绕过下面的方法调用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48168078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档