注解和反射笔记

  • 自定义两个注解
@Target({ElementType.CONSTRUCTOR, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)//运行时加载Annotation 到JVM 中,只有此状态可通过反射获取注解的信息。
@interface A {
	String value() default "默认构造方法";//成员
}

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface B {
	String describe();
	Class<?> type() default void.class;
}
  • 把以上两个注解来注解一个类
@A
public class Record {
	@B(describe="编号", type=int.class)
	int id;
	
	@B(describe="姓名", type=String.class)
	String name;
	
	@A()
	public Record() {
		
	}
	
	@A("立即初始化构造函数")
	public Record(
			@B(describe="编号", type=int.class) int id,
			@B(describe="姓名", type=String.class) String name) {
		this.id = id;
		this.name = name;
	}
	
	@B(describe="获取id", type=int.class)
	public int getId() {
		return id;
	}

	@B(describe="设置id", type=int.class)
	public void setId(@B(describe="编号", type=int.class) int id) {
		this.id = id;
	}

	@B(describe="获取姓名", type=String.class)
	public String getName() {
		return name;
	}

	@B(describe="设置姓名", type=String.class)
	public void setName(@B(describe="姓名", type=String.class) String name) {
		this.name = name;
	}		
	
}
  • 测试类
public class TestAnnotation {
	public static void main(String[] args) throws Exception {
		Class<?> clazz = Record.class;
		
		System.out.println("类上是否有注解:" + clazz.isAnnotationPresent(A.class));
		A a = clazz.getAnnotation(A.class);
		System.out.println("A注解的value值:" + a.value());
		
		System.out.println("---------");
		System.out.println("属性上的注解:");
		Field[] fields = clazz.getDeclaredFields();
		for(Field f: fields) {
			System.out.println(f.getName() + ": ");
			Annotation[] annotations = f.getAnnotations();//与注解本身名字有何不同呢?其实就是多态
			for(Annotation annotation: annotations) {
				System.out.println("     " + annotation);
			}
		}
		
		System.out.println("---------");
		System.out.println("构造方法上的注解:");
		Constructor<?>[] constructors = clazz.getDeclaredConstructors();
		for (Constructor<?> constructor : constructors) {
			constructor.setAccessible(true);
			System.out.println(constructor);
			Annotation[] annotations = constructor.getAnnotations();
			for (Annotation annotation : annotations) {
				System.out.println("     " + annotation);
			}
		}
		
		System.out.println("---------");
		System.out.println("方法上的注解:");
		Method[] methods = clazz.getDeclaredMethods();
		for(Method m: methods) {
			System.out.println(m.getName() + ":");
			Annotation[] annotations = m.getDeclaredAnnotations();
			for(Annotation annotation: annotations) {
				System.out.println("     " + annotation);
			}
		}
	}
}
  • 运行结果
类上是否有注解:true
A注解的value值:默认构造方法
---------
属性上的注解:
id: 
     @com.qhf.annotation.B(type=int, describe=编号)
name: 
     @com.qhf.annotation.B(type=class java.lang.String, describe=姓名)
---------
构造方法上的注解:
public com.qhf.annotation.Record()
     @com.qhf.annotation.A(value=默认构造方法)
public com.qhf.annotation.Record(int,java.lang.String)
     @com.qhf.annotation.A(value=立即初始化构造函数)
---------
方法上的注解:
getName:
     @com.qhf.annotation.B(type=class java.lang.String, describe=获取姓名)
getId:
     @com.qhf.annotation.B(type=int, describe=获取id)
setName:
     @com.qhf.annotation.B(type=class java.lang.String, describe=设置姓名)
setId:
     @com.qhf.annotation.B(type=int, describe=设置id)

猜你喜欢

转载自blog.csdn.net/pigqhf/article/details/89512850