MyBeanUtils.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. package com.kmall.common.utils;
  2. import org.apache.commons.beanutils.DynaBean;
  3. import org.apache.commons.beanutils.DynaProperty;
  4. import org.apache.commons.beanutils.PropertyUtils;
  5. import org.apache.commons.beanutils.PropertyUtilsBean;
  6. import java.beans.PropertyDescriptor;
  7. import java.lang.annotation.Annotation;
  8. import java.lang.reflect.Field;
  9. import java.lang.reflect.InvocationTargetException;
  10. import java.lang.reflect.Method;
  11. import java.math.BigDecimal;
  12. import java.math.BigInteger;
  13. import java.util.*;
  14. /**
  15. * bean 对象工具类
  16. * 描述:<br>
  17. *
  18. * @author Scott
  19. * @version 1.0
  20. * @since 1.0.0
  21. */
  22. public class MyBeanUtils extends PropertyUtilsBean {
  23. /**
  24. * 主要功能:转换
  25. * 注意事项:无
  26. *
  27. * @param dest 结果对象
  28. * @param orig 来源对象
  29. * @throws IllegalAccessException 异常
  30. * @throws InvocationTargetException 异常
  31. */
  32. @SuppressWarnings("all")
  33. private static void convert(Object dest, Object orig)
  34. throws IllegalAccessException,
  35. InvocationTargetException {
  36. // Validate existence of the specified beans
  37. if (dest == null) {
  38. throw new IllegalArgumentException("No destination bean specified");
  39. }
  40. if (orig == null) {
  41. throw new IllegalArgumentException("No origin bean specified");
  42. }
  43. // Copy the properties, converting as necessary
  44. if (orig instanceof DynaBean) {
  45. DynaProperty[] origDescriptors = ((DynaBean) orig).getDynaClass().getDynaProperties();
  46. for (int i = 0; i < origDescriptors.length; i++) {
  47. String name = origDescriptors[i].getName();
  48. if (PropertyUtils.isWriteable(dest, name)) {
  49. Object value = ((DynaBean) orig).get(name);
  50. try {
  51. getInstance().setSimpleProperty(dest, name, value);
  52. } catch (Exception e) {
  53. ; // Should not happen
  54. }
  55. }
  56. }
  57. } else if (orig instanceof Map) {
  58. Iterator names = ((Map) orig).keySet().iterator();
  59. while (names.hasNext()) {
  60. String name = (String) names.next();
  61. if (PropertyUtils.isWriteable(dest, name)) {
  62. Object value = ((Map) orig).get(name);
  63. try {
  64. getInstance().setSimpleProperty(dest, name, value);
  65. } catch (Exception e) {
  66. ; // Should not happen
  67. }
  68. }
  69. }
  70. } else { /* if (orig is a standard JavaBean) */
  71. PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(
  72. orig);
  73. for (int i = 0; i < origDescriptors.length; i++) {
  74. String name = origDescriptors[i].getName();
  75. // String type = origDescriptors[i].getPropertyType().toString();
  76. if ("class".equals(name)) {
  77. continue; // No point in trying to set an object's class
  78. }
  79. if (PropertyUtils.isReadable(orig, name)
  80. && PropertyUtils.isWriteable(dest, name)) {
  81. try {
  82. Object value = PropertyUtils.getSimpleProperty(orig,
  83. name);
  84. getInstance().setSimpleProperty(dest, name, value);
  85. } catch (IllegalArgumentException ie) {
  86. ; // Should not happen
  87. } catch (Exception e) {
  88. ; // Should not happen
  89. }
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * 对象拷贝
  96. * 数据对象空值不拷贝到目标对象
  97. *
  98. * @param databean 数据对象
  99. * @param tobean 返回对象
  100. * @throws Exception 异常
  101. */
  102. public static void copyBeanNotNull2Bean(Object databean, Object tobean)
  103. throws Exception {
  104. PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(
  105. databean);
  106. for (int i = 0; i < origDescriptors.length; i++) {
  107. String name = origDescriptors[i].getName();
  108. // String type = origDescriptors[i].getPropertyType().toString();
  109. if ("class".equals(name)) {
  110. continue; // No point in trying to set an object's class
  111. }
  112. if (PropertyUtils.isReadable(databean, name)
  113. && PropertyUtils.isWriteable(tobean, name)) {
  114. try {
  115. Object value = PropertyUtils.getSimpleProperty(databean,
  116. name);
  117. if (value != null) {
  118. getInstance().setSimpleProperty(tobean, name, value);
  119. }
  120. } catch (IllegalArgumentException ie) {
  121. ; // Should not happen
  122. } catch (Exception e) {
  123. ; // Should not happen
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * 把orig和dest相同属性的value复制到dest中
  130. *
  131. * @param dest 结果
  132. * @param orig 来源
  133. * @throws Exception 异常
  134. */
  135. public static void copyBean2Bean(Object dest, Object orig)
  136. throws Exception {
  137. convert(dest, orig);
  138. }
  139. /**
  140. * 主要功能:bean 转换成 map
  141. * 注意事项:无
  142. *
  143. * @param map map对象
  144. * @param bean bean对象
  145. */
  146. @SuppressWarnings("all")
  147. public static void copyBean2Map(Map map, Object bean) {
  148. PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
  149. for (int i = 0; i < pds.length; i++) {
  150. PropertyDescriptor pd = pds[i];
  151. String propname = pd.getName();
  152. try {
  153. Object propvalue = PropertyUtils.getSimpleProperty(bean,
  154. propname);
  155. map.put(propname, propvalue);
  156. } catch (IllegalAccessException e) {
  157. // e.printStackTrace();
  158. } catch (InvocationTargetException e) {
  159. // e.printStackTrace();
  160. } catch (NoSuchMethodException e) {
  161. // e.printStackTrace();
  162. }
  163. }
  164. }
  165. /**
  166. * 主要功能:bean 转换成 map
  167. * 注意事项:无
  168. *
  169. * @param map map对象
  170. * @param bean bean对象
  171. */
  172. @SuppressWarnings("all")
  173. public static void copyBean2Map(Map map, Object bean,
  174. List<String> fieldNameList) {
  175. for (int i = 0; i < fieldNameList.size(); i++) {
  176. String oldPropname = fieldNameList.get(i);
  177. String propname = StringUtils.lineToHump(oldPropname);
  178. try {
  179. Object propvalue = getProp(bean, propname);
  180. map.put(oldPropname, propvalue);
  181. } catch (Exception e) {
  182. }
  183. }
  184. }
  185. /**
  186. * 将Map内的key与Bean中属性相同的内容复制到BEAN中
  187. *
  188. * @param bean Object
  189. * @param properties Map
  190. */
  191. @SuppressWarnings("all")
  192. public static void copyMap2Bean(Object bean, Map properties) {
  193. if ((bean == null) || (properties == null)) {
  194. return;
  195. }
  196. // map key 集合
  197. Iterator names = properties.keySet().iterator();
  198. // 逐个处理key转换
  199. while (names.hasNext()) {
  200. String name = (String) names.next();
  201. if (name == null) {
  202. continue;
  203. }
  204. Object value = properties.get(name);
  205. try {
  206. // name转换成驼峰
  207. name = StringUtils.lineToHump(name);
  208. // 获取属性类型
  209. Class clazz = PropertyUtils.getPropertyType(bean, name);
  210. if (null == clazz) {
  211. continue;
  212. }
  213. String className = clazz.getName();
  214. // 时间戳
  215. if (className.equalsIgnoreCase("java.sql.Timestamp")) {
  216. if (value == null || value.equals("")) {
  217. continue;
  218. }
  219. }
  220. // 整型INT
  221. if (className.equalsIgnoreCase("int")) {
  222. if (value == null || value.equals("")) {
  223. continue;
  224. } else {
  225. value = ((BigDecimal) value).intValue();
  226. }
  227. }
  228. // long型
  229. if (className.equalsIgnoreCase("long")) {
  230. if (value == null || value.equals("")) {
  231. continue;
  232. } else {
  233. value = ((BigDecimal) value).longValue();
  234. }
  235. }
  236. // long型
  237. if (className.equalsIgnoreCase("java.lang.Long")) {
  238. if (value == null || value.equals("")) {
  239. continue;
  240. } else {
  241. value = ((BigDecimal) value).longValue();
  242. }
  243. }
  244. // long型
  245. if (className.equalsIgnoreCase("java.lang.Integer")) {
  246. if (value == null || value.equals("")) {
  247. continue;
  248. } else {
  249. value = Integer.parseInt(value + "");
  250. }
  251. }
  252. // 属性赋值
  253. getInstance().setSimpleProperty(bean, name, value);
  254. } catch (Exception e) {
  255. continue;
  256. }
  257. }
  258. }
  259. /**
  260. * 自动转Map key值大写
  261. * 将Map内的key与Bean中属性相同的内容复制到BEAN中
  262. *
  263. * @param bean Object
  264. * @param properties Map
  265. * @throws IllegalAccessException 异常
  266. * @throws InvocationTargetException 异常
  267. */
  268. @SuppressWarnings("all")
  269. public static void copyMap2BeanNobig(Object bean, Map properties)
  270. throws IllegalAccessException,
  271. InvocationTargetException {
  272. // Do nothing unless both arguments have been specified
  273. if ((bean == null) || (properties == null)) {
  274. return;
  275. }
  276. // Loop through the property name/value pairs to be set
  277. Iterator names = properties.keySet().iterator();
  278. while (names.hasNext()) {
  279. String name = (String) names.next();
  280. // Identify the property name and value(s) to be assigned
  281. if (name == null) {
  282. continue;
  283. }
  284. Object value = properties.get(name);
  285. // 命名应该大小写应该敏感(否则取不到对象的属性)
  286. // name = name.toLowerCase();
  287. try {
  288. if (value == null) { // 不光Date类型,好多类型在null时会出错
  289. continue; // 如果为null不用设 (对象如果有特殊初始值也可以保留?)
  290. }
  291. Class clazz = PropertyUtils.getPropertyType(bean, name);
  292. if (null == clazz) { // 在bean中这个属性不存在
  293. continue;
  294. }
  295. String className = clazz.getName();
  296. // 临时对策(如果不处理默认的类型转换时会出错)
  297. if (className.equalsIgnoreCase("java.util.Date")) {
  298. value = new Date(
  299. ((java.sql.Timestamp) value).getTime());// wait to do:貌似有时区问题, 待进一步确认
  300. }
  301. // if (className.equalsIgnoreCase("java.sql.Timestamp")) {
  302. // if (value == null || value.equals("")) {
  303. // continue;
  304. // }
  305. // }
  306. getInstance().setSimpleProperty(bean, name, value);
  307. } catch (NoSuchMethodException e) {
  308. continue;
  309. }
  310. }
  311. }
  312. /**
  313. * Map内的key与Bean中属性相同的内容复制到BEAN中
  314. * 对于存在空值的取默认值
  315. *
  316. * @param bean Object
  317. * @param properties Map
  318. * @param defaultValue String
  319. * @throws IllegalAccessException 异常
  320. * @throws InvocationTargetException 异常
  321. */
  322. @SuppressWarnings("all")
  323. public static void copyMap2Bean(Object bean, Map properties,
  324. String defaultValue)
  325. throws IllegalAccessException,
  326. InvocationTargetException {
  327. // Do nothing unless both arguments have been specified
  328. if ((bean == null) || (properties == null)) {
  329. return;
  330. }
  331. // Loop through the property name/value pairs to be set
  332. Iterator names = properties.keySet().iterator();
  333. while (names.hasNext()) {
  334. String name = (String) names.next();
  335. // Identify the property name and value(s) to be assigned
  336. if (name == null) {
  337. continue;
  338. }
  339. Object value = properties.get(name);
  340. try {
  341. Class clazz = PropertyUtils.getPropertyType(bean, name);
  342. if (null == clazz) {
  343. continue;
  344. }
  345. String className = clazz.getName();
  346. if (className.equalsIgnoreCase("java.sql.Timestamp")) {
  347. if (value == null || value.equals("")) {
  348. continue;
  349. }
  350. }
  351. if (className.equalsIgnoreCase("java.lang.String")) {
  352. if (value == null) {
  353. value = defaultValue;
  354. }
  355. }
  356. getInstance().setSimpleProperty(bean, name, value);
  357. } catch (NoSuchMethodException e) {
  358. continue;
  359. }
  360. }
  361. }
  362. /**
  363. * 获取一个类和其父类的所有属性
  364. *
  365. * @param clazz 要查找的类
  366. * @return 属性列表
  367. */
  368. @SuppressWarnings({"rawtypes", "unchecked"})
  369. public static List<Field> findAllFieldsOfSelfAndSuperClass(Class clazz) {
  370. Field[] fields = null;
  371. List fieldList = new ArrayList();
  372. while (true) {
  373. if (clazz == null) {
  374. break;
  375. } else {
  376. fields = clazz.getDeclaredFields();
  377. for (int i = 0; i < fields.length; i++) {
  378. fieldList.add(fields[i]);
  379. }
  380. clazz = clazz.getSuperclass();
  381. }
  382. }
  383. return fieldList;
  384. }
  385. /**
  386. * 主要功能:把一个Bean对象转换成Map对象</br>
  387. * 注意事项:无
  388. *
  389. * @param obj 要转换的对象
  390. * @param ignores 要忽略的属性值
  391. * @return Map<String, Object>
  392. */
  393. public static Map<String, Object> convertBean2Map(Object obj,
  394. String[] ignores) {
  395. Map<String, Object> map = new HashMap<String, Object>();
  396. // Class clazz = obj.getClass();
  397. List<Field> fieldList = findAllFieldsOfSelfAndSuperClass(
  398. obj.getClass());
  399. Field field = null;
  400. try {
  401. for (int i = 0; i < fieldList.size(); i++) {
  402. field = fieldList.get(i);
  403. // 定义fieldName是否在拷贝忽略的范畴内
  404. boolean flag = false;
  405. if (ignores != null && ignores.length != 0) {
  406. flag = isExistOfIgnores(field.getName(), ignores);
  407. }
  408. if (!flag) {
  409. Object value = getProp(obj, field.getName());
  410. if (null != value
  411. && !StringUtils.EMPTY.equals(value.toString())) {
  412. map.put(field.getName(), getProp(obj, field.getName()));
  413. }
  414. }
  415. }
  416. } catch (SecurityException e) {
  417. e.printStackTrace();
  418. } catch (IllegalArgumentException e) {
  419. e.printStackTrace();
  420. }
  421. return map;
  422. }
  423. /**
  424. * 把一个Bean对象转换成Map对象</br>
  425. *
  426. * @param obj 要转换的对象
  427. * @return Map<String,Object>
  428. */
  429. public static Map<String, Object> convertBean2Map(Object obj) {
  430. return convertBean2Map(obj, null);
  431. }
  432. /**
  433. * 主要功能: 把一个Bean对象转换成Map对象,忽略UID</br>
  434. * 注意事项:无
  435. *
  436. * @param obj 要转换的对象
  437. * @return Map<String,Object>
  438. */
  439. public static Map<String, Object> convertBean2MapForIngoreserialVersionUID(Object obj) {
  440. return convertBean2Map(obj, new String[]{"serialVersionUID"});
  441. }
  442. /**
  443. * 判断fieldName是否是ignores中排除的
  444. *
  445. * @param fieldName 要判断的属性值
  446. * @param ignores 要忽略的列表
  447. * @return 匹配为true
  448. */
  449. private static boolean isExistOfIgnores(String fieldName,
  450. String[] ignores) {
  451. boolean flag = false;
  452. for (String str : ignores) {
  453. if (str.equals(fieldName)) {
  454. flag = true;
  455. break;
  456. }
  457. }
  458. return flag;
  459. }
  460. /**
  461. * 主要功能: 取得属性信息
  462. * 注意事项:无
  463. *
  464. * @param clazz 要取得的类
  465. * @param propertyName 要取得的属性
  466. * @return 属性信息
  467. */
  468. public static PropertyDescriptor getPropertyDescriptor(Class<Object> clazz,
  469. String propertyName) {
  470. StringBuffer sb = new StringBuffer();// 构建一个可变字符串用来构建方法名称
  471. Method setMethod = null;
  472. Method getMethod = null;
  473. PropertyDescriptor pd = null;
  474. try {
  475. Field f = clazz.getDeclaredField(propertyName);// 根据字段名来获取字段
  476. if (f != null) {
  477. // 构建方法的后缀
  478. String methodEnd = propertyName.substring(0, 1).toUpperCase()
  479. + propertyName.substring(1);
  480. sb.append("set" + methodEnd);// 构建set方法
  481. setMethod = clazz.getDeclaredMethod(sb.toString(),
  482. new Class[]{f.getType()});
  483. sb.delete(0, sb.length());// 清空整个可变字符串
  484. sb.append("get" + methodEnd);// 构建get方法
  485. // 构建get 方法
  486. getMethod = clazz.getDeclaredMethod(sb.toString(),
  487. new Class[]{});
  488. // 构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
  489. pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
  490. }
  491. } catch (Exception ex) {
  492. ex.printStackTrace();
  493. }
  494. return pd;
  495. }
  496. /**
  497. * 主要功能: 设置某个类的属性值
  498. * 注意事项:无
  499. *
  500. * @param obj 要修改的类实例
  501. * @param propertyName 要修改的属性
  502. * @param value 要设置的值
  503. */
  504. @SuppressWarnings("unchecked")
  505. public static void setProp(Object obj, String propertyName, Object value) {
  506. @SuppressWarnings("rawtypes")
  507. Class clazz = obj.getClass();// 获取对象的类型
  508. // 获取 clazz类型中的propertyName的属性描述器
  509. PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);
  510. Method setMethod = pd.getWriteMethod();// 从属性描述器中获取 set 方法
  511. try {
  512. setMethod.invoke(obj, new Object[]{value});// 调用 set 方法将传入的value值保存属性中去
  513. } catch (Exception e) {
  514. e.printStackTrace();
  515. }
  516. }
  517. /**
  518. * 主要功能: 取得对象的某属性的值
  519. * 注意事项:无
  520. *
  521. * @param obj 要取值对对象
  522. * @param propertyName 要取值的属性
  523. * @return Object 取得的值
  524. */
  525. @SuppressWarnings({"unchecked", "rawtypes"})
  526. public static Object getProp(Object obj, String propertyName) {
  527. Class clazz = obj.getClass();// 获取对象的类型
  528. // 获取 clazz 类型中的propertyName 的属性描述器
  529. PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);
  530. Method getMethod = pd.getReadMethod();// 从属性描述器中获取 get 方法
  531. Object value = null;
  532. try {
  533. // 调用方法获取方法的返回值
  534. value = getMethod.invoke(obj, new Object[]{});
  535. } catch (Exception e) {
  536. e.printStackTrace();
  537. }
  538. return value;// 返回值
  539. }
  540. /**
  541. * 构造方法
  542. */
  543. public MyBeanUtils() {
  544. super();
  545. }
  546. public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
  547. if (map == null) {
  548. return null;
  549. }
  550. Object obj = null;
  551. try {
  552. Map datas = map;
  553. obj = Class.forName(clazz.getName()).newInstance();
  554. Method[] ms = clazz.getDeclaredMethods();
  555. Field[] fs = clazz.getDeclaredFields();
  556. Set<String> keys = datas.keySet();
  557. for (int i = 0; i < fs.length; i++) {
  558. String fieldname = fs[i].getName();
  559. Object val = null;
  560. String type = fs[i].getGenericType().toString().split(
  561. "[.]")[fs[i].getGenericType().toString().split("[.]").length
  562. - 1];
  563. try {
  564. for (Method m : ms) {
  565. String temp = m.getName();
  566. if (temp.startsWith("set")) {
  567. String tmp = temp.substring(3, 4).toLowerCase()
  568. + temp.substring(4);
  569. if (igFirstChar(fieldname).equals(
  570. igFirstChar(tmp))) {
  571. String keyName = getAnnotationValue(
  572. fs[i].getAnnotations());
  573. if ("".equals(keyName)) {
  574. keyName = getAnnotationValue(
  575. clazz.getDeclaredMethod(
  576. "get" + temp.substring(3),
  577. null).getAnnotations());
  578. if ("".equals(keyName)) {
  579. keyName = turnToTable(tmp,
  580. m.getAnnotations());
  581. }
  582. }
  583. // map的key中找不到该字段
  584. if (!keys.contains(keyName.toUpperCase())) {
  585. // System.out.println(keyName);
  586. boolean can = true;
  587. int isMore = 0;
  588. for (String str : keys) {
  589. String temp_str = str.replaceAll("_",
  590. "");
  591. String temp_field = fieldname.replaceAll(
  592. "_", "");
  593. if (temp_str.toLowerCase().equals(
  594. temp_field.toLowerCase())) {
  595. if (isMore > 0) {
  596. throw new RuntimeException(
  597. "java字段" + temp_str + "在数据库中有多个字段相对应!");
  598. }
  599. val = datas.get(str);
  600. can = false;
  601. isMore++;
  602. }
  603. }
  604. if (can) {
  605. String msg = "";
  606. msg = "[警告]:" + clazz.getName() + "."
  607. + fieldname + "在数据库中找不到对应的字段!";
  608. // log.info(msg);
  609. }
  610. } else {
  611. val = datas.get(keyName.toUpperCase());
  612. }
  613. if (val == null) {
  614. continue;
  615. }
  616. Class<?> paramType = m.getParameterTypes()[0];// set方法的参数
  617. if (!String.class.equals(paramType)
  618. && "".equals(val)) {
  619. continue;
  620. }
  621. if ("String".equals(
  622. paramType.getSimpleName())) {
  623. m.invoke(obj,
  624. m.getParameterTypes()[0].getConstructor(
  625. String.class).newInstance(
  626. val.toString()));
  627. } else if ("Double".equals(
  628. paramType.getSimpleName())
  629. || "double".equals(
  630. paramType.getSimpleName())) {
  631. m.invoke(obj,
  632. Double.parseDouble(val.toString()));
  633. } else if ("Long".equals(
  634. paramType.getSimpleName())
  635. || "long".equals(
  636. paramType.getSimpleName())) {
  637. m.invoke(obj,
  638. Long.parseLong(val.toString()));
  639. } else if ("Integer".equals(
  640. paramType.getSimpleName())
  641. || "int".equals(
  642. paramType.getSimpleName())) {
  643. m.invoke(obj,
  644. Integer.parseInt(val.toString()));
  645. } else if ("BigInteger".equals(
  646. paramType.getSimpleName())) {
  647. m.invoke(obj,
  648. new BigInteger(val.toString()));
  649. } else if ("Date".equals(
  650. paramType.getSimpleName())) {
  651. m.invoke(obj, DateUtils.strToDate(
  652. val.toString()));
  653. } else if ("Short".equals(
  654. paramType.getSimpleName())) {
  655. m.invoke(obj,
  656. Short.parseShort(val.toString()));
  657. } else if ("Byte".equals(
  658. paramType.getSimpleName())
  659. || "byte".equals(
  660. paramType.getSimpleName())) {
  661. m.invoke(obj,
  662. Byte.parseByte(val.toString()));
  663. } else if ("char".equals(
  664. paramType.getSimpleName())) {
  665. if (val.toString().length() == 0) {
  666. continue;
  667. }
  668. m.invoke(obj,
  669. (char) (val.toString().charAt(0)));
  670. } else if ("Character".equals(
  671. paramType.getSimpleName())) {
  672. if (val.toString().length() == 0) {
  673. continue;
  674. }
  675. m.invoke(obj,
  676. (Character) (val.toString().charAt(0)));
  677. } else {
  678. if (val.getClass().equals(paramType)) {
  679. // System.out.println(m.getParameterTypes()[0].getName());
  680. m.invoke(obj, val);
  681. } else {
  682. // System.out.println(val.getClass().getName()
  683. // + ":" +
  684. // m.getParameterTypes()[0].getName());
  685. m.invoke(obj,
  686. m.getParameterTypes()[0].getConstructor(
  687. String.class).newInstance(
  688. val.toString()));
  689. }
  690. }
  691. }
  692. }
  693. }
  694. } catch (Exception e) {
  695. throw new RuntimeException(e.getMessage());
  696. }
  697. }
  698. } catch (InstantiationException e) {
  699. e.printStackTrace();
  700. } catch (IllegalAccessException e) {
  701. e.printStackTrace();
  702. } catch (ClassNotFoundException e) {
  703. e.printStackTrace();
  704. }
  705. return obj;
  706. }
  707. private static String igFirstChar(String fieldName) {
  708. if (fieldName == null) {
  709. return null;
  710. }
  711. if (fieldName.length() <= 1) {
  712. return fieldName;
  713. } else {
  714. return fieldName.substring(0, 1).toLowerCase()
  715. + fieldName.substring(1, fieldName.length());
  716. }
  717. }
  718. private static String getAnnotationValue(Annotation[] annos) {
  719. String result = "";
  720. try {
  721. for (Annotation anno : annos) {
  722. String simpleName = anno.annotationType().getSimpleName();
  723. // System.out.println(anno.annotationType().getSimpleName());
  724. // //Table or IroyUtils
  725. if ("JNTable".equals(simpleName)) {
  726. Method[] ms = anno.annotationType().getDeclaredMethods();
  727. for (Method method : ms) {
  728. String methodName = method.getName();
  729. if ("name".equals(methodName)) {
  730. Object val = method.invoke(anno, null);
  731. // System.out.println(method.getName() + ":" + val);
  732. // //name : value
  733. result = val + "";
  734. }
  735. }
  736. } else if ("Table".equals(simpleName)) {
  737. if ("".equals(result)) {
  738. Method[] ms = anno.annotationType().getDeclaredMethods();
  739. for (Method method : ms) {
  740. String methodName = method.getName();
  741. if ("name".equals(methodName)) {
  742. Object val = method.invoke(anno, null);
  743. // System.out.println(method.getName() + ":" +
  744. // val); //name : value
  745. result = val + "";
  746. }
  747. }
  748. }
  749. } else if ("JNField".equals(simpleName)) {
  750. Method[] ms = anno.annotationType().getDeclaredMethods();
  751. for (Method method : ms) {
  752. String methodName = method.getName();
  753. if ("name".equals(methodName)) {
  754. Object val = method.invoke(anno, null);
  755. // System.out.println(method.getName() + ":" + val);
  756. // //name : value
  757. result = val + "";
  758. }
  759. }
  760. } else if ("Column".equals(simpleName)) {
  761. if ("".equals(result)) {
  762. Method[] ms = anno.annotationType().getDeclaredMethods();
  763. for (Method method : ms) {
  764. String methodName = method.getName();
  765. if ("name".equals(methodName)) {
  766. Object val = method.invoke(anno, null);
  767. // System.out.println(method.getName() + ":" +
  768. // val); //name : value
  769. result = val + "";
  770. }
  771. }
  772. }
  773. }
  774. }
  775. } catch (Exception e) {
  776. e.printStackTrace();
  777. }
  778. return result;
  779. }
  780. private static String turnToTable(String fieldName, Annotation[] annos) {
  781. // System.out.println(flag);
  782. String str = getAnnotationValue(annos);
  783. // System.out.println(fieldName + ":" +str);
  784. if (!"".equals(str.trim())) {
  785. return str;
  786. }
  787. if (fieldName == null) {
  788. return "";
  789. }
  790. String temp = fieldName.toLowerCase();
  791. char[] tmp1 = fieldName.toCharArray();
  792. char[] tmp2 = temp.toCharArray();
  793. for (int i = 0; i < tmp2.length; i++) {
  794. if (i == 0) {
  795. str += tmp2[i] + "";
  796. if ((tmp1[0] != tmp2[0]) && (tmp1[1] != tmp2[1])) {
  797. return fieldName;
  798. }
  799. continue;
  800. }
  801. if (tmp1[i] != tmp2[i]) {
  802. if (i < tmp2.length - 1 && (tmp1[i + 1] != tmp2[i + 1])) {
  803. return fieldName;
  804. }
  805. str += "_" + tmp2[i];
  806. } else {
  807. str += tmp2[i] + "";
  808. }
  809. }
  810. return str;
  811. }
  812. /**
  813. * 名称:Struct <br>
  814. * 描述:〈功能详细描述〉<br>
  815. *
  816. * @author yaojie
  817. * @version 1.0
  818. * @since 1.0.0
  819. */
  820. private static class Struct {
  821. private Class<?> cls;
  822. private Object value;
  823. private String alias;
  824. public Struct(Class<?> cls, Object value, String alias) {
  825. this.cls = cls;
  826. this.value = value;
  827. this.alias = alias;
  828. }
  829. public Class<?> getType() {
  830. return cls;
  831. }
  832. public Object getValue() {
  833. return value;
  834. }
  835. public String getAlias() {
  836. return alias;
  837. }
  838. public String getString(String name) {
  839. return cls + " " + name + " : " + value;
  840. }
  841. }
  842. }