java8 新增的@Repeatable注解

  1. * java8 新增的@Repeatable注解,其实只是语法糖而已.  
  2.  * java8 注解的 {@link RepeatAnn} 类与 {@link Annotations}是等价的.  
  3.  * 新注解讲语法糖转化为注解值为数组形式.  
    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 新增的@Repeatable注解,其实只是语法糖而已. 
     * java8 注解的 {@link RepeatAnn} 类与 {@link Annotations}是等价的. 
     * 新注解讲语法糖转化为注解值为数组形式. 
     * @author doctor 
     * 
     * @since 2015年2月3日 下午8:33:43 
     */  
    public class Chapter2Code {  
      
        public static void main(String[] args) {  
            Annotation[] annotations = RepeatAnn.class.getAnnotations();  
            System.out.println(annotations.length); //1  
            Arrays.stream(annotations).forEach(System.out::println);//@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);//1  
            Arrays.stream(annotations2).forEach(System.out::println);//@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 that each annotation is marked as @Repeatable. In the 
         * following code, the @Repeatable annotation is used to develop an 
         * annotation that can be repeated, rather than grouped together as in 
         * previous releases of Java. In this situation, an annotation named Role is 
         * being created, and it will be used to signify a role for an annotated 
         * class or method. 
         *  
         * @author doctor 
         * 
         * @since 2015年2月3日 下午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{  
              
        }  
    }  

来自:http://blog.csdn.net/doctor_who2004/article/details/43457689 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值