0

I'm using Eclipse 2020-03 (4.15.0) with Sonarlint for Eclipse 5.1.0.17086 and I get , IMO, false positive S1854 warnings in the following code (taken from the book "Java 8 In Action"). Working with Java OpenJDK 13.0.2. This is not a showstopper since I am merely studying Java 8 techniques. I just want to understand why these lines are flagged...

package nl.paul.forkjoin;

import java.util.concurrent.RecursiveTask;

public class ForkJoinSumCalculator extends RecursiveTask<Long> {
private static final long serialVersionUID = 1L;

private final long[] numbers;
private final int start;
private final int end;

public static final long THRESHOLD = 10_000;

public ForkJoinSumCalculator(long[] numbers) {
    this(numbers, 0, numbers.length);
}

private ForkJoinSumCalculator(long[] numbers, int start, int end) {
    this.numbers = numbers;
    this.start = start;
    this.end = end;
}

@Override
protected Long compute() {
    int length = end - start; //SonarLint S1854 warning
    if (length <= THRESHOLD) {
        return computeSequentially();
    }
    ForkJoinSumCalculator leftTask = new ForkJoinSumCalculator(numbers, start, start + length / 2); //SonarLint S1854 warning
    ForkJoinSumCalculator rightTask = new ForkJoinSumCalculator(numbers, start + length / 2, end); //SonarLint S1854 warning
    leftTask.fork();
    return leftTask.join() + rightTask.compute();
}

private long computeSequentially() {
    long sum = 0;
    for (int i = start; i < end; i++) {
        sum += numbers[i];
    }
    return sum;
}
}

Did I miss something here? Both "length", "leftTask" and "rightTask" are used several times in the code...

Above class is tested with the following class:

package nl.paul.forkjoin;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class ForkJoinTest {

    public static void main(String[] args) {
        System.out.println(new ForkJoinTest().doIt(10_000_000));
    }

    private long doIt(long n) {
        long[] numbers = LongStream.rangeClosed(0, n).toArray();
        long start = System.currentTimeMillis();
        ForkJoinTask<Long> task = new ForkJoinSumCalculator(numbers);
        long result = new ForkJoinPool().invoke(task);
        long finish = System.currentTimeMillis();
        System.out.println("Processing took " + (finish - start) + " msec.");
        return result;
    }
}
PaulN
  • 63
  • 2
  • 10

2 Answers2

0

I did a brief survey about Warning S1854 on Sonar and saw several issues created on Github and on the Sonar website itself, apparently it was fixed in February/2020. Check if your Sonar is up to date, and if it is, the error probably hasn't been fixed :(

https://jira.sonarsource.com/browse/SONARJAVA-3281

  • Thanks for your survey. I checked in Eclipse for SonarLint updates. There were none. I also checked your link. It handles false positives in lambda code, which is not the case in my code. We'll see... – PaulN May 13 '20 at 11:52
0

I finally solved the problem, I was set on the right track by a post on the sonarlint forum (https://community.sonarsource.com/t/java-s2166-rule-not-working/22943/15). My projects were working with Java 13 while my Windows cmd "Java -version" returned Java 1.8 as the running Java version. This mismatch (Eclipse starting up with Java 1.8 and the project working with Java 13) caused the SonarLint FP's.

PaulN
  • 63
  • 2
  • 10