package com.pwt.netty.util;


import org.apache.commons.lang3.RandomStringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ObjUtil {

    public static Object getProperty(Object obj) throws IllegalAccessException
    {
        //利用反射获取类属性
        Field[] field = obj.getClass().getDeclaredFields();
        for (Field f : field)
        {
            f.setAccessible(true);
            // key:得到属性名
            String key = f.getName();
            //获取属性名
            String name = f.getName();
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            //获取属性类型
            String type = f.getGenericType().toString();
            Method method = null;
            try
            {
                try
                {
                    switch (type)
                    {
                        case "class java.lang.String":
                            method = obj.getClass().getMethod("set" + name, String.class);
                            String randomStr = "";
                            if (method.toString().contains("setBatteryNumber"))
                            {
                                randomStr = RandomStringUtils.randomNumeric(24);
                            } else
                            {
                                randomStr = RandomStringUtils.randomNumeric(8);
                            }
                            method.invoke(obj, randomStr);
                            break;
                        case "byte":
                            method = obj.getClass().getMethod("set" + name, byte.class);
                            byte v = 0x01;
                            method.invoke(obj, v);
                            break;
                        case "int":
                            method = obj.getClass().getMethod("set" + name, int.class);
                            method.invoke(obj, 2);
                            break;
                        case "short":
                            method = obj.getClass().getMethod("set" + name, short.class);
                            short s = 10;
                            method.invoke(obj, s);
                            break;
                        case "float":
                            method = obj.getClass().getMethod("set" + name, float.class);
                            float ff = 10f;
                            method.invoke(obj, ff);
                            break;
                        case "double":
                            method = obj.getClass().getMethod("set" + name, double.class);
                            double d = 10;
                            method.invoke(obj, d);
                            break;
                    }
                    //其他类型还未测试，所以没有写出
                } catch (InvocationTargetException e)
                {
                    e.printStackTrace();
                }
            } catch (NoSuchMethodException e)
            {
                e.printStackTrace();
            }
        }
        return obj;
    }
}
