Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could I add custom annotation @Generator or other annotation to all class in Java #9752

Open
akash33 opened this issue Jun 11, 2021 · 5 comments

Comments

@akash33
Copy link

akash33 commented Jun 11, 2021

Description

I want to add custom annotation to all classes that is generated by openapi. Something like @Generated so Jacoco can exclude those classes. Currently generator does support adding annotation to model classes but I want to add it all the classes. I do see @javax.annotation.Generated on all classes but the retention policy is SCOPE so jacoco is not able to exclude those classes from the report.
Is there any functionality like this additionalModelTypeAnnotations: '@lombok.Generated' that supports adding annotations to all classes?

@mbert
Copy link

mbert commented Jul 28, 2021

I second this. Another use case is adding @SuppressWarnings annotations since the generated classes usually lead to lots of code warnings difficult to deal with in projects with zero warnings policy.

@baincd
Copy link

baincd commented Aug 8, 2021

I also think this feature would be extremely useful and valuable.

  • The generated code causes a lot of warnings. Add @SuppressWarnings("all") would make these warnings go away. I think there is a strong argument to just also include this annotation, but an option to add this specific annotation or to add custom annotations would also work.
  • There would also be value in having the ability to include @lombok.Generated on classes. This could be an option to add this specific annotation, or an option to add custom annotations

@TomasLukac
Copy link

Any updates on this? We are facing the same issue.

@MarvinVaillant
Copy link

Yep. I'd also be interested in a solution.

@mbert
Copy link

mbert commented Feb 21, 2025

For the record, there is a workaround which isn't pretty but does the job: maven-replacer-plugin. In order to add annotations (in this case: suppress warnings) you can proceed as follows:

<execution>
  <phase>process-sources</phase>
  <goals>
    <goal>replace</goal>
  </goals>
  <configuration>
    <filesToInclude>${project.build.directory}/generated-sources/**/*.java</filesToInclude>
    <replacements>
      <replacement>
        <!-- First remove existing @SuppressWarnings annotations in order to avoid duplicating them.-->
        <token>(@SuppressWarnings|@edu.umd.cs.findbugs.annotations.SuppressFBWarnings).*</token>
        <value></value>
      </replacement>
      <replacement>
        <!-- Now insert our new annotations.-->
        <!-- Note that we should really have a '^' here, but it does not work, see bug #98 (https://code.google.com/archive/p/maven-replacer-plugin/issues/
98).-->
        <token>public (class|enum|interface) </token>
        <value>@SuppressWarnings({ your-list-of-suppressions-here })
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings({ your-list-of-suppressions-here })
public $1 </value>
      </replacement>
    </replacements>
  </configuration>
</execution>

I think we all agree that an "official way" to do this would be better, but at least this gets the job done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants