zoukankan      html  css  js  c++  java
  • java 注解@interface

    类注解:

    package com.cglibs;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    // 适用类、接口(包括注解类型)或枚举
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface ClassInfo {
        String value();
    }
    field属性注解
    package com.cglibs;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    // 适用field属性,也包括enum常量
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface FieldInfo {
        int[] value();
    }

    方法注解

    package com.cglibs;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    // 适用方法
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface MethodInfo {
        String name() default "long";
        String data();
        int age() default 27;
    }

    参数注解

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.cglibs;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    
    @Target({ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AutoUserModel {
        boolean value() default true;
        boolean require() default true;
        String message() default "user is null";
    }

    测试类

    package com.cglibs;
    
    /**
     * 测试运行时注解
     */
    @ClassInfo("Test Class")
    public class RuntimeAnnotation {
     
        @FieldInfo(value = {1, 2})
        public String fieldInfo = "FiledInfo";
     
        @FieldInfo(value = {10086})
        public int i = 100;
     
        @MethodInfo(name = "BlueBird", data = "Big")
        public static String getMethodInfo(@AutoUserModel(value = false,require = true) @AutoPlatformModel String a) {
            System.out.println("RuntimeAnnotation.getMethodInfo "+a);
            return RuntimeAnnotation.class.getSimpleName();
        }
    }

    运行

    package com.cglibs;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.util.Arrays;
    
    /**
     * 测试运行时注解
     */
    public class TestRuntimeAnnotation {
        /**
         * 测试运行时注解
         */
        public static void main(String[] args) {
            StringBuilder sb = new StringBuilder();
            Class<?> cls = RuntimeAnnotation.class;
            // 获取指定类型的注解
            sb.append("Class注解:").append("
    ");
            ClassInfo classInfo = cls.getAnnotation(ClassInfo.class);
            if (classInfo != null) {
                sb.append(Modifier.toString(cls.getModifiers())).append(" ")
                        .append(cls.getSimpleName()).append("
    ");
                sb.append("注解值: ").append(classInfo.value()).append("
    
    ");
            }
    
            sb.append("Field注解:").append("
    ");
            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {
                FieldInfo fieldInfo = field.getAnnotation(FieldInfo.class);
                if (fieldInfo != null) {
                    sb.append(Modifier.toString(field.getModifiers())).append(" ")
                            .append(field.getType().getSimpleName()).append(" ")
                            .append(field.getName()).append("
    ");
                    sb.append("注解值: ").append(Arrays.toString(fieldInfo.value())).append("
    
    ");
                }
            }
    
            sb.append("Method注解:").append("
    ");
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods) {
                MethodInfo methodInfo = method.getAnnotation(MethodInfo.class);
                if (methodInfo != null) {
                    sb.append(Modifier.toString(method.getModifiers())).append(" ")
                            .append(method.getReturnType().getSimpleName()).append(" ")
                            .append(method.getName()).append("
    ");
                    sb.append("注解值: ").append("
    ");
                    sb.append("name: ").append(methodInfo.name()).append("
    ");
                    sb.append("data: ").append(methodInfo.data()).append("
    ");
                    sb.append("age: ").append(methodInfo.age()).append("
    ");
                    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
                    for (Annotation[] a : parameterAnnotations
                            ) {
                        if (a != null) {
                            for (Annotation b : a
                                    ) {
                                if (AutoUserModel.class.isAssignableFrom(b.annotationType())) {
                                    AutoUserModel b1 = (AutoUserModel) b;
                                    sb.append("找到了 AutoUserModel =").append(b1.value()).append("
    ");
                                }
                                if (AutoPlatformModel.class.isAssignableFrom(b.annotationType())) {
                                    AutoPlatformModel b1 = (AutoPlatformModel) b;
                                    sb.append("找到了 AutoPlatformModel =").append(b1.value()).append("
    ");
                                }
                            }
                        }
    
                    }
                }
            }
            System.out.print(sb.toString());
        }
    }
  • 相关阅读:
    常用学习网站 (转载)
    Cookie 与 Session 的区别(转载)
    Asp.net 控件ClientID,UniqueID命名规则问题
    HTTP POST GET 本质区别详解
    asp net中的状态管理的方式有哪几种分别有哪些优势和缺点
    http 协议 (转载)
    Java 学习 (转载)
    数据库日常维护常用的脚本部分收录(转载)
    ASP.NET页面与IIS底层交互和工作原理详解(转载)
    Web网站的性能测试工具(转载)
  • 原文地址:https://www.cnblogs.com/bestzhang/p/11836944.html
Copyright © 2011-2022 走看看