反射 根据字节码获取对象和其中的属性和方法

package com.wc;


import com.wc.domain.Dept;
import com.wc.domain.Emp;
import com.wc.util.RandomUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author nalan
 */

public class App {
    public static void main(String[] args) throws Exception {
//        String className = "com.wc.domain.Dept";
        
        Object obj = genObj(Dept.class);

        Object obj1 = genObj(Emp.class);

        System.out.println(obj);
        System.out.println(obj1);

    }

    private static Object genObj(Class<?> clazz) throws Exception {
        //根据字节码生成对象
        Object obj = clazz.getDeclaredConstructor().newInstance();
        //根据字节码获取类中声明的所有方法
        Method[] methods = clazz.getDeclaredMethods();

        for (Method method : methods) {
            //获取方法的名称
            String methodName = method.getName();
            if (methodName.startsWith("set")) {
                //获取方法形参的参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    //如果类型范围是Integer则使用方法调用调用Random-util里面的方法
                    if (parameterType == Integer.class) {
                        method.invoke(obj, RandomUtil.genInteger(1000, 9999));
                    }
                    if (parameterType == String.class) {
                        method.invoke(obj, RandomUtil.genString(5, RandomUtil.genInteger(6, 10)));
                    }
                    if (parameterType == LocalDate.class) {
                        method.invoke(obj,RandomUtil.genLocalDate("1999-9-21", "2022-2-22") );
                    }
                    if (parameterType == BigDecimal.class) {
                        //BigDecimal使用时需要新创建一个它的对象然后才能使用
                        method.invoke(obj, new BigDecimal(Math.random() * 10000 + 1000));

                    }
                }


            }

        }

        return obj;
    }

}



            

根据字节码获取对象和方法

  //根据字节码生成对象
        Object obj = clazz.getDeclaredConstructor().newInstance();
        //根据字节码获取类中声明的所有方法
        Method[] methods = clazz.getDeclaredMethods();

通过循环判断来获取方法名称 、参数类型、可以使用random-util调用里面的方法、Bigdecimal使用时需要先创建对象然后再调用赋值

for (Method method : methods) {
            //获取方法的名称
            String methodName = method.getName();
            if (methodName.startsWith("set")) {
                //获取方法形参的参数类型
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    //如果类型范围是Integer则使用方法调用调用Random-util里面的方法
                    if (parameterType == Integer.class) {
                        method.invoke(obj, RandomUtil.genInteger(1000, 9999));
                    }
                    if (parameterType == String.class) {
                        method.invoke(obj, RandomUtil.genString(5, RandomUtil.genInteger(6, 10)));
                    }
                    if (parameterType == LocalDate.class) {
                        method.invoke(obj,RandomUtil.genLocalDate("1999-9-21", "2022-2-22") );
                    }
                    if (parameterType == BigDecimal.class) {
                        //BigDecimal使用时需要新创建一个它的对象然后才能使用
                        method.invoke(obj, new BigDecimal(Math.random() * 10000 + 1000));

                    }
                }


            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值