热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

构建模式Intellij警告:从不使用方法的返回值

如何解决《构建模式Intellij警告:从不使用方法的返回值》经验,为你挑选了1个好方法。

我已经实现了一个简单的构建器模式 - 下面的代码.代码执行并运行,但构建器类中的每个'with ..'方法都会显示一条警告,其中"方法的返回值永远不会被使用"

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

Bas Leijdekk.. 20

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



1> Bas Leijdekk..:

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


推荐阅读
author-avatar
Ale__x小葡萄
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有