2016年6月13日更新
不写代码就不知道自己到底有多笨
百度了很久,都好像没有找到一个将Map转变成Class的方法(也许太简单,别人根本不屑去写),so,自力更生
该类主要实现将Map转变成Class,其中Map的key是Class中的属性,Map的value是要设置的Class的属性值,主要方法如下:
package com.essayants.utils;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONObject;
/**
* Map的公共方法类
* @author yuan_hj
*
*/
public class MapUtils {
/**
* 将map转变成Class
* @param map
* @param cls
* @return
* @throws Exception
*/
public static <T> T changeMap2Class(Map<String, Object> map,Class<T> cls){
try {
T mycls = cls.newInstance();
for (Entry<String, Object> entry : map.entrySet()) {
String pro = entry.getKey();
String firstLetter = pro.substring(0, 1).toUpperCase();
String lastLetter = pro.substring(1);
if (lastLetter != null && lastLetter.contains("_")) {
lastLetter = lastLetter.split("_")[0]
+ lastLetter.split("_")[1].substring(0, 1).toUpperCase()
+ lastLetter.split("_")[1].substring(1);
}
String setter = "set" + firstLetter + lastLetter;
Method method = mycls.getClass().getMethod(setter, new Class[] {entry.getValue().getClass()});
method.invoke(mycls, new Object[] {entry.getValue()});
}
return mycls;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将Map转变成JSON
* @param map
* @return
*/
public static JSONObject changeMap2JSON(Map<String, Object> map){
return JSONObject.fromObject(map);
}
}
改方法处理a_b这种类型是可以的,但a_b_c甚至是a_b_c_d的就出现bug了,网上找了一个驼峰法代码,
package com.essayants.utils;
/**
* 驼峰法,用于字符的处理
* @author yuan_hj
*
*/
public class CamelCaseUtils {
private static final char SEPARATOR = '_';
public static String toUnderlineName(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder();
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
boolean nextUpperCase = true;
if (i < (s.length() - 1)) {
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
}
if ((i >= 0) && Character.isUpperCase(c)) {
if (!upperCase || !nextUpperCase) {
if (i > 0) sb.append(SEPARATOR);
}
upperCase = true;
} else {
upperCase = false;
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
public static String toCapitalizeCamelCase(String s) {
if (s == null) {
return null;
}
s = toCamelCase(s);
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
public static void main(String[] args) {
System.out.println(CamelCaseUtils.toUnderlineName("ISOCertifiedStaff"));
System.out.println(CamelCaseUtils.toUnderlineName("CertifiedStaff"));
System.out.println(CamelCaseUtils.toUnderlineName("UserID"));
System.out.println(CamelCaseUtils.toCamelCase("iso_certified_staff"));
System.out.println(CamelCaseUtils.toCamelCase("certified_staff"));
System.out.println(CamelCaseUtils.toCamelCase("user_id_ss"));
}
}
只需修改MapUtils中的这一段代码即可
String pro = CamelCaseUtils.toCamelCase(entry.getKey());
String firstLetter = pro.substring(0, 1).toUpperCase();
String lastLetter = pro.substring(1);
String setter = "set" + firstLetter + lastLetter;
Method method = mycls.getClass().getMethod(setter, new Class[] {entry.getValue().getClass()});
method.invoke(mycls, new Object[] {entry.getValue()});