Java泛型:利用泛型动态确认方法返回值类型

根据泛型类型动态返回对象

public <T extends PackObject> T  unPackMessage(String interfaceCode, String respValue, Class<T>  clazz, String sysType) throws IOException {
    log.info(">> unPackMessage start, interfaceCode is {}, respValue is {}", interfaceCode, respValue);
    Map<String, Object> 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 <E extends BaseLiquidation, T extends BaseLiquidation> List<E> convertFileToObj(String localFilePath, Class<E> clazz, Class<T> 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<E> result = messageToObj(sb.toString(), clazz);
     logger.info("<< convertFileToObj end");
     return result;
}

 

public <E extends BaseLiquidation> List<E> convertFileToObj(String localFilePath, Class<E> 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<E> result = messageToObj(sb.toString(), clazz);
    logger.info("<< convertFileToObj end");
    return result;
}

泛型方法基本介绍

  • public与返回值中间的<T>非常重要,表示此方法声明为泛型方法
  • 只有声明了<T>的方法才是泛型方法,泛型类中使用了泛型的成员方法并不能算是泛型方法
  • <T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T
  • 与泛型类的定义一样,T可以随便写成任意标识,比如T、E、K、V、S等

泛型限制上下边界

  • 泛型限制上边界:传入的类型只能是指定类的子类,比如Class<? extend PackObject>
  • 泛型限制下边界:传入的类型只能是指定类的父类,比如DiagnosticListener<? super JavaFileObject>

另外附常用泛型标识说明

  • 常见的一些泛型标识就是T(类型)、E(元素)、K(key)、V(value)、?(通配符,不确定的)这几个,其实填任何字母都行

 

posted @ 2019-07-17 14:21  hy_wx  阅读(29249)  评论(0编辑  收藏  举报