java8 新增的@Repeatable注解
2015-02-03 21:14
253 查看
* java8 新增的@Repeatable注解,其实只是语法糖而已. * java8 注解的 {@link RepeatAnn} 类与 {@link Annotations}是等价的. * 新注解讲语法糖转化为注解值为数组形式.
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{ }}
相关文章推荐
- java8 新增的@Repeatable注解
- java8的注解-Repeatable
- Java8新增的重复注解@Repeatble和类型注解
- SCA的java注解和API规范v100
- EasyDBO中Java注解配置映射的原理及使用
- java annocation(注解)
- EasyDBO中Java注解配置映射的原理及使用
- 体验Java 5.0的新增语言特性
- 告别System.out.print()—J2SDK1.4新增Java日志框架(二)
- 在EasyDBO中使用Java注解配置映射的原理及使用
- 在EasyDBO中使用Java注解配置映射的原理及使用
- 告别System.out.print()—J2SDK1.4新增Java日志框架(三)
- 在EasyDBO中使用Java注解配置映射的原理及使用
- Java的String经典笔试题付注解
- 在EasyDBO中使用Java注解配置映射的原理及使用
- java annocation(注解)
- Sun Java Wireless Toolkit 2.3 beta新增功能(翻译)
- 体验Java 5.0的新增语言特性
- 在EasyDBO中使用Java注解配置映射的原理及使用
- java.sql.ResultSet 新增、更新、刪除資料