zoukankan      html  css  js  c++  java
  • java 代码分享

    以下代码可以在有“代码混淆器,混淆后正常工作”,希望分享给有可以使用到的人,(*^__^*) 嘻嘻

    /*

    * HashMapToPo 转换为集合对象
    */
    public static <T> ArrayList<T> HashMapToPoList(
    final ArrayList<HashMap<String, Object>> hmList,
    final Class<T> clazz, boolean encode, boolean isEncode)
    throws ForerDealArgumentException {
    if (hmList == null || clazz == null) {
    String msg = className
    + ".HashMapToPoList(final ArrayList<HashMap<String, Object>> hmList,final Class<?> clazz,boolean encode) Args is not null";
    logger.error(msg);
    throw new ForerDealArgumentException(msg);
    }
    final HashMap<String, String> dic = GetObjectPropertyName(clazz);
    final HashMap<String, Method> dic2 = GetObjectMethodName(clazz);
    final ArrayList<T> lt = new ArrayList<T>();
    for (HashMap<String, Object> hm : hmList) {
    T o = null;
    try {
    o = clazz.newInstance();
    } catch (final Exception e) {
    e.printStackTrace();
    }
    for (Map.Entry<String, Object> entry : hm.entrySet()) {
    String key = entry.getKey().toUpperCase().replace("_", "");
    Object value = entry.getValue();
    if (dic.containsKey(key) && value != null) {
    try {
    String str = dic.get(key);
    String temp = str.substring(0, 1);
    str = str.replaceFirst(temp, temp.toUpperCase());
    str = "set" + str;
    Class<?> paraType = dic2.get(str).getParameterTypes()[0];
    if (paraType != value.getClass())// 类型一致
    value = StringToObject(paraType, value.toString());
    if (isEncode) {
    if (encode) {// iso8859-1 编码
    if (paraType == String.class) {
    value = CharsetConvert.charsetConvert(value
    .toString());
    }
    } else {// GBK
    if (paraType == String.class) {
    value = CharsetConvert
    .ISO_8859_1ToGBK(value.toString());
    }
    }
    }
    try {
    dic2.get(str).invoke(o, value);
    } catch (Exception e) {
    logger.error("paraType:" + paraType + ";valueType:"
    + value.getClass() + "[" + str + ":"
    + value + "]");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } else if (!dic.containsKey(key)) {
    String msg = clazz.getName()
    + "Object does not contain the '" + key + "' field";
    logger.debug(msg);
    }
    }
    lt.add(o);
    }
    return lt;
    }
    /*
    * 字段类型转换
    */
    private static Object StringToObject(Class<?> clazz, String str) {
    Object o = str;
    if (clazz == Date.class && str != null && str != "") {
    DateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
    o = dt1.parse(str);
    } catch (ParseException e1) {
    DateFormat dt2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    try {
    o = dt2.parse(str);
    } catch (ParseException e2) {
    DateFormat dt3 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    try {
    o = dt3.parse(str);
    } catch (ParseException e3) {
    DateFormat dt4 = new SimpleDateFormat("yyyyMMdd");
    try {
    o = dt4.parse(str);
    } catch (ParseException e4) {
    e4.printStackTrace();
    }
    }
    }
    }
    } else if (clazz == BigDecimal.class) {
    o = new BigDecimal(str);
    } else if (clazz == Long.class) {
    o = new Long(str);
    } else if (clazz == Integer.class) {
    o = new Integer(str);
    } else if (clazz == int.class) {
    o = Integer.parseInt(str);
    } else if (clazz == float.class) {
    o = Float.parseFloat(str);
    } else if (clazz == boolean.class) {
    o = Boolean.parseBoolean(str);
    } else if (clazz == byte.class) {
    o = Byte.parseByte(str);
    }
    return o;
    }
    /*
    * HashMap转换为单一对象
    */
    public static <T> T HashMapToSinglePo(final HashMap<String, Object> hm,
    final Class<T> clazz, boolean encode, boolean isEncode)
    throws Exception {
    if (hm == null || clazz == null) {
    String msg = className
    + ".HashMapToSinglePo(final ArrayList<HashMap<String, Object>,boolean encode> hmList,final Class<T> clazz) Args is  null";
    logger.error(msg);
    }
    T o = null;
    ArrayList<HashMap<String, Object>> hmList = new ArrayList<HashMap<String, Object>>();
    hmList.add(hm);
    ArrayList<T> lt = HashMapToPoList(hmList, clazz, encode, isEncode);
    if (lt != null && lt.size() > 0) {
    o = lt.get(0);
    }
    return o;
    }
    /*
    * 对象比较 ,结果HashMap<String, String> key 为checkColums 字段,value 相同 Same
    * ,不同Different,不存在Object does not contain the
    */
    public static <T> HashMap<String, String> Compare(Class<T> clazz, T o1,
    T o2, String[] checkColums) {
    HashMap<String, String> ht = new HashMap<String, String>();
    if (clazz == null || o1 == null || o2 == null) {
    String msg = className
    + ".Compare(Class<T> clazz,T o1, T o2,List<String> checkColums) Args clazz,o1,o2 is null";
    logger.error(msg);
    }
    final HashMap<String, String> dic1 = GetObjectPropertyName(clazz);
    final HashMap<String, Method> dic2 = GetObjectMethodName(clazz);
    if (checkColums == null) {
    int count = dic1.size();
    checkColums = new String[count];
    dic1.values().toArray(checkColums);
    }
    for (String str : checkColums) {
    try {
    String temp = str.toUpperCase();
    if (dic1.containsKey(temp)) {
    String str1 = dic1.get(temp);
    String temp1 = str1.substring(0, 1);
    str1 = str1.replaceFirst(temp1, temp1.toUpperCase());
    str1 = "get" + str1;
    Object result1 = dic2.get(str1).invoke(o1);
    Object result2 = dic2.get(str1).invoke(o2);
    if (result1 != null || result2 != null) {
    if (result1 != null && result2 != null) {
    if (!result1.equals(result2)) {
    ht.put(str, "Different");
    } else {
    ht.put(str, "Same");
    }
    } else {
    ht.put(str, "Different");
    }
    }
    } else {
    String msg = clazz.getName()
    + " Object does not contain the '" + str
    + "' field";
    ht.put(str, msg);
    logger.debug(msg);
    }
    } catch (Exception e) {
    e.printStackTrace();
    logger.equals(e);
    }
    }
    return ht;
    }
    /*
    * 对象克隆
    */
    public static <T> T Clone(Class<T> clazz, T o) {
    if (o == null || clazz == null) {
    String msg = className
    + ".Clone(Class<T> clazz,Object o) Args clazz or o is null";
    logger.error(msg);
    }
    T cloneObject = null;
    try {
    cloneObject = clazz.newInstance();
    } catch (final Exception e) {
    e.printStackTrace();
    }
    final HashMap<String, String> dic = GetObjectPropertyName(clazz);
    final HashMap<String, Method> dic2 = GetObjectMethodName(clazz);
    for (Map.Entry<String, String> entry : dic.entrySet()) {
    try {
    String str = entry.getValue();
    String temp = str.substring(0, 1);
    str = str.replaceFirst(temp, temp.toUpperCase());
    String str1 = "get" + str;
    String str2 = "set" + str;
    if (dic2.containsKey(str1) && dic2.containsKey(str2)) {
    Object result = dic2.get(str1).invoke(o);
    dic2.get(str2).invoke(cloneObject, result);
    } else {
    String msg = str1 + ";" + str2 + "Error";
    System.out.println(msg);
    logger.error(msg);
    }
    } catch (Exception e) {
    e.printStackTrace();
    logger.error(e);
    }
    }
    return cloneObject;
    }
    /*
    * 反射获取对象字段
    */
    private static HashMap<String, HashMap<String, String>> propertys = new HashMap<String, HashMap<String, String>>();
    private static HashMap<String, HashMap<String, Method>> methods = new HashMap<String, HashMap<String, Method>>();
    private static HashMap<String, String> GetObjectPropertyName(Class<?> clazz) {
    HashMap<String, Method> method = GetObjectMethodName(clazz);
    String className = clazz.getName();
    HashMap<String, String> ht = new HashMap<String, String>();
    if (propertys.containsKey(className)) {
    ht = propertys.get(className);
    } else {
    ht = new HashMap<String, String>();
    getPropertys(clazz, ht, method);// 递归父类
    propertys.put(className, ht);
    }
    return ht;
    }
    private static HashMap<String, String> getPropertys(Class<?> clazz,
    HashMap<String, String> ht, HashMap<String, Method> method) {
    Method[] md = clazz.getMethods();
    for (int i = 0; i < md.length; i++) {
    String str = md[i].getName().replace("get", "").replace("set", "");
    if (method.containsKey("get" + str)
    && method.containsKey("set" + str)) {
    if (!ht.containsKey(str)) {
    ht.put(str.toUpperCase(), str);
    }
    }
    }
    Class<?> superClazz = clazz.getSuperclass();
    if (superClazz != null && superClazz != Object.class) {
    getPropertys(superClazz, ht, method);
    }
    return ht;
    }
    /*
    * 反射获取类的方法名
    */
    private static HashMap<String, Method> GetObjectMethodName(Class<?> clazz) {
    HashMap<String, Method> ht = null;
    String className = clazz.getName();
    if (methods.containsKey(className)) {
    ht = methods.get(className);
    } else {
    ht = new HashMap<String, Method>();
    getMethods(clazz, ht);// 递归父类
    methods.put(className, ht);
    }
    return ht;
    }
    private static HashMap<String, Method> getMethods(Class<?> clazz,
    HashMap<String, Method> ht) {
    Method[] md = clazz.getMethods();
    for (int i = 0; i < md.length; i++) {
    ht.put(md[i].getName(), md[i]);
    }
    Class<?> superClazz = clazz.getSuperclass();
    if (superClazz != null && superClazz != Object.class) {
    getMethods(superClazz, ht);
    }
    return ht;
    }
  • 相关阅读:
    基础知识梳理
    计算机基础
    IAR平台下使用STM32的DSP配置方法
    第五节:STM32输入捕获(用CubeMX学习STM32)
    第四节:定时器中断及定时器产生PWM(用CubeMX学习STM32)
    第三节: 串口通信(用CubeMX学习STM32)
    第二节: 外部中断学习(用CubeMX学习STM32)
    第一节补充: 按键操作(CubeMX加HAL库学STM32系列)
    第一节:用Cube学32之简单IO口操作(点灯及按键)
    STM32程序中使用printf打印中文字符乱码
  • 原文地址:https://www.cnblogs.com/flex/p/2413589.html
Copyright © 2011-2022 走看看