學習反射過程中,自己編寫的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;
}
}
懶得粘就下載