12

In my project codebase, I see @Generated annotation being used in many places. When I read through the docs it states the following.

Lombok will eventually automatically add this annotation to all generated constructors, methods, fields, and types.

As per the docs, @Generated annotation is automatically added by Lombok for the generated code. Apart from that, @Generated doesn't generate any code like @Getter or @Setter does. I don't see the rationale in using this.

Am I missing something here?

Andy
  • 5,433
  • 6
  • 31
  • 38
  • Not really sure, but maybe it specifies that the targets will be generated automatically and not provided explicitly. – Debargha Roy Jul 31 '20 at 06:07
  • As the documentation states, it is for you to know that this is a lombok generated method/field/etc. This helps the IDE to identify the generated classes and warn you from modifying them. – Siddharth Agrawal Aug 01 '20 at 13:07

1 Answers1

25

I came across one valid use case for @Generated. So I thought I will update here hoping it might help someone sometime.

In our project, we use Jacoco to generate a coverage report. There are a few classes and methods that don't make sense to be a part of the coverage report. Eg: configuration classes, POJOs, close() etc.

Classes can be excluded from the report by using the <exclude> tag in POM.xml. But If you would like to ignore only one method from a class, you can't achieve that by adding entries in pom.xml.

In scenarios like that, we can use @Generated annotation on methods to be excluded from the coverage report. Cheers.

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
Andy
  • 5,433
  • 6
  • 31
  • 38
  • Does anybody has a custom annotation to achieve this? It's kinda an overkill using lombok just to exclude classes from Jacoco – mayid Jun 23 '22 at 12:50
  • 1
    I've found it here: https://stackoverflow.com/a/66918619/3001897. And you can change FUNCTION with CLASS to convert it to class level annotation – mayid Jun 30 '22 at 17:01