java开发中常用到小方法

ArrayList转数组

/*
   * 获取优惠券全部策略ID String[]
   */
  private String[] getStrategyIDArrayCache() throws Exception {
    ArrayList<String> list = new ArrayList<String>();
    String[] strArray = new String[] {};
    int RightsType = BusinessEmun.RightsType.优惠券.getValue();
    List<McRights> resultList = cacheManagement.rightsListCache.getData(RightsType);

    if (resultList != null && resultList.size() > 0) {
      for (McRights mcRights : resultList) {
        list.add(mcRights.getStrategyID());
      }

      if (list.size() > 0) {
        strArray = (String[]) list.toArray(new String[0]);
      }
    }

    return strArray;
  }

验证是否在String[] arr范围内

/** * 验证是否在String[] arr范围内 * * @Title: IsContains * @Description: TODO * @param * @return * @throws */
  private boolean isContains(String[] arr, String targetValue) {
    int a = Arrays.binarySearch(arr, targetValue);
    if (a >= 0)
      return true;
    else
      return false;
  }

根据属性名获取属性值

/** * @Title: getFieldValueByName * @Description: 根据属性名获取属性值 * @param obj:需要查找的对象,fieldName:对象属性 * @return 得到结果对象 * @throws */
    private  Object getFieldValueByName(Object obj, String fieldName) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = obj.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(obj, new Object[]{});
            return value;
        } catch (Exception e) {
            Logger.error("getFieldValueByName异常", e);
            return null;
        }
    }
相关文章

相关标签/搜索