29

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.

Deep
  • 929
  • 2
  • 16
  • 32
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43

4 Answers4

20

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.

dimo414
  • 47,227
  • 18
  • 148
  • 244
Seshagiri
  • 728
  • 5
  • 10
  • 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 :/ – Blood-HaZaRd May 07 '12 at 13:46
  • 1
    Please go through this link http://www.javabeat.net/2007/06/introduction-to-java-6-0-new-features-part-i/ – Seshagiri May 07 '12 at 13:56
  • 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. – Blood-HaZaRd May 07 '12 at 14:00
  • 3
    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. – David May 07 '12 at 14:00
  • @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. – Seshagiri May 07 '12 at 14:02
  • 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. – Blood-HaZaRd May 07 '12 at 14:05
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.

dimo414
  • 47,227
  • 18
  • 148
  • 244
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.

Erik van Oosten
  • 1,541
  • 14
  • 16
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.

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22