False positive in serializable rule - multiple bounds in parameter type

Hello everyone, I’m here to report an issue which I found while I was converting some Java classes to Serializable.

  • Rule: Fields in a “Serializable” class should either be transient or serializable (squid:S1948)
  • versions used: SonarLint 4.1 in Eclipse and Sonarlint 4.1.0.3312 in IntelliJ IDEA Community Edition
  • minimal code sample to reproduce:
import java.io.Serializable;
import java.util.ListIterator;

public class CustomIteratorWrapper<T extends ListIterator<Object> & Serializable> implements Serializable  {
	private T listIterator;
	public CustomIteratorWrapper(T listIterator) {
		this.listIterator = listIterator;
	}

	public Object getNext() {
		return listIterator.next();
	}

	public boolean reachedEnd() {
		return !listIterator.hasNext();
	}

}

In the code above, SonarLint displays a warning, informing that the listIterator field should be transient or serializable.

If we switch the order of the bounds in the type parameter, the warning is not shown:

import java.io.Serializable;
import java.util.ListIterator;

public class CustomIteratorWrapper<T extends Serializable & ListIterator<Object>> implements Serializable  {
	private T listIterator;
	public CustomIteratorWrapper(T listIterator) {
		this.listIterator = listIterator;
	}

	public Object getNext() {
		return listIterator.next();
	}

	public boolean reachedEnd() {
		return !listIterator.hasNext();
	}

}

Hello Ramon, thanks for the feedback.

Sorry for the late reply, these past few months, we worked hard on SonarJava front-end migration, we were hoping to fix at the same time the kind of FP that you are reporting.
However, this one is still present on the current version, I created a ticket to track the issue.

Best,
Quentin