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