package com.justbon.bestsign.common.contract.client; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException { Class<?> cat = Class.forName("com.justbon.bestsign.common.contract.client.cat"); cat OBJ = (com.justbon.bestsign.common.contract.client.cat) cat.newInstance(); Field[] fields = cat.getFields(); for (Field f : fields) { if (f.getName().equals("arrayListoo")) { try { // ====== List arr=new ArrayList<String>(); Method method01 = arr.getClass().getMethod("add", Object.class); method01.invoke(arr,"123456"); arr.forEach(e->{ System.out.println(e.toString()); }); // 这里的f.getClass()就是cat来里面的arrayListoo,但是添加会报错 Class<? extends Field> aClass = f.getClass(); Class<?> type = f.getType(); Method method = type.getMethod("add", Object.class); method.invoke(OBJ.arrayListoo, "123456"); OBJ.arrayListoo.forEach(e -> { System.out.println("======================"); System.out.println(e); }); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } package com.fanshe; import java.util.ArrayList; import java.util.List; public class cat { public List arrayListoo=new ArrayList<String>(); public void getcat(){ arrayListoo.add("1"); } }