klee-contrib / topmodel

Outil de modélisation et générateurs pour divers langages
https://klee-contrib.github.io/topmodel/#/
MIT License
9 stars 9 forks source link

[JAVA] SonarQube règles java:S2057 and java:S2162 #381

Closed kbuntrock closed 3 weeks ago

kbuntrock commented 3 weeks ago

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 {
[...]
}

Après cohérent avec mon projet :

public static class XXXId {
[...]
}

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 :

public boolean equals(Object o) {
    if(!(o instanceof ContactRolePropositionCommercialeId oId)) {
        return false;
    }
    return !(!this.contact.equals(oId.contact)
        || !this.rolePropositionCommerciale.equals(oId.rolePropositionCommerciale));
}
public boolean equals(Object o) {
    if(o == this) {
        return true;
    }
    if(o == null) {
        return false;
    }
    if(this.getClass() != o.getClass()) {
        return false;
    }

    return Objects.equals(this.contact, ((ContactRolePropositionCommercialeId) o).contact) &&
        Objects.equals(this.rolePropositionCommerciale, ((ContactRolePropositionCommercialeId) o).rolePropositionCommerciale);
}