java反射遍歷實體類屬性和類型,並賦值和獲取值


/*
* GetModelNameAndType.java
* Version 1.0.0
* Created on 2017年12月15日
* Copyright ReYo.Cn
*/
package reyo.sdk.utils.reflect;

/**    
* <B>創  建 人:</B>AdministratorReyoAut <BR>
* <B>創建時間:</B>2017年12月15日 下午9:20:44<BR>
* 
* @author ReYo
* @version 1.0
*/
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/** 
 * 獲取實體類型的屬性名和類型 
 * @param model 為實體類 
 * @author kou  為傳入參數 
 */
public class GetModelNameAndType {

    public static void testReflect(Object model) throws SecurityException, NoSuchMethodException,
            IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 獲取實體類的所有屬性,返回Field數組  
        Field[] field = model.getClass().getDeclaredFields();
        // 獲取屬性的名字  
        String[] modelName = new String[field.length];
        String[] modelType = new String[field.length];
        for (int i = 0; i < field.length; i++) {
            // 獲取屬性的名字  
            String name = field[i].getName();
            modelName[i] = name;
            // 獲取屬性類型  
            String type = field[i].getGenericType().toString();
            modelType[i] = type;

            //關鍵。。。可訪問私有變量  
            field[i].setAccessible(true);
            //給屬性設置  
            field[i].set(model, field[i].getType().getConstructor(field[i].getType()).newInstance("kou"));

            // 將屬性的首字母大寫  
            name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());

            if (type.equals("class java.lang.String")) {
                // 如果type是類類型,則前面包含"class ",后面跟類名  
                Method m = model.getClass().getMethod("get" + name);
                // 調用getter方法獲取屬性值  
                String value = (String) m.invoke(model);
                if (value != null) {

                    System.out.println("attribute value:" + value);
                }

            }
            if (type.equals("class java.lang.Integer")) {
                Method m = model.getClass().getMethod("get" + name);
                Integer value = (Integer) m.invoke(model);
                if (value != null) {
                    System.out.println("attribute value:" + value);
                }
            }
            if (type.equals("class java.lang.Short")) {
                Method m = model.getClass().getMethod("get" + name);
                Short value = (Short) m.invoke(model);
                if (value != null) {
                    System.out.println("attribute value:" + value);
                }
            }
            if (type.equals("class java.lang.Double")) {
                Method m = model.getClass().getMethod("get" + name);
                Double value = (Double) m.invoke(model);
                if (value != null) {
                    System.out.println("attribute value:" + value);
                }
            }
            if (type.equals("class java.lang.Boolean")) {
                Method m = model.getClass().getMethod("get" + name);
                Boolean value = (Boolean) m.invoke(model);
                if (value != null) {
                    System.out.println("attribute value:" + value);
                }
            }
            if (type.equals("class java.util.Date")) {
                Method m = model.getClass().getMethod("get" + name);
                Date value = (Date) m.invoke(model);
                if (value != null) {
                    System.out.println("attribute value:" + value.toLocaleString());
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            testReflect(new VO());
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM