Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Generated Annotation, how do we use it?

Tags:

I recently read an article talking about the Java annotations, and on this latter comes the @Generated one. They say that it is used for automatically generate code.

Could someone explain me that in further with a little example ?

All what i found on the net was some pro question or something beyond what i was looking for.

like image 393
Blood-HaZaRd Avatar asked May 07 '12 13:05

Blood-HaZaRd


People also ask

What does @generated annotation do?

@generated annotation is kind of bookmark to indicate the respective code is autogenerated by tools. So that if any IDE who respects it may block the code to edit.

What does Lombok @generated do?

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.

How are annotations used?

Annotations have a number of uses, among them: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.

How do annotation processors work?

As we briefly mentioned, annotations processors are typically used to inspect the codebase against the presence of particular annotations and, depending on use case, to: generate a set of source or resource files. mutate (modify) the existing source code. analyze the exiting source code and generate diagnostic messages.


2 Answers

As per the JavaDoc:

The Generated annoation 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.

like image 152
Seshagiri Avatar answered Sep 16 '22 23:09

Seshagiri


@Generated is used by meta-programs such as Auto/Value which generate source code so you don't have to manually write it. If you're writing a .java file by hand (which is normally what one does), don't use @Generated.

like image 23
dimo414 Avatar answered Sep 17 '22 23:09

dimo414