Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Meta-annotations are annotations that can be applied to other annotations in Java. They provide metadata or configuration information for the annotation they are applied to. There are several built-in meta-annotations in Java that you can use to control the behavior and usage of your custom annotations. In this tutorial, we will cover some of the most commonly used meta-annotations in Java.
The @Target
meta-annotation is used to specify the types of Java elements that an annotation can be applied to. You can choose one or more ElementType
constants as the value for the @Target
annotation.
import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.FIELD}) public @interface MyAnnotation { }
In this example, the custom MyAnnotation
can only be applied to methods and fields.
The @Retention
meta-annotation is used to specify how long the annotation should be retained in the compiled class files. It can have one of the following RetentionPolicy
constants as its value:
RetentionPolicy.SOURCE
: The annotation is retained only in the source code and is not included in the compiled class files.RetentionPolicy.CLASS
: The annotation is included in the compiled class files but is not available at runtime.RetentionPolicy.RUNTIME
: The annotation is included in the compiled class files and is available at runtime through reflection.import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { }
In this example, the custom MyAnnotation
is retained at runtime.
The @Inherited
meta-annotation indicates that an annotation should be inherited by subclasses of the annotated class. If a class is annotated with an @Inherited
annotation, its subclasses will also be considered as annotated with that annotation, unless they have their own annotation of the same type.
import java.lang.annotation.Inherited; @Inherited public @interface MyAnnotation { }
The @Documented
meta-annotation is used to indicate that the annotation should be included in the generated JavaDoc documentation. When an element is annotated with a @Documented
annotation, the annotation will be part of the element's JavaDoc.
import java.lang.annotation.Documented; @Documented public @interface MyAnnotation { }
You can use multiple meta-annotations together in your custom annotation definition.
import java.lang.annotation.*; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface MyAnnotation { }
In this example, the custom MyAnnotation
is applied to types (classes, interfaces, enums, etc.), is retained at runtime, is inherited by subclasses, and is included in the JavaDoc documentation.
This tutorial covered some of the most commonly used meta-annotations in Java, which can be applied to custom annotations to control their behavior and usage. By using these meta-annotations, you can create more powerful and flexible annotations for your Java projects.
List of built-in meta-annotations in Java:
@Target
, @Retention
, @Documented
, and @Inherited
. These annotations are used to provide metadata about other annotations.@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { // Annotation attributes and elements }
Creating custom meta-annotations in Java:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyMetaAnnotation { String value(); }
Usage of meta-annotations in Java annotations:
@MyMetaAnnotation("example") public @interface MyAnnotation { // Annotation attributes and elements }
Meta-annotations for retention and targeting in Java:
@Retention
specifies how long an annotation should be retained (e.g., during runtime), and @Target
specifies where the annotation can be applied (e.g., on fields, methods, or types).@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyRuntimeAnnotation { // Annotation attributes and elements }
Combining multiple annotations with meta-annotations:
@MyAnnotation @AnotherAnnotation public class MyClass { // Class content }
Inheritance and meta-annotations in Java:
@Inherited
meta-annotation indicates that an annotation should be inherited by subclasses.@Inherited @MyAnnotation public @interface InheritedAnnotation { // Annotation attributes and elements }
Annotation processors and meta-annotations in Java:
@SupportedAnnotationTypes("MyAnnotation") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class MyAnnotationProcessor extends AbstractProcessor { // Annotation processing logic }