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

Java Meta-annotations

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.

  • @Target:

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.

  • @Retention:

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.

  • @Inherited:

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 {
}
  • @Documented:

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 {
}
  • Example of using multiple meta-annotations:

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.

  1. List of built-in meta-annotations in Java:

    • Java provides several built-in meta-annotations such as @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
    }
    
  2. Creating custom meta-annotations in Java:

    • You can create custom meta-annotations by combining built-in meta-annotations.
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyMetaAnnotation {
        String value();
    }
    
  3. Usage of meta-annotations in Java annotations:

    • Meta-annotations are used to annotate other annotations, providing additional information about how the annotated annotation should be processed.
    @MyMetaAnnotation("example")
    public @interface MyAnnotation {
        // Annotation attributes and elements
    }
    
  4. 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
    }
    
  5. Combining multiple annotations with meta-annotations:

    • You can combine multiple annotations using meta-annotations to create a composite annotation.
    @MyAnnotation
    @AnotherAnnotation
    public class MyClass {
        // Class content
    }
    
  6. Inheritance and meta-annotations in Java:

    • The @Inherited meta-annotation indicates that an annotation should be inherited by subclasses.
    @Inherited
    @MyAnnotation
    public @interface InheritedAnnotation {
        // Annotation attributes and elements
    }
    
  7. Annotation processors and meta-annotations in Java:

    • Annotation processors can be used to process annotations at compile time. Meta-annotations play a role in specifying how annotation processors should handle annotations.
    @SupportedAnnotationTypes("MyAnnotation")
    @SupportedSourceVersion(SourceVersion.RELEASE_8)
    public class MyAnnotationProcessor extends AbstractProcessor {
        // Annotation processing logic
    }