1

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)

G-Crow
  • 11
  • 4
  • This is probably an issue with sonarlint not grasping your code. That might be because the *erasure* of `T` is `Comparable` which doesn't guarantee serializablilty, so if you ignore compiler warnings you *could* constructan unserializable `Pair`. But mostly this is just a false positive. – Joachim Sauer Oct 09 '20 at 08:37
  • If you change sequence of `Comparable` and `Serializable` to make it like `T extends Serializable & Comparable super T>`, sonar doesn't give warning about `first` and `last` not being serializable. I am not sure about the reason behind that. I couldn't find such rule. There might be some possible issue with Sonar. – Viral Lalakia Oct 09 '20 at 09:16
  • @ViralLalakia: that probably works because it changes the erasure (i.e. the actual underlying type used by the JVM in the absence of runtime generic information) of `T` from `Comparable` to `Serializable`. – Joachim Sauer Oct 09 '20 at 09:27

1 Answers1

-3

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

}

  • 3
    What is this meant to improve? When posting answers, please *describe* what you're doing and don't just post code only. – Joachim Sauer Oct 09 '20 at 09:27