EDIT 2 : Problem solved. Props to Joachim Sauer and Viral Lalakia for their answers.
Solution : SonarLint won't make a warning if I swap the Comparable<T>
and the Serializable
for the type T
. The warning it gives should be considered a false positive.
Initial issue :
So, I have a generic class Pair<T>
that I want to be Serializable
and Comparable
.
Moreover, I want T
to be Comparable
(and I need it to be Serializable
for Pair<T>
to be Serializable too).
The class PairCoord
inherits from it, with T
being an Integer
.
I'm using SonarLint for code analysis, and I'm trying to force myself to follow every advice (especially highly important ones), and it keeps warning me about the generic class Pair
's attributes not being serializables, despite having marked them as so.
Here is what I did :
public class Pair<T extends Comparable<? super T> & Serializable> implements Comparable<Pair<T>>, Serializable {
private static final long serialVersionUID = 5797102044530257848L;
private T first;
private T last;
public Pair(T first, T last) {
this.first = first;
this.last = last;
}
public Pair() {
}
// And so on
}
public class PairCoord extends Pair<Integer> implements Serializable {
private static final long serialVersionUID = 8389593640798914292L;
public PairCoord(int first, int last) {
super(first, (last + 8) % 8);
// Since each of the 3 squares are loops, the node before n°0 is therefore n°-1
// Except that n°-1 doesn't exist, but (-1 mod 8) = 7
// The only issue is that % isn't a modulo, but the remain of the euclidean division
// So by adding 8 to "last", I make sure that the number in the operation is positive,
// and since for all x >= 0, % behaves like mod, I have my node number correct (between 0 and 7)
}
}
And I have SonarLint Critical Warnings on the first
and last
fields as them being not serializable, even though I marked them as so ('Fields in a "Serializable" class should either be transient or serializable')
What could I do to fix that (if it is possible) ?
(Edit 1 : typo)
private static final long serialVersionUID = 5797102044530257848L;
}
public class PairCoord extends Pair implements Serializable {
}