经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)
来源:cnblogs  作者:^^ITBOY^^  时间:2019/1/25 9:09:58  对本文有异议

本篇主要介绍mybatis反射工具中用到的三个属性工具类:PropertyTokenizer、PropertyNamer、PropertyCopier。

PropertyTokenizer

主要用来解析Mybatis使用过程中遇到的类似"order[0].items[0].name"这种由"."和"[ ]"组成的表达式:

  1. public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
  2. /** ________________________
  3. * |order[0]|.items[0].name|
  4. * —————————————————————————
  5. *
  6. * 第一级表达式:name=order;indexName=order[0];index=0;children=item[0].name
  7. */
  8. /**
  9. * 当前表达式的名称
  10. */
  11. private String name;
  12. /**
  13. * 当前表达式的索引名
  14. */
  15. private String indexedName;
  16. /**
  17. * 当前表达式的索引
  18. */
  19. private String index;
  20. /**
  21. * 子表达式
  22. */
  23. private String children;
  24. /**
  25. * 初始化上述属性
  26. */
  27. public PropertyTokenizer(String fullname) {
  28. int delim = fullname.indexOf('.');
  29. if (delim > -1) {
  30. name = fullname.substring(0, delim);
  31. children = fullname.substring(delim + 1);
  32. } else {
  33. name = fullname;
  34. children = null;
  35. }
  36. indexedName = name;
  37. delim = name.indexOf('[');
  38. if (delim > -1) {
  39. index = name.substring(delim + 1, name.length() - 1);
  40. name = name.substring(0, delim);
  41. }
  42. }
  43. public String getName() {
  44. return name;
  45. }
  46. public String getIndex() {
  47. return index;
  48. }
  49. public String getIndexedName() {
  50. return indexedName;
  51. }
  52. public String getChildren() {
  53. return children;
  54. }
  55. @Override
  56. public boolean hasNext() {
  57. return children != null;
  58. }
  59. @Override
  60. public PropertyTokenizer next() {
  61. return new PropertyTokenizer(children);
  62. }
  63. @Override
  64. public void remove() {
  65. throw new UnsupportedOperationException("Remove is not supported, as it has no meaning in the context of properties.");
  66. }
  67. }

PropertyNamer

主要是用于实体类中getter、setter方法与相应属性的名称转换和相应的一些检测判断操作:

  1. public final class PropertyNamer {
  2. private PropertyNamer() {
  3. // Prevent Instantiation of Static Class
  4. }
  5. /**
  6. * get/set方法名转换为相应的属性名称
  7. */
  8. public static String methodToProperty(String name) {
  9. if (name.startsWith("is")) {
  10. name = name.substring(2);
  11. } else if (name.startsWith("get") || name.startsWith("set")) {
  12. name = name.substring(3);
  13. } else {
  14. throw new ReflectionException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'.");
  15. }
  16. if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
  17. name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
  18. }
  19. return name;
  20. }
  21. /**
  22. * 检测方法名是否get、set方法
  23. */
  24. public static boolean isProperty(String name) {
  25. return name.startsWith("get") || name.startsWith("set") || name.startsWith("is");
  26. }
  27. /**
  28. * 检测方法名是否get方法
  29. */
  30. public static boolean isGetter(String name) {
  31. return name.startsWith("get") || name.startsWith("is");
  32. }
  33. /**
  34. * 检测方法名是否get方法
  35. */
  36. public static boolean isSetter(String name) {
  37. return name.startsWith("set");
  38. }
  39. }

PropertyCopier

主要用于相同类型的两个对象之间进行属性值的拷贝:

  1. public final class PropertyCopier {
  2. private PropertyCopier() {
  3. // Prevent Instantiation of Static Class
  4. }
  5. public static void copyBeanProperties(Class<?> type, Object sourceBean, Object destinationBean) {
  6. Class<?> parent = type;
  7. while (parent != null) {
  8. /**
  9. * 获取到parent中所有被声明的字段,包括public、protected、default、private修饰的,除了
  10. * 继承父类的字段
  11. */
  12. final Field[] fields = parent.getDeclaredFields();
  13. for (Field field : fields) {
  14. try {
  15. field.setAccessible(true);
  16. /**
  17. * 将sourceBean的属性值设置到destinationBean中
                * set方法也不会将所有的属性值复制,有限制,具体限制可查看set源码
  18. */
  19. field.set(destinationBean, field.get(sourceBean));
  20. } catch (Exception e) {
  21. // Nothing useful to do, will only fail on final fields, which will be ignored.
  22. }
  23. }
  24. parent = parent.getSuperclass();
  25. }
  26. }
  27. }

 

原文链接:http://www.cnblogs.com/wly1-6/p/10313223.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号