I am trying to understand how the annotation understands what to do when we use the annotation. I am not talking about the behaviors like when to execute that is covered by Retention, values etc. I want to understand how annotations understand the rules for that annotation. For example how does @Override annotation knows how to check if the function overrides a method in super class and so on. I tried digging a lot and I reached here but I don't get where the rules for the annotations are written. It feels like magic to me.
-
https://dzone.com/articles/how-annotations-work-java – Lorelorelore Jul 02 '21 at 06:53
-
1Annotations are like a label that says "Fragile" or "Urgent" on a delivery package. In itself, it does nothing. A package handler reads it and treats the package accordingly. It's the annotation processor that knows what to do on the annotation. – ernest_k Jul 02 '21 at 06:53
-
Rightly said by @ernest_k, you are after annotation processors. – KnockingHeads Jul 02 '21 at 07:10
-
Related: https://stackoverflow.com/q/18189980/1189885 – chrylis -cautiouslyoptimistic- Jul 02 '21 at 07:25
-
Annotations as a language construct are described by §9.6 and §9.7 of the [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se16/html/index.html). Annotation representation in the class file is described by §3.15 of the [Java Virtual Machine Specification](https://docs.oracle.com/javase/specs/jvms/se16/html/index.html). _Compile-time_ annotation processing is described by [javax.annotation.processing](https://docs.oracle.com/en/java/javase/16/docs/api/java.compiler/javax/annotation/processing/package-summary.html) and related packages. – Slaw Jul 02 '21 at 07:31
-
2Though note, if I'm not mistaken, annotations such as `@Override` are handled directly by the Java compiler rather than via a `Processor` implementation. If you want to implement _run-time_ annotation processing see [java.lang.reflect.AnnotatedElement](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/reflect/AnnotatedElement.html). And finally, if you want to see how everything is implemented in OpenJDK you can always look at the source code: https://github.com/openjdk/jdk – Slaw Jul 02 '21 at 07:37
1 Answers
As already commented, processors (e.g. the compiler) must interpret the annotations, but the running program can also read/use the annotations (e.g by reflection).
For example the @Override
annotation is used by the compiler, see it's documentation:
Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
- The method does override or implement a method declared in a supertype.
- The method has a signature that is override-equivalent to that of any public method declared in Object.
The @Override
annotation is part of the standard API, other annotations may be part of some framework (e.g .JUnit @Test
) or additional annotation processors, see Annotation Processing in javac
. The developer can also declare annotations, see Annotation Interfaces.
In other words:
an Annotation is just that, an annotation. It is like a tag or mark that can be added to some elements (e.g. to a method, class, ...). There is no rule or anything similar directly attached to it. But some tools, like the compiler, or even normal Java code (some framework/library or written by you) can read and handle that annotation as desired.
There are a couple of annotations in the Java Language Specification (JLS) which compilers are required to handle. The action for the @Override (as example) is coded in the compiler, to do as specified in the JLS. Same for @Deprecated.

- 135
- 1
- 3
- 9
-
My question is that where is the rule that says check if there is a parent class and check if the method has the same declaration are written. When I go to Override interface, I see that only definition for when to run the annotation but nothing on how Override able to understand what action to take and when. There should be a place where this is written whether using reflections or otherwise. I understand how to create a new annotation but I want to know where the rules for the current annotations are written to get a better understanding. – Rohith Joseph Oct 19 '21 at 04:35