Search code examples
eclipseannotationssonarqubegenerated-code

How can I get Eclipse to mark generated code like equals to prevent code quality tooling issues?


Generated code tends to flag up in our SONAR code quality tooling for things like cyclomatic complexity. It seems that it is a recognised problem and there is support for annotation-based suppression.

I don't want to have to manually edit or annotate this code to prevent these warnings. How can I change the generated code produced by Eclipse to include say a @Generated or @SuppressWarnings("all") annotation whenever it generates code for me?


Solution

  • I don't think there's a central place in Eclipse config where you can specify some flags that should be added when Eclipse generates code for you.

    The only way to kind-of achieve what you want is to modify each code templates of "Java > Editor > Templates" and surround them with some flags like "// SONAR-OFF" and "// SONAR-ON", and to configure the Sonar Switch-Off Violations plugin accordingly.

    But:

    • This is quite a pain to modify all those templates (even if you'll do it only once)

    • This won't work for advanced code generation like for "equals" or "hashCode" method