Generated code annotated with @Generated get considered as duplicated

Hi, I am maintainer of a plugin for IntelliJ IDE which is a code generator for step builders pattern in JAVA)

The step builders classes and code generated are annotated with @javax…Generated annotation.

I would like to know if it would be possible to find a way so that the generated code (Annotated with @Generated) do not get considered as duplicated code by sonarqube ?

The problem is that the end user can nothing for that… it’s code generated by the IDE and in builders it is quite common to have duplicated code between two builders having a property named the same.

Do you know if there is a way to make both world be happy together ? :slight_smile:

Thank you

Hi,

For issues (from the docs):

You can ignore all issues on specific blocks of code, while continuing to scan and mark issues on the remainder of the file.

But sub-file exclusions aren’t available for duplications. I don’t suppose we’re talking about entire files that should be ignored, are we?

 
Ann

Hi,

Thank you for your answer.

I tried to add @SuppressWarnings("common-java:DuplicatedBlocks") to the inner interfaces that my code generator generate but no success sonar still see it as duplicated code.

But shouldn’t sonar honor the @Generated annotation and considers that a generated code out of the hands of the developer should not be analyzed ?

Here is the javadoc from javax.annotation.Generated:

The Generated annotation is used to mark source code that has been generated. It can also be used to differentiate user written code from generated code in a single file.

Hi,

Unfortunately that’s not likely to work for these particular issues. Rules from ‘common’ repositories are supplied by - and calculated at - the server. On the other hand, language-specific issue suppression such as @SuppressWarnings is handled analysis side by the language analyzer.

This means that the duplications issue won’t have been raised yet when that issue suppression would run.

That said, when I gave my earlier answer, I thought we were talking about duplications metrics & markers, not duplications issues. Since we are talking about issues (right?) then exclusion becomes an option again. What you want here is multi-criteria exclusion on a path that covers your generated files and a key of common-java:DuplicatedBlocks.

 
HTH,
Ann

Hi,

That seems indeed a viable alternative. I will add an option to my plugin so that it can generate a standalone StepBuilder.java class (This way it’s easy to add an exclusion on sonar cpd for */**StepBuilder.java)

Thank you Ann :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.