* Java8 added @repeatable annotations, in fact, just a syntactic sugar. * The {@link Repeatann} class of java8 annotations is equivalent to {@link Annotations}. * New annotations say syntactic sugars are converted to annotation values in an array form.
Package Com.github.jdk8.ebook.java8_recipes2nd_edition;import Java.lang.annotation.annotation;import Java.lang.annotation.elementtype;import Java.lang.annotation.repeatable;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.target;import java.util.Arrays;/** * java8 The new @repeatable annotation is actually just a syntactic sugar. * The {@link Repeatann} class of java8 annotations is equivalent to {@link Annotations}. * New annotations say syntactic sugars are converted to annotation values in an array form. * @author Doctor * * @since February 3, 2015 PM 8:33:43 */public class Chapter2code {public static void main (string[] args) {Annot ation[] annotations = RepeatAnn.class.getAnnotations (); System.out.println (annotations.length); 1arrays.stream (annotations). ForEach (System.out::p rintln);//@com. Github.jdk8.ebook.java8_recipes2nd_edition. Chapter2code$roles (value=[@com. github.jdk8.ebook.java8_recipes2nd_edition. Chapter2code$role (Name=doctor), @com. Github.jdk8.ebook.java8_recipes2nd_edition. Chapter2code$role (name=who)]) annotation[] Annotations2 = Annotations.class.getAnnotatiONS (); System.out.println (annotations2.length);//1arrays.stream (ANNOTATIONS2). ForEach (System.out::p rintln);//@ Com.github.jdk8.ebook.java8_recipes2nd_edition. Chapter2code$roles (value=[@com. github.jdk8.ebook.java8_recipes2nd_edition. Chapter2code$role (Name=doctor), @com. Github.jdk8.ebook.java8_recipes2nd_edition. Chapter2code$role (name=who)])}/** * The same annotation can be applied to a declaration or type more than * once, given th At all annotation is marked as @Repeatable. In the * following code, the @Repeatable annotation are used to develop a * annotation, can be repeated, rather than g Rouped together as in * previous releases of Java. In this situation, the annotation named role is * being created, and it'll be used to signify a role for a annotated * C Lass or method. * * @author Doctor * * @since February 3, 2015 afternoon 8:51:09 */@Repeatable (value = roles.class) public static @interface Role {String Name () default "Doctor";} @Target (Elementtype.type) @Retention (retentionpolicy.runtime) PublIC static @interface Roles {role[] value ();} @Role (name = "Doctor") @Role (name = "who") public static Class repeatann{} @Roles ({@Role (name= "Doctor"), @Role (name= "who" )}) public static class annotations{}}
Java8 New @repeatable Annotations