Java8 New @repeatable Annotations

Source: Internet
Author: User

* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.