Java annotations are a way of adding metadata to Java code. They are used to provide information to the compiler, the runtime, or other tools that process Java code. Annotations can be used for various purposes, such as:
Annotations are defined by using the @interface keyword, which is similar to the interface keyword but with a leading @ sign. An annotation can have elements that specify the values for the annotation. For example:
@interface Author This defines an annotation called @Author that has two elements: name and email. To use this annotation, we can write:
@Author(name = "John Doe", email = "john...@example.com")This annotates the class MyClass with the @Author annotation and provides the values for the name and email elements. The annotation can be accessed by tools that process Java code, such as IDEs, documentation generators, or annotation processors.
There are also some predefined annotations in Java that have special meanings. For example:
@Override: indicates that a method overrides a method from a superclass or an interface.@Deprecated: indicates that a class, method, or field is deprecated and should not be used.@SuppressWarnings: indicates that a compiler warning should be suppressed for a specific reason.@FunctionalInterface: indicates that an interface is intended to be a functional interface, which means it has only one abstract method and can be used as a lambda expression.To learn more about Java annotations and how to use them, you can refer to the following sources:
To create a custom annotation in Java, we need to follow these steps:
@interface keyword and provide the elements for the annotation.@Retention meta-annotation. This determines how long the annotation is retained by the Java compiler and runtime. The possible values are SOURCE, CLASS, and RUNTIME.@Target meta-annotation. This determines where the annotation can be applied, such as classes, methods, fields, parameters, etc.default keyword.@Documented, @Inherited, or @Repeatable.For example, let's create a custom annotation called @Test that can be used to mark test methods in a class. The annotation has one element called value that specifies the name of the test. The annotation has a retention policy of RUNTIME and a target of METHOD. The default value for the element is an empty string.
@Retention(RetentionPolicy.RUNTIME)To process custom annotations in Java, we need to use reflection or annotation processors. Reflection is a mechanism that allows us to inspect and manipulate the metadata of Java classes, methods, fields, and annotations at runtime. Annotation processors are tools that can generate code or resources based on annotations at compile time.
To use reflection to process custom annotations, we need to use the following steps:
Class object of the class that contains the annotations.getAnnotations(), getDeclaredAnnotations(), or getAnnotation(Class) methods to get the annotations of the class or its members.instanceof operator or the annotationType() method to check if an annotation is of a specific type.For example, let's use reflection to process our custom @Test annotation and print out the names of the test methods in a class called MyTestClass.
// Get the Class object of MyTestClassTo use annotation processors to process custom annotations, we need to use the following steps:
javax.annotation.processing.Processor interface and override its methods.@SupportedAnnotationTypes, @SupportedSourceVersion, and optionally @SupportedOptions.process() method, use the provided parameters to get access to the annotated elements and generate code or resources using a file manager or a code generator.META-INF/services/javax.annotation.processing.Processor and list your processor class name in it.