学习反射过程中,自己编写的demo,中间混合了泛型+注解
package com.rgy.ano;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(FIELD)
public @interface Ano1 {
String ano() default "";
}
package com.rgy.model;
import com.rgy.ano.Ano1;
public class Emp extends Person{
@Ano1(ano="this is id")
private String id;
@Ano1(ano="this is name")
private String name;
@Ano1(ano="this is time")
private String time;
public Emp(String id, String name, String time) {
super();
this.id = id;
this.name = name;
this.time = time;
}
}
package com.rgy.model;
import com.rgy.ano.Ano1;
public class Person {
@Ano1(ano="this is super id")
private String id;
}
package com.rgy.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import com.rgy.ano.Ano1;
import com.rgy.model.Emp;
/**
* @author 任冠宇
* @param <T>
* 泛型,不想定义在类的后面,也可以定义在方法的后面
*/
public class Test<T> {
public static void main(String[] args) throws Exception {
// 构造测试数据
List<Emp> emps = initData();
// 利用利用反射打印list
new Test<Emp>().outTList(emps,Ano1.class);
}
//第二版本,也就是泛型版本
// 恩,设置成了高大上的泛型
private void outTList(List<T> objs, Class<Ano1> cls) throws Exception {
// 通过反射的方式去获取属性,属性值,注解,注解值
for (T obj : objs) {
//获取对象的类对象
Class<? extends Object> class1 = obj.getClass();
//Emp还有一个父类Person,也利用反射去查询一下父类里面的数据吧!
//毕竟是练习Demo,需要写的全面一些
Field[] declaredFields2 = class1.getSuperclass().getDeclaredFields();
for (Field field : declaredFields2) {
field.setAccessible(true);
//父类的注解
Ano1 superTypeAno = field.getAnnotation(cls);
//父类注解的类型
Class<? extends Annotation> superTypeAnoType = superTypeAno.annotationType();
//父类注解类型的名字
String simpleName = superTypeAnoType.getSimpleName();
//父类注解类型的值
String superAnoValue = superTypeAno.ano();
//父类的属性值
Object superTypeAnoValue = field.get(obj);
System.out.println("super->>>>"+superAnoValue+"\t"+superTypeAnoValue);
}
// 得到对象里面的属性
Field[] declaredFields = class1.getDeclaredFields();
// 循环属性数组
for (Field field : declaredFields) {
// 设置可以访问属性里面的值,就是打开可可读的权限
field.setAccessible(true);
// 获取属性的注解
Ano1 ano1 = field.getAnnotation(cls);
// 获取注解类型的名字
String ano1ClassName = ano1.annotationType().getSimpleName();
// 获取注解的值
String ano1Value = ano1.ano();
// 获取属性的类型名字
String simpleName = field.getType().getSimpleName();
// 获取属性的名字
String name = field.getName();
// 获取属性的值,这里需要说的一点,这个语法看起来挺别扭的,但是他就是这么设计的
// 传入对象,然后用属性对象获取自己的属性,相当于field.getId();
Object fieldValue = field.get(obj);
// 获取到了我想要的全部数据,打印出来吧!
System.out.println(ano1ClassName + "\t" + ano1Value);
System.out.println(simpleName + "\t" + name + "\t" + fieldValue);
}
System.out.println("=====================================");
}
}
//第一版本
// 这里的类型貌似可以设置成泛型,嗯~o(* ̄▽ ̄*)o可以,试一试
private static void outEmpList(List<Emp> emps) throws IllegalAccessException {
// 通过反射的方式去获取属性,属性值,注解,注解值
for (Emp emp : emps) {
// 得到对象里面的属性
Field[] declaredFields = emp.getClass().getDeclaredFields();
// 循环属性数组
for (Field field : declaredFields) {
// 设置可以访问属性里面的值,就是打开可可读的权限
field.setAccessible(true);
// 获取属性的注解
Ano1 ano1 = field.getAnnotation(Ano1.class);
// 获取注解类型的名字
String ano1ClassName = ano1.annotationType().getSimpleName();
// 获取注解的值
String ano1Value = ano1.ano();
// 获取属性的类型名字
String simpleName = field.getType().getSimpleName();
// 获取属性的名字
String name = field.getName();
// 获取属性的值,这里需要说的一点,这个语法看起来挺别扭的,但是他就是这么设计的
// 传入对象,然后用属性对象获取自己的属性,相当于field.getId();
Object fieldValue = field.get(emp);
// 获取到了我想要的全部数据,打印出来吧!
System.out.println(ano1ClassName + "\t" + ano1Value);
System.out.println(simpleName + "\t" + name + "\t" + fieldValue);
}
System.out.println("=====================================");
}
}
// 构造一组数据,用于测试之用
private static List<Emp> initData() {
List<Emp> emps = new ArrayList<Emp>();
for (int i = 0; i < 3; i++) {
String id = UUID.randomUUID().toString();
String name = i + "";
String time = new Timestamp(System.currentTimeMillis()).toString();
Emp e = new Emp(id, name, time);
emps.add(e);
}
return emps;
}
}
懒得粘就下载