Feature Meta Annotations

Annotations   «Prev  Next»

@Inherited meta-annotation

The @Inherited meta-annotation can be used to annotate any annotation, but when the annotation is used, it only gets inherited by a subclass of a class that is annotated by the inherited annotation. In the Java Code, create another class.
The name of this class is
public class CustomProcessor extends AbstractProcessor {

We want to create some Annotations in a new class.
meta annotation of Retention and RetentionPolicy.RUNTIME
 
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface InheritedClassAnnotation { }

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface InheritedInterfaceAnnotation { }

Create a new class called InheritedAnnotationExample. You have to be at the following path before you execute the command.
D:\AnnotationsProject\out\production\AnnotationsProject>
// Command to execute
javac -d . -cp . -processor CustomProcessor ../../../src/InheritedAnnotationExample.java

// This is the output of the program 
InheritedClassAnnotation
        CLASS :  SuperClass
        CLASS :  InheritedAnnotationExample
InheritedInterfaceAnnotation
        INTERFACE :  SuperInterface
D:\AnnotationsProject\out\production\AnnotationsProject>


I didn't specify Target Type for either of these annotations.
An Annotation that has not been Annotated with the meta-annotation Target, can be applied to most types but not all.
The target types are:
PACKAGE, MODULE, ANNOTATION_TYPE, CONSTRUCTOR, 
FIELD, LOCAL_VARIABLE, METHOD, PARAMETER, TYPE, TYPE_PARAMETER, and TYPE_USE.

When an annotation definition doesn't include the `@Target` meta-annotation, it becomes applicable to a wide range of program elements. However, explicitly defining the `@Target` provides better control and clarity about where an annotation is intended to be used.
Here is an explanation of the purpose of each `@Target` ElementType:
  1. PACKAGE:
    • Purpose: Indicates that the annotation can be applied to entire Java packages.
    • Use Case: Often used for package-level documentation, setting licensing information, or applying package-wide conventions or rules. These annotations are typically placed in the package-info.java file within the package directory.
  2. MODULE:
    • Purpose: Specifies that the annotation can be applied to Java modules (introduced in Java 9).
    • Use Case: Used to annotate module declarations (module-info.java) to provide metadata about the module, such as licensing, author information, or custom processing instructions relevant to the module as a whole.
  3. ANNOTATION_TYPE:
    • Purpose: Indicates that the annotation can be applied to the definitions of other annotations.
    • Use Case: Used to create meta-annotations, which are annotations that provide information about other annotations. @Retention and @Target themselves are examples of meta-annotations. This allows you to customize the behavior and applicability of your own annotations.
  4. CONSTRUCTOR:
    • Purpose: Specifies that the annotation can be applied to the declarations of constructors.
    • Use Case: Useful for marking specific constructors for dependency injection, testing setups, or indicating specific initialization logic.

  5. FIELD:
    • Purpose: Indicates that the annotation can be applied to the declarations of fields (instance variables or static variables).
    • Use Case: Commonly used for tasks like data validation (@NotNull, @Size), object-relational mapping (@Column, @Id), dependency injection (@Autowired), or serialization control (@Transient).
  6. LOCAL_VARIABLE:
    • Purpose: Specifies that the annotation can be applied to the declarations of local variables within methods or blocks.
    • Use Case: Less common than other targets, but can be used for purposes like marking variables that require specific handling (e.g., resource management), or for static analysis tools to perform checks on local variable usage.
  7. METHOD:
    • Purpose: Indicates that the annotation can be applied to the declarations of methods.
    • Use Case: Widely used for various purposes, including marking methods for testing (@Test), lifecycle management (@PostConstruct, @PreDestroy), transaction management (@Transactional), security checks (@Secured), or mapping to web endpoints (@GetMapping, @PostMapping).
  8. PARAMETER:
    • Purpose: Specifies that the annotation can be applied to the parameters of methods, constructors, or exception handlers.
    • Use Case: Used for tasks like dependency injection on parameters, validating method arguments (@PathVariable, @RequestBody), or specifying parameter-level information for documentation or API generation.
  9. TYPE:
    • Purpose: Indicates that the annotation can be applied to the declarations of classes, interfaces, enums, and annotation types themselves.
    • Use Case: Used for applying metadata at the type level, such as indicating a class is a service (@Service), a component (@Component), an entity (@Entity), or for applying custom rules or aspects to all instances of a type.
  10. TYPE_PARAMETER:
    • Purpose: Specifies that the annotation can be applied to the type parameters of generic classes, interfaces, methods, or constructors.
    • Use Case: Allows you to annotate the type variables themselves, providing metadata about the generic type. This can be useful for static analysis or for influencing how the generic type is handled.


By understanding these target types, you can effectively use the `@Target` meta-annotation to restrict where your custom annotations can be applied, leading to more maintainable and understandable code.

Add additional annotations to the Custom Annotation Source File.
// Intended for Type Parameter
@Target(ElementType.TYPE_PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interface TypeParameterAnnotation { }

// Intended for Type Use
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
@interface TypeUseAnnotation { }

Add another class called
public class AnnotateEverythingExample {

The class is attempting to Annotate everything with the exclusion of module and package.
All of the Annotations that I have declared in the CustomAnnotations file. Annotations can be applied almost anywhere, but there are 3 exceptions.
We need these types to be specified before they can be used. Let us do this at the Annotation Level. Go back to the CustomAnnotation Source file.
Retention only
The Type Annotation has retention only.
D:\AnnotationsProject\out\production\AnnotationsProject>javac -d . -cp . 
-processor CustomProcessor ../../../src/AnnotateEverythingExample.java
AttributeAnnotation

        FIELD:  AnnotateEverythingExample.aField
TypeAnnotation
        CLASS:  AnnotateEverythingExample
ConstructorAnnotation
        CONSTRUCTOR:  AnnotateEverythingExample.AnnotateEverythingExample()
TypeParameterAnnotation
        TYPE_PARAMETER:  <T>getSomething(T).T
MethodParameterAnnotation
        PARAMETER:  doSomething(java.lang.String).s
MethodAnnotation
        METHOD:  AnnotateEverythingExample.doSomething(java.lang.String)
D:\AnnotationsProject\out\production\AnnotationsProject>

SEMrush Software