JavaRush /Java Blog /Random EN /Annotations. Part one, a bit boring

Annotations. Part one, a bit boring

Published in the Random EN group
First part. I wrote very briefly about annotations with the SOURCE and CLASS types. Worth reading so as not to get lost in the second part and to begin to "misunderstand" a little more =) There will definitely be at least one word that you know!
Annotations.  Part one, a bit boring - 1
The first time I saw them in the problems here and somehow did not notice. Well, Override is hanging out, IDEA wrote it, so it should be so. Over time, I realized that everything is much deeper. While you are learning, annotations seem to be something useless, but necessary. You don't know why they do what they do. It seems that I read a couple of articles, they said “how great it is that now we have annotations, everything has become so simple.” But I did not know how it was before, and did not understand that now it has become easier. Now I know and want to tell a little. There are 3 types of annotations (RetentionPolicy):
  • SOURCE - compiler annotations
  • CLASS - the data from the annotation will be written to the bytecode but not available during operation. They write that in the standard library a lot of annotations use this kind, and now it is kept due to backward compatibility. Used for very specific tasks.
  • Question and Answer on StackOverflow
  • RUNTIME - the most popular, used while the code is running.
Since part of the article was taken up by the introduction, I will write here about SOURCE and CLASS annotations. Here are some annotations I could find (thanks to problem 3607). I do not write Runtime, there are too many of them and not the topic of the article. SOURCE:
  • java/lang/annotation/Native.class;
  • java/lang/SuppressWarnings.class
  • javax/annotation/Generated.class
  • ,java/lang/Override.class
class: I don't know why annotations with CLASS type are needed. Documentation for existing annotations could not be found, so I think we can just leave this baggage behind. But if you find it, please share. SOURCE annotations:
  1. Native - a variable under this annotation can refer to native code;

  2. SuppressWarnings - suppresses various compiler warnings;

  3. Generated - marks the source code that has been generated;

  4. Override - Checks if a method has been overridden.
More details:
Annotations.  Part one, a bit boring - 2
Native - never seen and never used. I think it's a rather rare annotation, because use it if you need to run code in another "native" language. I tried to find a clear reference to her, but failed.
Annotations.  Part one, a bit boring - 3
SuppressWarnings - Often used in the form of @SuppressWarnings("unchecked"). Used to suppress warnings that you are aware of. The example above suppresses warnings about unchecked type casting. Again, I met only in such a form, use.
Annotations.  Part one, a bit boring - 4
Generated - encountered now when, on assignment, you have to generate classes from xsd files. These 3 annotations are quite specific and most likely not of interest to you at the moment. I will describe the latter.
Annotations.  Part one, a bit boring - 5
Override - you use it all the time and it does a very useful thing. It's easy to make a mistake when overriding a method, unless IDEA does it. Mistakes or just mistakes, it happens. This annotation will make sure that the method in the parent class matches our (tagged) method. This guarantees us that the method will be overridden and not added. When refactoring the code, the method can be removed or changed. Again, the annotation will point you to the error. Without it, our method would simply be added.
Annotations.  Part one, a bit boring - 6
Boring? I would say yes, there is not much to learn from this article. Almost everything in it (90%) is a story about something that you will not use, or will, but very rarely. The remaining 10% is a hello and a description of the Override annotation, which at first glance is useless. Well, what do I think, the second part of the article will be more interesting. There will be RUNTIME annotations, and they interact with the code in real time and do black magic. Annotations. Part two. lombok.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION