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

com.gitee.sunchenbin.mybatis.actable.utils.FieldUtils Maven / Gradle / Ivy

Go to download

A.CTable is a Maven project based on Spring and Mybatis, which enhances the function of Mybatis

There is a newer version: 1.5.0.RELEASE
Show newest version
package com.gitee.sunchenbin.mybatis.actable.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FieldUtils {

    public static  Field getKeyField(T obj) {
        Field[] allFields = getAllFields(obj);
        for (Field field : allFields){
            // 设置访问权限
            field.setAccessible(true);
            if(ColumnUtils.isKey(field,obj.getClass())){
                return field;
            }
        }
        return null;
    }

    public static  Field[] getAllFields(T obj) {
        Field[] declaredFields = obj.getClass().getDeclaredFields();

        // 递归扫描父类的filed
        declaredFields = recursionParents(obj.getClass(), declaredFields);
        return declaredFields;
    }

    /**
     * 递归扫描父类的fields
     * @param clas
     * @param fields
     */
    @SuppressWarnings("rawtypes")
    public static  Field[] recursionParents(Class clas, Field[] fields) {
        if(clas.getSuperclass()!=null){
            Class clsSup = clas.getSuperclass();
            List fieldList = new ArrayList();
            fieldList.addAll(Arrays.asList(fields));
            fieldList.addAll(Arrays.asList(clsSup.getDeclaredFields()));
            fields = new Field[fieldList.size()];
            int i = 0;
            for (Object field : fieldList.toArray()) {
                fields[i] = (Field) field;
                i++;
            }
            fields = recursionParents(clsSup, fields);
        }
        return fields;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy