Java - @Target Annotations
Last Updated :
23 Feb, 2022
Annotations in java are used to associate metadata to program elements like classes, methods, instance variables, etc. There are mainly three types of annotations in java: Marker Annotation (without any methods), Single-Valued Annotation (with a single method), and Multi-Valued Annotation (with more than one method). @Target annotation is a meta-annotation, i.e., it can only be used to annotate other annotations. It takes ElementType enumeration as its only argument. ElementType enumeration is a constant which specifies the type of the program element declaration (class, interface, constructor, etc.) to which the annotation can be applied. If we apply the @Target annotation with some ElementType as an annotation to a custom annotation name CustomAnnotation, then @CustomAnnotation can only be used to annotate those specific elements types otherwise we will get an error.
The table below shows the element types, and the element declaration that can be annotated are mentioned below as follows:
Element Type | Element to be Annotated |
---|
Type | Class, interface or enumeration |
Field | Field |
Method | Method |
Constructor | Constructor |
Local_Variable | Local variable |
Annotation_Type | Annotation Type |
Package | PACKAGE |
Type_Parameter | Type Parameter |
Parameter | Formal Parameter |
In order to use @Target annotation follow the below syntax as follows:
Syntax:
@Target(ElementType.TYPE)
@interface CustomAnnotation {}
In the code sample mentioned above, we have created a custom annotation annotated using @Target annotation. Since we have used ElementType as TYPE, therefore, the custom annotation can only annotate a class, enumeration, or interface. We can also create a custom annotation that can be applied to multiple elements by giving a braces-delimited list of element types as an argument to @Target annotation as shown below.
Syntax:
@Target({ElementType.METHOD, ElementType.PACKAGE})
@interface CustomAnnotation {}
The custom annotation created above can be used to annotate a method or a package. The code mentioned below shows how we can use @Target annotation. We have created two custom annotations: one which can be applied to a single element type and one which can be applied to multiple element types. Then we apply these annotations to our class and method, and finally, we print these annotations by fetching them.
Implementation:
Example
Java
// Java program to Illustrate Targeted Annotations
// Importing required classes from java.lang package
import java.lang.annotation.*;
// Creating a custom annotation with target
// as TYPE which means it can annotate a class,
// enumeration, or interface
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
// Class Annotation
@interface ClassAnnotation
{
String value() default "Can annotate a class";
}
// Creating custom annotation with
// target as multiple Element types as parameters
// which means it can annotate various Elements
@Target({ ElementType.METHOD, ElementType.TYPE,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR })
@Retention(RetentionPolicy.RUNTIME)
@interface MultipleElementTypeAnnotation
{
String value() default "Can annotate a class, method, "
+ "annotation, or constructor";
}
// Class to demonstrate the use of custom
// created annotations
@ClassAnnotation
// Main class
// TargetAnnotationDemo
public class GFG {
@MultipleElementTypeAnnotation public void myMethod() {}
// Main drive method
public static void main(String[] args) throws Exception
{
GFG obj = new GFG();
// Accessing the annotations used to annotate the
// class and storing them in an array of Annotation
// type since only one annotation is used to
// annotate our class therefore we print a[0]
Annotation a[] = obj.getClass().getAnnotations();
System.out.println(a[0]);
// Accessing the annotations used to annotate the
// method and storing them in an array of Annotation
// type
Class<?> className = Class.forName("GFG");
Annotation b[] = className.getMethod("myMethod")
.getAnnotations();
System.out.println(b[0]);
}
}
Output@ClassAnnotation(value="Can annotate a class")
@MultipleElementTypeAnnotation(value="Can annotate a class, method, annotation, or constructor")
Similar Reads
Java @Retention Annotations
In Java, annotations are used to attach meta-data to a program element such as a class, method, instances, etc. Some annotations are used to annotate other annotations. These types of annotations are known as meta-annotations. @Retention is also a meta-annotation that comes with some retention polic
3 min read
Annotations in Java
Annotations are used to provide supplemental information about a program. Annotations start with â@â.Annotations do not change the action of a compiled program.Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.A
9 min read
Hibernate - Annotations
Annotation in JAVA is used to represent supplemental information. As you have seen @override, @inherited, etc are an example of annotations in general Java language. For deep dive please refer to Annotations in Java. In this article, we will discuss annotations referred to hibernate. So, the motive
7 min read
Scala | Annotation
Scala Annotations are metadata added to the program source code. Annotations are allowed on any kind of definition or declaration including vals, vars, classes, objects, traits, defs and types. Annotations are used to associate meta-information with definitions. Syntax: @annot(exp_{1}, exp_{2}, ...)
3 min read
Spring @Lazy Annotation
In Spring Framework, the @Lazy annotation can be used to configure the lazy initialization of the beans. By default, Spring initializes all singleton beans eagerly at the application startup. However, in certain scenarios where the bean initialization is resource intensive or not immediately require
3 min read
Spring Core Annotations
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.Spring Framework
5 min read
Spring Boot - Annotations
Spring Boot Annotations are a form of metadata that provides data about a spring application. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the
7 min read
Spring - Stereotype Annotations
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. Spring
10 min read
The @SuppressWarnings Annotation in Java
Annotations are a very important part of Java in modern technologies, Most of the technologies such as Hibernate, Spring, Spring Boot, JPA, and so Many other Libraries are using annotations and making developers' life a lot easy. In Java, built-in General Annotations are - @Override@Deprecated@Funct
4 min read
Spring Data JPA - @Id Annotation
Spring Data JPA is a important part of Spring Boot applications, providing an abstraction over JPA (Java Persistence API) and simplifying database interactions. JPA is a specification that defines a standard way to interact with relational databases in Java, while Hibernate is one of the most widely
3 min read