最近做了一個新的需求,要通過反射機制,獲取A類中的私有子類a,並用該對象構建list<a>並塞入A的對象中。
主要思路是:(1)反射獲取私有類;(2)構建私有類對象並反射插入list;(3)反射調用add方法。
查了挺多資料,但是很少有專門描述這個問題的。其難點主要在於編譯時檢測和參數無法匹配,通過多次反射才解決該問題,記錄如下,直接上代碼。
static void test2() { try { String str = "cn.com.template.data.transactiondata.orpo.TradedDetail"; Class<?> clazz = Class.forName(str); Object newInstance = clazz.newInstance(); for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); // System.out.println(field.getModifiers() + "\t" + field.getType() + "\t" + field.getName()); if (List.class.getName().equals(field.getType().getName())) { Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) type; String sublist = t.getActualTypeArguments()[0].getTypeName(); // System.out.println(">>>>>>>>>>>" + sublist); // 構建重復組,加載類 Class<?> list_clz = Class.forName(sublist); // System.out.println(list_clz.newInstance()); // 建立對象列表//創建對象list(List同樣反射,越過泛型檢查) List list = new ArrayList(); Method m = list.getClass().getMethod("add", Object.class); m.invoke(list, list_clz.newInstance()); // 調用set方法,塞入重復組 String methodName = "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1); Method method = clazz.getMethod(methodName, List.class); method.invoke(newInstance, list); } } } // 打印結構體的json字符串 System.out.println(newInstance.toString()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } }