Sonar nous remonte deux règles pertinentes sur les classes générées représentants des clés composites :
• Rule ID: java:S2057
"Serializable" classes should have a "serialVersionUID"
A serialVersionUID field is strongly recommended in all Serializable classes. If you do not provide one, one will be calculated for you by the compiler. The danger in not explicitly choosing the value is that when the class changes, the compiler will generate an entirely new id, and you will be suddenly unable to deserialize (read from file) objects that were serialized with the previous version of the class.
serialVersionUID's should be declared with all of these modifiers: static final long.
Dans notre configuration de projet, implémenter l'interface Serializable n'est pas nécessaire / utilisé. Nous pouvons donc complètement nous en séparer et ne pas avoir à gérer de serialVersionUID. Mais potentiellement d'autres projets souhaitent une autre configuration.
Exemple avant :
public static class XXXId implements Serializable {
[...]
}
A key facet of the equals contract is that if a.equals(b) then b.equals(a), i.e. that the relationship is symmetric.
Using instanceofbreaks the contract when there are subclasses, because while the child is an instanceofthe parent, the parent is not an instanceofthe child. For instance, assume that Raspberry extendsFruitand adds some fields (requiring a new implementation of equals) [...]
Sonar nous remonte deux règles pertinentes sur les classes générées représentants des clés composites :
• Rule ID: java:S2057
"Serializable" classes should have a "serialVersionUID"
A serialVersionUID field is strongly recommended in all Serializable classes. If you do not provide one, one will be calculated for you by the compiler. The danger in not explicitly choosing the value is that when the class changes, the compiler will generate an entirely new id, and you will be suddenly unable to deserialize (read from file) objects that were serialized with the previous version of the class. serialVersionUID's should be declared with all of these modifiers: static final long.
Dans notre configuration de projet, implémenter l'interface Serializable n'est pas nécessaire / utilisé. Nous pouvons donc complètement nous en séparer et ne pas avoir à gérer de serialVersionUID. Mais potentiellement d'autres projets souhaitent une autre configuration.
Exemple avant :
Après cohérent avec mon projet :
Règle complète ici : https://sonar-interne.dev.klee.lan.net/coding_rules?open=java%3AS2057&rule_key=java%3AS2057
• Rule ID: java:S2162
Compare to "this.getClass()" instead.
A key facet of the equals contract is that if a.equals(b) then b.equals(a), i.e. that the relationship is symmetric. Using instanceofbreaks the contract when there are subclasses, because while the child is an instanceofthe parent, the parent is not an instanceofthe child. For instance, assume that Raspberry extendsFruitand adds some fields (requiring a new implementation of equals) [...]
Règle complète disponible ici : https://sonar-interne.dev.klee.lan.net/coding_rules?open=java%3AS2162&rule_key=java%3AS2162
Exemple de code généré à ce niveau actuellement :