EntityField.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.nuliji.tools.mybatis;
  2. import java.beans.PropertyDescriptor;
  3. import java.lang.annotation.Annotation;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. /**
  8. * Created by gaojie on 2017/11/9.
  9. */
  10. public class EntityField {
  11. private String name;
  12. private Field field;
  13. private Class<?> javaType;
  14. private Method setter;
  15. private Method getter;
  16. /**
  17. * 构造方法
  18. *
  19. * @param field 字段
  20. * @param propertyDescriptor 字段name对应的property
  21. */
  22. public EntityField(Field field, PropertyDescriptor propertyDescriptor) {
  23. if (field != null) {
  24. this.field = field;
  25. this.name = field.getName();
  26. this.javaType = field.getType();
  27. }
  28. if (propertyDescriptor != null) {
  29. this.name = propertyDescriptor.getName();
  30. this.setter = propertyDescriptor.getWriteMethod();
  31. this.getter = propertyDescriptor.getReadMethod();
  32. this.javaType = propertyDescriptor.getPropertyType();
  33. }
  34. }
  35. public void set(Object o, Object v){
  36. if(this.setter != null){
  37. try {
  38. this.setter.invoke(o, v);
  39. } catch (IllegalAccessException e) {
  40. e.printStackTrace();
  41. } catch (InvocationTargetException e) {
  42. e.printStackTrace();
  43. }
  44. }else if(this.field != null){
  45. try {
  46. this.field.set(o, v);
  47. } catch (IllegalAccessException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. public Object get(Object o){
  53. if(this.getter != null){
  54. try {
  55. return this.getter.invoke(o);
  56. } catch (IllegalAccessException e) {
  57. e.printStackTrace();
  58. } catch (InvocationTargetException e) {
  59. e.printStackTrace();
  60. }
  61. }else if(this.field != null){
  62. try {
  63. return this.field.get(o);
  64. } catch (IllegalAccessException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. return null;
  69. }
  70. /**
  71. * 先创建field,然后可以通过该方法获取property等属性
  72. *
  73. * @param other
  74. */
  75. public void copyFromPropertyDescriptor(EntityField other) {
  76. this.setter = other.setter;
  77. this.getter = other.getter;
  78. this.javaType = other.javaType;
  79. this.name = other.name;
  80. }
  81. /**
  82. * 是否有该注解
  83. *
  84. * @param annotationClass
  85. * @return
  86. */
  87. public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
  88. boolean result = false;
  89. if (field != null) {
  90. result = field.isAnnotationPresent(annotationClass);
  91. }
  92. if (!result && setter != null) {
  93. result = setter.isAnnotationPresent(annotationClass);
  94. }
  95. if (!result && getter != null) {
  96. result = getter.isAnnotationPresent(annotationClass);
  97. }
  98. return result;
  99. }
  100. /**
  101. * 获取指定的注解
  102. *
  103. * @param annotationClass
  104. * @param <T>
  105. * @return
  106. */
  107. public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
  108. T result = null;
  109. if (field != null) {
  110. result = field.getAnnotation(annotationClass);
  111. }
  112. if (result == null && setter != null) {
  113. result = setter.getAnnotation(annotationClass);
  114. }
  115. if (result == null && getter != null) {
  116. result = getter.getAnnotation(annotationClass);
  117. }
  118. return result;
  119. }
  120. /**
  121. * 字段属性名
  122. *
  123. * @return
  124. */
  125. public String getName() {
  126. return name;
  127. }
  128. /**
  129. * 获取javaType
  130. *
  131. * @return
  132. */
  133. public Class<?> getJavaType() {
  134. return javaType;
  135. }
  136. /**
  137. * 设置javaType
  138. *
  139. * @param javaType
  140. */
  141. public void setJavaType(Class<?> javaType) {
  142. this.javaType = javaType;
  143. }
  144. @Override
  145. public boolean equals(Object o) {
  146. if (this == o) return true;
  147. if (o == null || getClass() != o.getClass()) return false;
  148. EntityField that = (EntityField) o;
  149. return !(name != null ? !name.equals(that.name) : that.name != null);
  150. }
  151. @Override
  152. public int hashCode() {
  153. return name != null ? name.hashCode() : 0;
  154. }
  155. }