Where does annotation processing take place?

I read about annotations recently and I'm a little confused. I used the @NotNull annotation, but I really don't know how it checks to see if a variable is null. No, where in the code I do not see anything, checking the values ​​for null. This makes sense because it is an interface, so where does the actual processing take place and why is this not indicated in the code? The examples I saw usually just make an annotation that takes on values ​​but doesn't do anything, so I'm confused as to where the implementation happens.

+4
source share
1 answer

Annotations are just metadata, nothing more. When you want to provide some information about a class, you put annotations on it. Think of them (to some extent) as an alternative to the old well-known XML way of defining metadata.

Now, it is obvious that someone is reading your XML and running code that does something with metadata. The same thing happens with annotations: the structure to which the annotation belongs is responsible for reading the annotation and creating something with this information. In the case of @NotNull , its hibernate-validator project. The java open API allows access to information in annotations by reflection (classes such as java.lang.Class , Method , Field , etc.). So somewhere inside the hibernate validator there is code that goes into your class, reads the annotations by reflection, and checks if the class adheres to these annotations. These annotations usually have a “temporary” retention policy, which means they are stored in bytecode and loaded with the class that contains these annotations.

There are also annotations that should be handled by the Java compiler. For example @Deprecated , @SuppressWarnings , etc. The benefit of this annotation is that you may find some problems with the code at compile time.

You can also place annotation handlers and “hook” them at the compilation stage, but has a completely different story.

Hope this clarifies the use of annotations a bit

+5
source

Source: https://habr.com/ru/post/1393583/


All Articles