java - Intellij 警告 : Return value of the method is never used

标签 java intellij-idea return-value

我有一些代码看起来没问题,但是 Intellij IDEA 警告它的许多方法返回

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 构造函数的代码:

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

最佳答案

“从未使用该方法的返回值”是来自Java | 的警告。声明冗余 |方法可以用void检查。生成此警告是因为调用站点从未使用此方法返回的值。可以通过使用 @SuppressWarnings("UnusedReturnValue") 注释类或在 Settings | 中禁用检查来忽略警告编辑|检查

关于java - Intellij 警告 : Return value of the method is never used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48168078/

相关文章:

java - 一对多查询jpql

intellij-idea - 如何在 JetBrains IDE 中生成自定义 "todo"标签?

java - 我在 IntelliJ IDEA 中使用 Artifacts 有什么用?

c - 有意义的函数返回值的常用方法是什么?

java - 需要多个 return 语句的函数

jquery - 如何使用 jQuery 仅返回 id 的一部分?

java - 如何在pom.xml中使用两个版本的itext jar文件?

java - 所有类如何继承自Object?

java - for 循环中嵌套 if 语句 [JAVA/LibGDX]

java - 将方法参数重构为其成员函数的有效方法?