PageRenderTime 71ms CodeModel.GetById 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/util/FieldInfo.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 227 lines | 186 code | 41 blank | 0 comment | 56 complexity | 566d0c21c95bed87e5b1ea079087b834 MD5 | raw file
  1. package com.alibaba.fastjson.util;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.GenericDeclaration;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import java.lang.reflect.ParameterizedType;
  8. import java.lang.reflect.Type;
  9. import java.lang.reflect.TypeVariable;
  10. public class FieldInfo implements Comparable<FieldInfo> {
  11. private final String name;
  12. private final Method method;
  13. private final Field field;
  14. private final Class<?> fieldClass;
  15. private final Type fieldType;
  16. private final Class<?> declaringClass;
  17. private boolean getOnly = false;
  18. public FieldInfo(String name, Class<?> declaringClass, Class<?> fieldClass, Type fieldType, Method method,
  19. Field field){
  20. this.name = name;
  21. this.declaringClass = declaringClass;
  22. this.fieldClass = fieldClass;
  23. this.fieldType = fieldType;
  24. this.method = method;
  25. this.field = field;
  26. if (method != null) {
  27. method.setAccessible(true);
  28. }
  29. if (field != null) {
  30. field.setAccessible(true);
  31. }
  32. }
  33. public FieldInfo(String name, Method method, Field field){
  34. this(name, method, field, null, null);
  35. }
  36. public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type type){
  37. this.name = name;
  38. this.method = method;
  39. this.field = field;
  40. if (method != null) {
  41. method.setAccessible(true);
  42. }
  43. if (field != null) {
  44. field.setAccessible(true);
  45. }
  46. Type fieldType;
  47. Class<?> fieldClass;
  48. if (method != null) {
  49. if (method.getParameterTypes().length == 1) {
  50. fieldClass = method.getParameterTypes()[0];
  51. fieldType = method.getGenericParameterTypes()[0];
  52. } else {
  53. fieldClass = method.getReturnType();
  54. fieldType = method.getGenericReturnType();
  55. getOnly = true;
  56. }
  57. this.declaringClass = method.getDeclaringClass();
  58. } else {
  59. fieldClass = field.getType();
  60. fieldType = field.getGenericType();
  61. this.declaringClass = field.getDeclaringClass();
  62. }
  63. if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) {
  64. TypeVariable<?> tv = (TypeVariable<?>) fieldType;
  65. Type genericFieldType = getInheritGenericType(clazz, tv);
  66. if (genericFieldType != null) {
  67. this.fieldClass = TypeUtils.getClass(genericFieldType);
  68. this.fieldType = genericFieldType;
  69. return;
  70. }
  71. }
  72. Type genericFieldType = getFieldType(clazz, type, fieldType);
  73. if (genericFieldType != fieldType) {
  74. if (genericFieldType instanceof ParameterizedType) {
  75. fieldClass = TypeUtils.getClass(genericFieldType);
  76. } else if (genericFieldType instanceof Class) {
  77. fieldClass = TypeUtils.getClass(genericFieldType);
  78. }
  79. }
  80. this.fieldType = genericFieldType;
  81. this.fieldClass = fieldClass;
  82. }
  83. public static Type getFieldType(Class<?> clazz, Type type, Type fieldType) {
  84. if (clazz == null || type == null) {
  85. return fieldType;
  86. }
  87. if (!(type instanceof ParameterizedType)) {
  88. return fieldType;
  89. }
  90. if (fieldType instanceof TypeVariable) {
  91. ParameterizedType paramType = (ParameterizedType) type;
  92. TypeVariable<?> typeVar = (TypeVariable<?>) fieldType;
  93. for (int i = 0; i < clazz.getTypeParameters().length; ++i) {
  94. if (clazz.getTypeParameters()[i].getName().equals(typeVar.getName())) {
  95. fieldType = paramType.getActualTypeArguments()[i];
  96. break;
  97. }
  98. }
  99. }
  100. return fieldType;
  101. }
  102. public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
  103. Type type = null;
  104. GenericDeclaration gd = tv.getGenericDeclaration();
  105. do {
  106. type = clazz.getGenericSuperclass();
  107. if (type == null) {
  108. return null;
  109. }
  110. if (type instanceof ParameterizedType) {
  111. ParameterizedType ptype = (ParameterizedType) type;
  112. if (ptype.getRawType() == gd) {
  113. TypeVariable<?>[] tvs = gd.getTypeParameters();
  114. Type[] types = ptype.getActualTypeArguments();
  115. for (int i = 0; i < tvs.length; i++) {
  116. if (tvs[i] == tv)
  117. return types[i];
  118. }
  119. return null;
  120. }
  121. }
  122. clazz = TypeUtils.getClass(type);
  123. } while (type != null);
  124. return null;
  125. }
  126. public String toString() {
  127. return this.name;
  128. }
  129. public Class<?> getDeclaringClass() {
  130. return declaringClass;
  131. }
  132. public Class<?> getFieldClass() {
  133. return fieldClass;
  134. }
  135. public Type getFieldType() {
  136. return fieldType;
  137. }
  138. public String getName() {
  139. return name;
  140. }
  141. public Method getMethod() {
  142. return method;
  143. }
  144. public Field getField() {
  145. return field;
  146. }
  147. public int compareTo(FieldInfo o) {
  148. return this.name.compareTo(o.name);
  149. }
  150. public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
  151. T annotation = null;
  152. if (method != null) {
  153. annotation = method.getAnnotation(annotationClass);
  154. }
  155. if (annotation == null) {
  156. if (field != null) {
  157. annotation = field.getAnnotation(annotationClass);
  158. }
  159. }
  160. return annotation;
  161. }
  162. public Object get(Object javaObject) throws IllegalAccessException, InvocationTargetException {
  163. if (method != null) {
  164. Object value = method.invoke(javaObject, new Object[0]);
  165. return value;
  166. }
  167. return field.get(javaObject);
  168. }
  169. public void set(Object javaObject, Object value) throws IllegalAccessException, InvocationTargetException {
  170. if (method != null) {
  171. method.invoke(javaObject, new Object[] { value });
  172. return;
  173. }
  174. field.set(javaObject, value);
  175. }
  176. public void setAccessible(boolean flag) throws SecurityException {
  177. if (method != null) {
  178. method.setAccessible(flag);
  179. return;
  180. }
  181. field.setAccessible(flag);
  182. }
  183. public boolean isGetOnly() {
  184. return getOnly;
  185. }
  186. }