zoukankan      html  css  js  c++  java
  • 07Java8新特性 其他新特性

    重复注解与类型注解

    Java8对注解处理提供了两点该进,可重复的注解及可用于类型的注解

    重复注解定义使用

    新建注解

    package com.dance.java8.day01.annotation;
    
    import java.lang.annotation.Repeatable;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
    
    // 重复注解 必须使用@Repeatable修饰 并指定注解容器
    @Repeatable(MyAnnotations.class)
    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    
        String value() default "flower";
    
    }

    新建注解容器

    package com.dance.java8.day01.annotation;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import static java.lang.annotation.ElementType.*;
    
    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotations {
    
        MyAnnotation[] value();
    
    }

    新建测试类

    package com.dance.java8.day01.annotation;
    
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    /**
     * 重复注解与类型注解
     */
    public class TestAnnotation {
    
        public static void main(String[] args) throws NoSuchMethodException {
            Class<TestAnnotation> testAnnotationClass = TestAnnotation.class;
            Method show = testAnnotationClass.getMethod("show");
            Arrays.stream(show.getAnnotationsByType(MyAnnotation.class)).forEach(System.out::println);
        }
    
        @MyAnnotation("flower")
        @MyAnnotation("dance")
        public void show(){
    
        }
    
    }

    执行结果

    @com.dance.java8.day01.annotation.MyAnnotation(value=flower)
    @com.dance.java8.day01.annotation.MyAnnotation(value=dance)

    类型注解定义使用

    修改注解,增加类型注解

    package com.dance.java8.day01.annotation;
    
    import java.lang.annotation.*;
    
    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
    
    // 重复注解 必须使用@Repeatable修饰 并指定注解容器
    @Repeatable(MyAnnotations.class)
    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, TYPE_PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    
        String value() default "flower";
    
    }

    修改测试类

    package com.dance.java8.day01.annotation;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    /**
     * 重复注解与类型注解
     */
    public class TestAnnotation {
    
        public static void main(String[] args) throws NoSuchMethodException {
            Class<TestAnnotation> testAnnotationClass = TestAnnotation.class;
            Method show = testAnnotationClass.getMethod("show",String.class);
            Arrays.stream(show.getAnnotationsByType(MyAnnotation.class)).forEach(System.out::println);
            Annotation[][] parameterAnnotations = show.getParameterAnnotations();
            Arrays.stream(parameterAnnotations).forEach(x-> Arrays.stream(x).forEach(System.out::println));
        }
    
        @MyAnnotation("flower")
        @MyAnnotation("dance")
        public void show(@MyAnnotation("string") String s){
    
        }
    
    }
    

    执行结果

    @com.dance.java8.day01.annotation.MyAnnotation(value=flower)
    @com.dance.java8.day01.annotation.MyAnnotation(value=dance)
    @com.dance.java8.day01.annotation.MyAnnotation(value=string)
     
  • 相关阅读:
    LAMP实例搭建wordpress博客步骤
    MySQL Replication
    Mariadb源码和二进制安装
    Linux九阴真经之九阴白骨爪残卷13(LVM的备份还原,恢复最新状态)
    Linux九阴真经之九阴白骨爪残卷12(日志功能)
    Linux九阴真经之九阴白骨爪残卷11(并发访问控制和事务Transactions)
    Linux九阴真经之九阴白骨爪残卷10(MySQL架构、缓存及索引)
    Linux九阴真经之九阴白骨爪残卷9(存储引擎MyISAM、MySQL服务器变量)
    Linux九阴真经之九阴白骨爪残卷8(存储函数、存储过程、触发器)
    Linux九阴真经之九阴白骨爪残卷7(Mariadb的三种安装方法)
  • 原文地址:https://www.cnblogs.com/flower-dance/p/15684991.html
Copyright © 2011-2022 走看看