java 注解@interface

时间:2019-11-11
本文章向大家介绍java 注解@interface,主要包括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("\n");
        ClassInfo classInfo = cls.getAnnotation(ClassInfo.class);
        if (classInfo != null) {
            sb.append(Modifier.toString(cls.getModifiers())).append(" ")
                    .append(cls.getSimpleName()).append("\n");
            sb.append("注解值: ").append(classInfo.value()).append("\n\n");
        }

        sb.append("Field注解:").append("\n");
        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("\n");
                sb.append("注解值: ").append(Arrays.toString(fieldInfo.value())).append("\n\n");
            }
        }

        sb.append("Method注解:").append("\n");
        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("\n");
                sb.append("注解值: ").append("\n");
                sb.append("name: ").append(methodInfo.name()).append("\n");
                sb.append("data: ").append(methodInfo.data()).append("\n");
                sb.append("age: ").append(methodInfo.age()).append("\n");
                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("\n");
                            }
                            if (AutoPlatformModel.class.isAssignableFrom(b.annotationType())) {
                                AutoPlatformModel b1 = (AutoPlatformModel) b;
                                sb.append("找到了 AutoPlatformModel =").append(b1.value()).append("\n");
                            }
                        }
                    }

                }
            }
        }
        System.out.print(sb.toString());
    }
}

原文地址:https://www.cnblogs.com/bestzhang/p/11836944.html