java 放射的用法


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());
		//}
	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值