Java自定义注解之运行时注解

Java自定义注解之运行时注解

java 自定义注解分为两种, 运行时注解和编译期注解。本篇文章主要讲的是运行时注解。

@Retention(RetentionPolicy.RUNTIME)表示运行时保留的注解。我们可以在运行时,通过java反射机制,获取到注解信息。

接口AnnotatedElement表示程序中的注解元素,可以使用反射读取。接口中返回的所有注解都是不可变和可序列化的,接口方法返回的数组可以被修改,但不会影响原有的注解。接口的getAnnotationsByType(Class)和getDeclaredAnnotationsByType(Class)方法支持重复注解。

目标为class的注解

1
2
3
4
5
6
7
8
9
10
11
12
13
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 name() default "";

String description() default "";
}

目标为方法的注解

1
2
3
4
5
6
7
8
9
10
11
12
13
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 "";

String description() default "";
}

目标为属性的注解

1
2
3
4
5
6
7
8
9
10
11
12
13
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.FIELD)
public @interface FieldInfo {

String name() default "";

String description() default "";
}

注解处理工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class InfoUtil {
public static void HandleInfo(Class<?> clazz){
if(clazz.isAnnotationPresent(ClassInfo.class)) {
ClassInfo classInfo = clazz.getAnnotation(ClassInfo.class);
System.out.println("classInfo:\n"+classInfo.name()+"::"+classInfo.description());
}

Method[] methods = clazz.getDeclaredMethods();
for(Method item: methods){
if(item.isAnnotationPresent(MethodInfo.class)){
MethodInfo methodInfo = item.getAnnotation(MethodInfo.class);
System.out.println("methodInfo:\n"+methodInfo.name()+"::"+methodInfo.description());
}
}

Field[] fields = clazz.getDeclaredFields();
for(Field item: fields){
if(item.isAnnotationPresent(FieldInfo.class)){
FieldInfo fieldInfo = item.getAnnotation(FieldInfo.class);
System.out.println("methodInfo:\n"+fieldInfo.name()+"::"+fieldInfo.description());
}
}
}
}

实体类demo

1
2
3
4
5
6
7
8
9
10
11
@ClassInfo(name = "DemoClass",description = "A demo class for annotation")
public class Demo {

@FieldInfo(name = "DemoField",description = "A demo field for annotation")
private String name;

@MethodInfo(name = "DemoMethod", description = "A demo method for annotation")
public String getName() {
return name;
}
}

测试类

1
2
3
4
5
public class Test {
public static void main(String[] args){
InfoUtil.HandleInfo(Demo.class);
}
}
1
2
3
4
5
6
classInfo:
DemoClass::A demo class for annotation
methodInfo:
DemoMethod::A demo method for annotation
methodInfo:
DemoField::A demo field for annotation

参考资料:

  • Java官方Tutorial
-------------本文结束感谢您的阅读-------------
坚持分享,您的支持将鼓励我继续创作!
0%