# Annotation Processor
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.
# @NonNull Annotation
public class Foo {
private String name;
public Foo(@NonNull String name){...};
...
}
Here @NonNull is annotation which is processed compile time by the android studio to warn you that the particular function needs non null parameter.
# Types of Annotations
There are three types of annotations.
@interface CustomAnnotation {}
@interface CustomAnnotation {
int value();
}
@interface CustomAnnotation{
int value1();
String value2();
String value3();
}
# Creating and Using Custom Annotations
For creating custom annotations we need to decide
For this, we have built in custom annotations. Check out these mostly used ones:
@Target
@Retention
Creating Custom Annotation
@Retention(RetentionPolicy.SOURCE) // will not be available in compiled class
@Target(ElementType.METHOD) // can be applied to methods only
@interface CustomAnnotation{
int value();
}
Using Custom Annotation
class Foo{
@CustomAnnotation(value = 1) // will be used by an annotation processor
public void foo(){..}
}
the value provided inside @CustomAnnotation
will be consumed by an Annotationprocessor may be to generate code at compile time etc.