- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Repeating Annotations
Another new JDK 8 annotation feature enables an annotation to be repeated on the same element. This is called as repeating annotations.
For an annotation to be repeatable, it must be annotated with the @Repeatable annotation, defined in the java.lang.annotation. Its value field specifies the container type for the repeatable annotation. The container is specified as an annotation for which the value field is an array of the repeatable annotation type. Thus, to create a repeatable annotation, you must create a container annotation and then specify that annotation type as an argument to the @Repeatable annotation.
Java Repeating Annotation Example
To access repeatable annotations using a method such as getAnnotation(), you will use the container annotation, not the repeatable annotation. The following program shows this approach. It converts the version of the MyAnnotat shown previously into a repeatable annotation and demonstrates its use.
/* Java Program Example - Java Repeating Annotations * This program demonstrate a repeated annotation */ import java.lang.annotation.*; import java.lang.reflect.*; /* make MyAnnotat repeatable */ @Retention(RetentionPolicy.RUNTIME) @Repeatable(MyRepeatedAnnos.class) @interface MyAnnotat { String str() default = "Testing"; int val() default 9000; } /* this is the container annotation */ @Retention(RetentionPolicy.RUNTIME) @interface MyRepeatedAnnos { MyAnnotat[] value(); } class RepeatAnnotat { /* repeat MyAnnotat on myMethod() */ @MyAnnotat(str = "First annotation", val = -1) @MyAnnotat(str = "Second annotation", val = 100); public static void myMethod(String str, int i) { RepeatAnnotat obj = new RepeatAnnotat(); try { Class<?> c = obj.getClass(); /* obtain the annotations for myMethod() */ Method m = c.getMethod("myMethod", String.class, int.class); /* display the repeated MyAnnotat annotations */ Annotation anno = m.getAnnotation(MyRepeatedAnnos.class); System.out.println(anno); } catch(NoSuchMethodException exc) { System.out.println("Method not found..!!"); } } public static void main(String args[]) { myMethod("test", 10); } }
The output of the above Java program is shown here :
@MyRepeatedAnnos(value=[@MyAnnotat(str=First annotation, val=-1), @MyAnnotat(str=Second annotation, val=100)])
As explained, in order for MyAnnotat to be repeatable, it must be annotated with @Repeatable annotation, which specifies its container annotation. The container annotation is called MyRepeatedAnnos. The program accesses the repeated annotations by calling the getAnnotation() method, passing in the class of the container annotation, not the repeatable annotation itself. As the output shows, the repeated annotations are separated by a comma. They are not returned individually.
Another way to obtain the repeated annotations is to use one of the new methods added to the AnnotatedElement by JDK 8, which can operated directly on a repeated annotation. These are getAnnotationsByType() and getDeclaredAnnotationsByType(). Here, we will use the former. It is shown below :
<T extends Annotation> T[] getAnnotationsByType(Class<T> annoType)
It returns an array of annotations of annoType associated with the invoking object. If no annotations are present, the array will be zero length. Here is an example. Assuming the preceding example, the following sequence uses getAnnotationsByType() to obtain the repeated MyAnnotat annotations :
Annotation[] annos = m.getAnnotationsByType(MyAnnotat.class); for(Annotation a : annos) System.out.println(a);
Here, the repeated annotation type, which is MyAnnotat, is passed to getAnnotationsByType(). The returned array contained all of the instances of MyAnnotat associated with myMethod(), which, in this example, is two. Each repeated annotation can be accessed via its index in the array. In this case, each MyAnnotat annotation is displayed via a for-each loop.
Some Restrictions
There are a number of restrictions that apply to the annotation declarations. First, no annotation can inherit another. Second, all the methods declared by an annotation must be without parameters. Furthermore, they must return one of the following :
- A primitive types, such as double or int
- An object of the type Class or String
- An enum type
- Another annotation type
- An array of one of the preceding types
Annotations can't be generic i.e. they can't take type parameters. Finally, annotation methods can't specify a throws clause.
« Previous Tutorial Next Tutorial »