java调用未知类的指定方法
/**
* 获取任意对象里面的returnMapObject的值.并且组成list
* @param <T>
* @param list
* @param fields
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static <T> List<Map<String,Object>>
returnMapList(List<T> list,String ...fields) throws Exception{
List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
if(list == null || list.isEmpty())return mapList;
for(T t : list){
Method m = t.getClass().getMethod("returnMapObject",fields.getClass());
Object result = m.invoke(t,(Object)fields);
mapList.add((Map<String, Object>) result);
}
return mapList;
}
有兴趣可以去看看jsonObject(Object o)的源码.用来学习挺合适的