Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do Java annotation ElementType constants mean?

java.lang.annotation.ElementType:

A program element type. The constants of this enumerated type provide a simple classification of the declared elements in a Java program. These constants are used with the Target meta-annotation type to specify where it is legal to use an annotation type.

There are the following constants:

  • ANNOTATION_TYPE - Annotation type declaration
  • CONSTRUCTOR - Constructor declaration
  • FIELD - Field declaration (includes enum constants)
  • LOCAL_VARIABLE - Local variable declaration
  • METHOD - Method declaration
  • PACKAGE - Package declaration
  • PARAMETER - Parameter declaration
  • TYPE - Class, interface (including annotation type), or enum declaration

Can someone explain what each of them are (where they'd be annotated in actual code)?

like image 637
Action Jackson Avatar asked Aug 23 '10 18:08

Action Jackson


People also ask

What is ElementType Annotation_type?

@Target(ElementType. ANNOTATION_TYPE) it is a tool that allows to extend the use of annotations. Follow this answer to receive notifications.

What is the use of @target annotation?

This @Target meta-annotation indicates that the declared type is intended solely for use as a member type in complex annotation type declarations. It cannot be used to annotate anything directly: @Target({}) public @interface MemberType { ... }

What is @interface annotation in Java?

@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example: @interface MyAnnotation { String value(); String name(); int age(); String[] newNames(); }

What is a target in Java?

java.lang.annotation.Target. Indicates the contexts in which an annotation type is applicable. The declaration contexts and type contexts in which an annotation type may be applicable are specified in JLS 9.6. 4.1, and denoted in source code by enum constants of java.


1 Answers

Let's say the annotation to which you specify the ElementType is called YourAnnotation:

  • ANNOTATION_TYPE - Annotation type declaration. Note: This goes on other annotations

    @YourAnnotation public @interface AnotherAnnotation {..} 
  • CONSTRUCTOR - Constructor declaration

    public class SomeClass {     @YourAnnotation     public SomeClass() {..} } 
  • FIELD - Field declaration (includes enum constants)

    @YourAnnotation private String someField; 
  • LOCAL_VARIABLE - Local variable declaration. Note: This can't be read at runtime, so it is used only for compile-time things, like the @SuppressWarnings annotation.

    public void someMethod() {     @YourAnnotation int a = 0; } 
  • METHOD - Method declaration

    @YourAnnotation public void someMethod() {..} 
  • PACKAGE - Package declaration. Note: This can be used only in package-info.java.

    @YourAnnotation package org.yourcompany.somepackage; 
  • PARAMETER - Parameter declaration

    public void someMethod(@YourAnnotation param) {..} 
  • TYPE - Class, interface (including annotation type), or enum declaration

    @YourAnnotation public class SomeClass {..} 

You can specify multiple ElementTypes for a given annotation. E.g.:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD}) 
like image 139
Bozho Avatar answered Sep 22 '22 23:09

Bozho