All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.zhxu.bp.utils.Reflections Maven / Gradle / Ivy

The newest version!
package cn.zhxu.bp.utils;

import cn.zhxu.xjson.JsonKit;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Reflections {

    public static List getAllDeclaredFields(Class clazz) {
        List fields = new ArrayList<>();
        Set names = new HashSet<>();
        while (clazz != Object.class) {
            for (Field field : clazz.getDeclaredFields()) {
                int modifiers = field.getModifiers();
                String name = field.getName();
                if (field.isSynthetic() || Modifier.isStatic(modifiers)
                        || Modifier.isTransient(modifiers)
                        || names.contains(name)) {
                    continue;
                }
                fields.add(field);
                names.add(name);
            }
            clazz = clazz.getSuperclass();
        }
        return fields;
    }

    public static  T newInstance(Class clazz, List fields, String[] values) {
        if (fields.size() != values.length) {
            throw new IllegalArgumentException("fields 与 values 的个数不匹配");
        }
        T bean = newInstance(clazz);
        for (int i = 0; i < values.length; i++) {
            Field field = fields.get(i);
            String value = values[i];
            if (value != null) {
                if (StringUtils.isBlank(value) && field.getType() != String.class) {
                    continue;
                }
                field.setAccessible(true);
                try {
                    field.set(bean, convert(field, value));
                } catch (ReflectiveOperationException e) {
                    throw new RuntimeException(
                            "A exception occurred when setting value to [" + clazz.getName() + "#" + field.getName() + "], please check whether it's setter is correct.", e);
                }
            }
        }
        return bean;
    }

    public static Object convert(Field field, String value) {
        Class type = field.getType();
        if (type == String.class) {
            return value;
        }
        if (type == int.class || type == Integer.class) {
            return Integer.parseInt(value);
        }
        if (type == long.class || type == Long.class) {
            return Long.parseLong(value);
        }
        if (type == float.class || type == Float.class) {
            return Float.parseFloat(value);
        }
        if (type == double.class || type == Double.class) {
            return Double.parseDouble(value);
        }
        if (type == short.class || type == Short.class) {
            return Short.parseShort(value);
        }
        if (type == byte.class || type == Byte.class) {
            return Byte.parseByte(value);
        }
        if (type == BigDecimal.class) {
            return new BigDecimal(value);
        }
        if (List.class.isAssignableFrom(type)) {
            Type genericType = field.getGenericType();
            if (genericType instanceof ParameterizedType) {
                Type itemType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
                return JsonKit.toList((Class) itemType, value);
            }
        }
        return JsonKit.toBean(type, value);
    }

    public static  T newInstance(Class beanClass) {
        try {
            return beanClass.getDeclaredConstructor().newInstance();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Can not instantiate [" + beanClass.getName() +
                    "], please check whether there is a constructor without parameters on it.", e);
        } catch (Exception e) {
            throw new RuntimeException("Can not instantiate [" + beanClass.getName() +
                    "], please check whether the constructor without parameters can be invoked without errors.", e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy