package com.wonlymall.mall.modular.user.modelVO;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.springframework.stereotype.Component;
public class TestReflect {
public static void main(String[] args) {
//擷取具體類的類型
Class<?>clazz=UserBaseInfoVO.class;
Object obj=new Object();
//擷取Object 類的類型
Class clazz1=obj.getClass();
//擷取類名
String className=clazz.getName();
System.out.println(className);
//擷取類的注解
Component c=clazz.getAnnotation(Component.class);
System.out.println(c);
//擷取注解的參數值
String value=c.value();
System.out.println(value);
//擷取類上面的注解
Component c1=clazz.getAnnotationsByType(Component.class)[0];
//擷取本類的所有注解
Annotation[]a=clazz.getDeclaredAnnotations();
Annotation[]a1=clazz.getAnnotations();
try {
//根據放射擷取構造器 利用構造器建立對象
//Constructor constructor=clazz.getConstructor(Integer.class);
//UserBaseInfoVO vo=(UserBaseInfoVO) constructor.newInstance(1121);
//System.out.println(vo.getEscOrgId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//擷取本類中的所有構造器
Constructor[]con=clazz.getDeclaredConstructors();
System.out.println(con);
//擷取父類
Class superClazz=clazz.getSuperclass();
System.out.println(superClazz);
Method[]method=clazz.getDeclaredMethods();
System.out.println(method);
try {
UserBaseInfoVO vo=(UserBaseInfoVO) clazz.newInstance();
Field field=clazz.getDeclaredField("escOrgId");
//擷取屬性上面的注解
//field.getAnnotation(annotationClass)
//擷取參數類型
//field.getGenericType().toString();
field.setAccessible(true);
field.set(vo, 111);
System.out.println(vo.getEscOrgId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//擷取類的所有屬性
//Field[]fields=clazz.getDeclaredFields();
//for(Field f:fields) {
//String fieldName=f.getName();
//System.out.println(f.getName());
//}
//Field[]fields=clazz.getSuperclass().getFields();
//for(Field f:fields) {
//String fieldName=f.getName();
//System.out.println(f.getName());
//}
}
}