@Generated Annotation, how do we use it?
Asked Answered
S

5

30

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.

Slalom answered 7/5, 2012 at 13:35 Comment(2)
It's hard to answer. What's really your question ? How to use @Generated or how to use annotation in Java ?Thanatos
The first one, How to use the @Generated in JavaSlalom
L
21

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.

Loxodromic answered 7/5, 2012 at 13:41 Comment(6)
Does that mean if we build classes of our project (for instance) then we annotate one classeby @Generated, the JVM will generate the .class files except the one with the annotation or I m totaly out of scope :/Slalom
Please go through this link javabeat.net/2007/06/…Loxodromic
yeah i tried to post that example as a comment but did not worked. I know i ask too much but i really did not see what will be generated with in that example.Slalom
Per the javadoc, I get the impression the @Generated annotation is just a marker that indicates the code was auto-generated (think JaxB turning XSD into Java code) rather than hand written. It has elements to identify which generator created the code and at what data / time it was done. I suppose those might be handy so a generator could run again and notice that an older version had created that code and do some updates.Southworth
@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.Loxodromic
So I have no worry about zones bookmarked with @Generated !!! Thank you Seshagiri for your help. I think It's more clear now. thank you David too.Slalom
P
3

@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.

Phaeton answered 16/12, 2016 at 17:55 Comment(0)
H
1

Some code linters use the annotation to skip generated code. For example, it doesn't make sense to calculate cyclomatic complexity on generated code.

Hydrogenize answered 12/1, 2023 at 11:14 Comment(0)
R
0

Fox example are good and bad policies on the border between generated and written code. Way of thinking is (i belive) different in compiled (static) languages, nad interpreted / dynamic.

Worst is to modify generated code (will be lost at next generation, or next generation is then prohibited) Usually is accepted to derive (manual) class from generated, or generate class what extends core "manual" class. If someone know good policies in this area, please comment.

Rhizoid answered 5/9, 2015 at 14:39 Comment(0)
C
0

Just as the word saying, Some codes which software generated, it is rarely appear in our normal handwriting.

Cunctation answered 24/2 at 11:35 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Zareba

© 2022 - 2024 — McMap. All rights reserved.