Annotation processor is a tool build in javac for scanning and processing annotations at compile time.
Annotations are a class of metadata that can be associated with classes, methods, fields, and even other annotations.There are two ways to access these annotations at runtime via reflection and at compile time via annotation processors.
Here @NonNull is annotation which is processed compile time by the android studio to warn you that the particular function needs non null parameter.
Creating and Using Custom Annotations
For creating custom annotations we need to decide
- Target - on which these annotations will work on like field level, method level, type level etc.
- Retention - to what level annotation will be available.
For this, we have built in custom annotations. Check out these mostly used ones:
Creating Custom Annotation
Using Custom Annotation
the value provided inside
@CustomAnnotation will be consumed by an Annotationprocessor may be to generate code at compile time etc.
Types of Annotations
There are three types of annotations.
Marker Annotation - annotation that has no method
Single-Value Annotation - annotation that has one method
Multi-Value Annotation - annotation that has more than one method