反射的一些常用方法

获取父类

getSuperClass()  //
getClass().getSuperclass().getName() //父类的名称   

通过反射拿到这个类中所有属性

        //拿到这个类中所有属性
        Field[] declaredFields = aClass.getDeclaredFields();

拿到属性上被这个注解修饰的注解值

          AnnotNote annotation = field.getAnnotation(AnnotNote.class);

属性描述器

        PropertyDescriptor ps = new PropertyDescriptor(field.getName(), aClass);
  // getReadMethod  获得用于读取属性值的方法;   getWriteMethod(),获得用于写入属性值的方法;
                Method readMethod = ps.getReadMethod();
                   Object invoke = readMethod.invoke(dataBean);

判断属性上是否加了AnnotNote这个注解

field.isAnnotationPresent(AnnotNote.class)

根据反射拿到这个类中所有方法

     Method[] methods = druidConfigClass.getMethods();

反射工具类,通过反射设置字段的值

/**
 * 功能描述:
 * 〈根据传入的字段,和要给字段赋的值,和对象,给对象中的这个字段赋值〉
 *
 * @param fieldName 1 字段名称
 * @param fieldValue 2 值
 * @param obj 3 对象
 * @return : void
 * @author : lqy
 * @date : 2020/9/16 11:10
 */
    public void setFieldValue(String fieldName,String fieldValue,Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        Class<?> aClass = obj.getClass();
        PropertyDescriptor ps = new PropertyDescriptor(fieldName,aClass);
        Method wri = ps.getWriteMethod();
        wri.invoke(obj,fieldValue);
    }

根据反射获取属性的值

/**
 * 功能描述:
 * 〈根据对象,和对象中的字段,获取对象中该字段的值〉
 *
 * @param obj 1 对象
 * @param fieldName 2 字段名称
 * @return : java.lang.Object
 * @author : lqy
 * @date : 2020/9/16 13:04
 */
    public Object getFieldValueUtil(Object obj,String fieldName) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        Class<?> aClass = obj.getClass();
        //属性描述器
        PropertyDescriptor ps = new PropertyDescriptor(fieldName, aClass);
        // getReadMethod  获得用于读取属性值的方法;   getWriteMethod(),获得用于写入属性值的方法;
        Method readMethod = ps.getReadMethod();
        Object invoke = readMethod.invoke(obj);
        //System.out.println("通过class,和字段。去读取指定对象的" + fieldName + "属性的值:" + invoke);
        //拿到的为空时返回0 否者把拿到的值转成String并返回
        return  invoke==null?0:invoke.toString();
    }

通过反射拿到包含所有父类在内所有属性

/**
 * 功能描述:
 * 〈获取包含父类在内所有得属性〉
 *
 * @param obj 1
 * @return : java.util.List<java.lang.reflect.Field>
 * @author : lqy
 * @date : 2020/9/21 18:19
 */
    public List<Field> getAllField(Object obj){
        ArrayList<Field> fields = new ArrayList<>();

        Class<?> aClass = obj.getClass();

        while (aClass!=null){
            Field[] declaredFields = aClass.getDeclaredFields();
            fields.addAll(Arrays.asList(declaredFields));
            aClass=aClass.getSuperclass();
        }

return fields;
    }

获取类中被改注解修饰的值,或者该注解的值

   /**
     * 功能描述:
     * 〈获取类中被该注解修饰的值,或者被修饰注解的值〉
     *
     * @param obj 1
     * @return : void
     * @author : lqy
     * @date : 2020/9/21 18:31
     */
    public void getAnAnnotField(Object obj) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        //根据工具拿到包含父类在内所有的属性
        List<Field> allField = getAllField(obj);
        //遍历属性
        for (Field field : allField
        ) {
            //判断该属性是否被AnnotNote注解修饰
            if (field.isAnnotationPresent(AnnotNote.class)) {
                //根据属性跟类,拿到值
                Object fieldValue = getFieldValueUtil(obj, field.toString());
                System.out.println("拿到被AnnotNote修饰得属性值:" + fieldValue);
                //拿到修饰该属性的注解
                AnnotNote annotation = field.getAnnotation(AnnotNote.class);
                String value = annotation.value();
                int age = annotation.age();
                System.out.println("属性上的注解:" + annotation + ",注解Value:" + value + ",注解age:" + age);
            }
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值