Here you see some of the violations found in the Configuration class in the equals() method. Nearly each line has a violation. The problem is: equals() is a automatically generated method. Coding conventions violations in generated code are just useless. Generated code doesn’t has to be maintainable. It doesn’t has to be readable.
I thought about how to tell Sonar to ignore this code. One could use the //NOSONAR comment to make Sonar ignore lines. But you’d have to place it on every line. Or you could use
@SuppressWarnings("all")
but this would suppress all warnings, not only Sonar violations (reference).
Then I stumbled upon @Generated annotation which is part of Java since 1.6. Using this annotation, code generators could automatically mark generated code, making life easier for code analyzers and developers. So in a perfect world my Eclipse code generator would generate this method:
@Override @Generated("Eclipse source generator") public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final ConfigurationBase other = (ConfigurationBase) obj; if (autoRefreshDatabaseEnabled != other.autoRefreshDatabaseEnabled) return false; .... }
and all code analyzers would magically ignore this method.
This idea was already picked up by the sonar team but sadly not yet implemented.
Keine Kommentare:
Kommentar veröffentlichen