PageRenderTime 187ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/xjt/ws/invoker/ObjectUtil.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 152 lines | 146 code | 6 blank | 0 comment | 79 complexity | 69784e806ac39444e12bf72dbf1ee422 MD5 | raw file
  1. package xjt.ws.invoker;
  2. import java.lang.reflect.ParameterizedType;
  3. import java.lang.reflect.Type;
  4. import java.math.BigDecimal;
  5. import java.math.BigInteger;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import xjt.ws.core.RequestFile;
  12. import xjt.ws.core.ServiceRequest;
  13. import com.alibaba.fastjson.JSON;
  14. import com.alibaba.fastjson.JSONArray;
  15. import com.alibaba.fastjson.JSONObject;
  16. public class ObjectUtil {
  17. @SuppressWarnings("unchecked")
  18. public static final Object initValue(Class<?> clazz, Type type,
  19. String paramName, JSONObject jsonParamsValues,
  20. ServiceRequest request) {
  21. if (clazz == ServiceRequest.class) {
  22. return request;
  23. }
  24. if (clazz == RequestFile.class) {
  25. return request.getFile(paramName);
  26. }
  27. if (jsonParamsValues == null) {
  28. return null;
  29. }
  30. if (!jsonParamsValues.containsKey(paramName)) {
  31. return null;
  32. }
  33. if(clazz.isArray()){
  34. return initArrayValue(clazz,type,paramName,jsonParamsValues,request);
  35. }
  36. if (clazz == int.class || clazz == Integer.class) {
  37. return jsonParamsValues.getInteger(paramName);
  38. }
  39. if (clazz == boolean.class || clazz == Boolean.class) {
  40. return jsonParamsValues.getBoolean(paramName);
  41. }
  42. if (clazz == byte.class || clazz == Byte.class) {
  43. return jsonParamsValues.getByte(paramName);
  44. }
  45. if (clazz == char.class || clazz == Character.class) {
  46. String value = jsonParamsValues.getString(paramName);
  47. if (StringUtils.isEmpty(value)) {
  48. return null;
  49. } else {
  50. return new Character(value.charAt(0));
  51. }
  52. }
  53. if (clazz == short.class || clazz == Short.class) {
  54. return jsonParamsValues.getShort(paramName);
  55. }
  56. if (clazz == long.class || clazz == Long.class) {
  57. return jsonParamsValues.getLong(paramName);
  58. }
  59. if (clazz == float.class || clazz == Float.class) {
  60. return jsonParamsValues.getFloat(paramName);
  61. }
  62. if (clazz == double.class || clazz == Double.class) {
  63. return jsonParamsValues.getDouble(paramName);
  64. }
  65. if (clazz == String.class) {
  66. return jsonParamsValues.getString(paramName);
  67. }
  68. if (List.class.isAssignableFrom(clazz)) {
  69. Class<?> genericClass = null;
  70. if (type instanceof ParameterizedType) {
  71. ParameterizedType pt = (ParameterizedType) type;
  72. genericClass = (Class<?>) pt.getActualTypeArguments()[0];
  73. }
  74. List<JSONObject> jsonObjectList = JSON.toJavaObject(
  75. jsonParamsValues.getJSONArray(paramName), List.class);
  76. if (genericClass != null) {
  77. if(genericClass==RequestFile.class){
  78. return request.getFiles();
  79. }
  80. List<Object> resultList = new ArrayList<Object>();
  81. for (JSONObject jsonObject : jsonObjectList) {
  82. resultList.add(JSON.toJavaObject(jsonObject, genericClass));
  83. }
  84. return resultList;
  85. } else {
  86. return jsonObjectList;
  87. }
  88. }
  89. if (clazz == Date.class) {
  90. return jsonParamsValues.getDate(paramName);
  91. }
  92. if (clazz == BigDecimal.class) {
  93. return jsonParamsValues.getBigDecimal(paramName);
  94. }
  95. if (clazz == BigInteger.class) {
  96. return jsonParamsValues.getBigInteger(paramName);
  97. }
  98. JSONObject jsonObject = jsonParamsValues.getJSONObject(paramName);
  99. return JSON.toJavaObject(jsonObject, clazz);
  100. }
  101. private static final Object initArrayValue(Class<?> clazz, Type type,
  102. String paramName, JSONObject jsonParamsValues,ServiceRequest request){
  103. JSONArray jsonArray = jsonParamsValues.containsKey(paramName) ? jsonParamsValues.getJSONArray(paramName) : null;
  104. String paramTypeName = clazz.getComponentType().getName();
  105. if (clazz.getComponentType().isPrimitive()) {
  106. if (paramTypeName.equals("int")) {
  107. return toArray(jsonArray, int.class);
  108. } else if (paramTypeName.equals("boolean")) {
  109. return toArray(jsonArray, boolean.class);
  110. } else if (paramTypeName.equals("byte")) {
  111. return toArray(jsonArray, byte.class);
  112. } else if (paramTypeName.equals("char")) {
  113. return toArray(jsonArray, char.class);
  114. } else if (paramTypeName.equals("short")) {
  115. return toArray(jsonArray, short.class);
  116. } else if (paramTypeName.equals("long")) {
  117. return toArray(jsonArray, long.class);
  118. } else if (paramTypeName.equals("float")) {
  119. return toArray(jsonArray, float.class);
  120. } else if (paramTypeName.equals("double")) {
  121. return toArray(jsonArray, double.class);
  122. }
  123. } else {
  124. Class<?> arrayClassType = null;
  125. try {
  126. arrayClassType = Class.forName(paramTypeName);
  127. } catch (ClassNotFoundException e) {
  128. return null;
  129. }
  130. if (arrayClassType.equals(MultipartFile.class)) {
  131. return request.getFiles();
  132. } else {
  133. return toArray(jsonArray, arrayClassType);
  134. }
  135. }
  136. return null;
  137. }
  138. private static final Object toArray(JSONArray jsonArray, Class<?> objectClass) {
  139. if (jsonArray == null) {
  140. return null;
  141. } else {
  142. Object object = JSON.toJavaObject(jsonArray, objectClass);
  143. return object;
  144. }
  145. }
  146. }