Java反射时如何获取返回值类型, 如何处理List类型的返回参数
作者:互联网
举个例子, 如上图中所示, 两种返回类型一种是List<T>, 一种是对象<T>时, 在写通用代码的时候应该怎么去处理
如果是List
private Object doSelect(SlsSelect select, SlsTable table, Method method, Object[] args) throws ClassNotFoundException, IllegalAccessException, LogException, InstantiationException {
String hql = select.value();
String project = table.project();
String logStore = table.logStore();
if(StringUtils.isBlank(hql) || StringUtils.isBlank(project) || StringUtils.isBlank(logStore)) {
return null;
}
ConcurrentHashMap<String, Object> actualParamsMap = getActualParamsMap(method, args);
String newSQL = questionMark(hql, actualParamsMap);
System.out.println("newSQL=" + newSQL);
Class<?> returnType = method.getReturnType();
boolean asList = Collection.class.isAssignableFrom(returnType);
if(asList) {
return doList(method, actualParamsMap, project, logStore, newSQL);
} else {
return doOne(method, actualParamsMap, project, logStore, newSQL, returnType);
}
}
private Object doList(Method method, ConcurrentHashMap<String, Object> actualParamsMap, String project, String logStore, String newSQL) throws ClassNotFoundException, LogException, IllegalAccessException, InstantiationException {
System.out.println("");
List<Object> list = Collections.synchronizedList(new ArrayList<>());
ParameterizedType parameterizedType = (ParameterizedType) method.getAnnotatedReturnType();
Type type = parameterizedType.getActualTypeArguments()[0];
Class<?> clazz = Class.forName(type.getTypeName());
Client client = SpringContextUtil.getBean("slsClient");
List<Map<String, String>> contents = selectList(client, project, logStore, newSQL, (Integer) actualParamsMap.getOrDefault("from", 0), (Integer)actualParamsMap.getOrDefault("to", 0));
for(Map<String, String> content : contents) {
Object record = clazz.newInstance();
Field[] fields = record.getClass().getDeclaredFields();
for(Field field : fields) {
String fieldName = field.getName();
Object fieldValue = content.get(fieldName);
field.setAccessible(true);
field.set(record, fieldValue);
}
list.add(record);
}
return list;
}
如果是单个对象
private Object doOne(Method method, ConcurrentHashMap<String, Object> actualParamsMap, String project, String logStore, String newSQL, Class<?> returnType) throws LogException, IllegalAccessException, InstantiationException {
Field[] declaredFields = returnType.getDeclaredFields();
Client client = SpringContextUtil.getBean("slsClient");
List<Map<String, String>> contents = selectList(client, project, logStore, newSQL, (Integer) actualParamsMap.getOrDefault("from", 0), (Integer)actualParamsMap.getOrDefault("to", 0));
if(contents.size() == 0) {
return null;
}
Map<String, String> content = contents.get(0);
Object record = returnType.newInstance();
for(Field field : declaredFields) {
String fieldName = field.getName();
Object fieldValue = content.get(fieldName);
field.setAccessible(true);
field.set(record, fieldValue);
}
return record;
}
这是我参考Mybatis的框架写的基于阿里云Sls日志服务写的查询框架, 完整源码太多了没有办法贴, 有需要的化可以评论区留下邮箱.
标签:Java,String,logStore,Object,List,project,newSQL,类型,actualParamsMap 来源: https://blog.csdn.net/qq_29777207/article/details/117397151