Java泛型:利用泛型动态确认方法返回值类型
程序员文章站
2022-06-09 13:30:05
...
根据泛型类型动态返回对象
public T unPackMessage(String interfaceCode, String respValue, Class clazz, String sysType) throws IOException {
log.info(">> unPackMessage start, interfaceCode is {}, respValue is {}", interfaceCode, respValue);
Map result = null;
// get pack_convert.prop file content
PackConvertMessage packConvertMessage = getPackConvertFileContent();
String func = packConvertMessage.getInterfaceMapping().get(interfaceCode);
if (StringUtils.isBlank(func)){
log.error("<< unPackMessage error, No response message with current interface configured");
return null;
}
String respSerialNumber = packConvertMessage.getRespFuncMapping().get(func);
String currPackType = PackConvertConstant.SystermType.CLIENT.equals(sysType) ? client_pack_type : pack_type;
switch (currPackType){
case PackConvertConstant.PackType.NON_FIXED_LENGTH:
result = unpackNoFixedMessage(respValue, connect_char, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR, sysType);
break;
case PackConvertConstant.PackType.FIXED_LENGTH:
result = unpackFixedMessage(respValue, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR);
break;
case PackConvertConstant.PackType.XML:
result = unpackXmlMessage(respValue, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR);
break;
case PackConvertConstant.PackType.JSON:
result = unpackJsonMessage(respValue, respSerialNumber);
break;
}
log.info("<< unPackMessage success, return value is {}", JSON.toJSONString(result));
T t = JSON.toJavaObject(JSON.parseObject(JSON.toJSONString(result)), clazz);
return t;
}
根据泛型类型动态返回集合
public List convertFileToObj(String localFilePath, Class clazz, Class tclazz) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
logger.info(">> convertFileToObj, localFilePath is {}, clazz is {}", localFilePath, clazz.getName());
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(localFilePath),"utf-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null){
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
List result = messageToObj(sb.toString(), clazz);
logger.info("<< convertFileToObj end");
return result;
}
public List convertFileToObj(String localFilePath, Class clazz) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
logger.info(">> convertFileToObj, localFilePath is {}, clazz is {}", localFilePath, clazz.getName());
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(localFilePath),"utf-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null){
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
List result = messageToObj(sb.toString(), clazz);
logger.info("<< convertFileToObj end");
return result;
}
泛型方法基本介绍
public与返回值中间的非常重要,表示此方法声明为泛型方法
只有声明了的方法才是泛型方法,泛型类中使用了泛型的成员方法并不能算是泛型方法
表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T
与泛型类的定义一样,T可以随便写成任意标识,比如T、E、K、V、S等
泛型限制上下边界
泛型限制上边界:传入的类型只能是指定类的子类,比如Class
泛型限制下边界:传入的类型只能是指定类的父类,比如DiagnosticListener
另外附常用泛型标识说明
常见的一些泛型标识就是T(类型)、E(元素)、K(key)、V(value)、?(通配符,不确定的)这几个,其实填任何字母都行
上一篇: 显式指定Java泛型方法返回值和输入参数类型的优点
下一篇: 大话设计模式笔记(九)の外观模式