java 註解@interface

類註解:java

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();
}

方法註解app

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";
}

測試類ui

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();
    }
}

運行spa

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());
    }
}
相關文章
相關標籤/搜索