Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1.  
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4.  
  5. import java.lang.reflect.*;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. /**
  11. * Created by bogdan on 27.11.2015.
  12. */
  13. public class Json {
  14. private static final Log LOG = LogFactory.getLog(Json.class.getSimpleName());
  15. private Map<String, String> json = new HashMap<>();
  16.  
  17. public <T> Json(T value, Class<T> clazz) {
  18. LOG.info("Starting to create json object");
  19. try {
  20. Object object = clazz.newInstance();
  21. for (Field field : object.getClass().getFields()) {
  22. for (Method method : object.getClass().getMethods()) {
  23. String fieldName = field.getName();
  24. if (method.getName().equalsIgnoreCase(String.format("%s%s", "get", fieldName))) {
  25. LOG.info("Trying to append field " + fieldName);
  26. String fieldValue = String.valueOf(method.invoke(value));
  27. json.put(fieldName, fieldValue);
  28. LOG.info("Managed to append field " + fieldName + " value " + fieldValue);
  29. }
  30. }
  31. }
  32. } catch (InstantiationException e) {
  33. LOG.error("Can't instantiate class");
  34. throw new ServiceException(e.getMessage());
  35. } catch (IllegalAccessException e) {
  36. LOG.error("Can't access class");
  37. throw new ServiceException(e.getMessage());
  38.  
  39. } catch (InvocationTargetException e) {
  40. LOG.error("Can't access class method");
  41. throw new ServiceException(e.getMessage());
  42. }
  43.  
  44. }
  45.  
  46. public <T> T getObject(String key) {
  47. return ((T) json.get(key));
  48. }
  49.  
  50. public <T> T getObject(Class<T> clazz) {
  51. Object object;
  52. try {
  53. object = clazz.newInstance();
  54. for (String fieldName : json.keySet()) {
  55. LOG.info("Init field object");
  56. // Object fieldObject = object.getClass().getField(fieldName).get(object);
  57. Field fieldObject = object.getClass().getField(fieldName);
  58. for (Method method : object.getClass().getMethods()) {
  59. if (method.getName().equalsIgnoreCase(String.format("%s%s", "set", fieldName))) {
  60. LOG.info("Managed to find a set method " + method.getName());
  61. method.invoke(object, getFieldValue(fieldObject, json.get(fieldName)));
  62. }
  63. }
  64. }
  65. } catch (InstantiationException e) {
  66. LOG.error("Can't instantiate class");
  67. throw new ServiceException(e.getMessage());
  68. } catch (IllegalAccessException e) {
  69. LOG.error("Can't access class");
  70. throw new ServiceException(e.getMessage());
  71. } catch (NoSuchFieldException e) {
  72. LOG.error("Field doesn't exist");
  73. throw new ServiceException(e.getMessage());
  74. } catch (InvocationTargetException e) {
  75. LOG.error("Method can't be access");
  76. throw new ServiceException(e.getMessage());
  77. }
  78. return clazz.cast(object);
  79. }
  80.  
  81. private Object getFieldValue(Field fieldObject, String value) {
  82. LOG.info("Field class is " + fieldObject.getType().getSimpleName() + " value is " + value);
  83. if (fieldObject.getType().getSimpleName().equalsIgnoreCase("string")) {
  84. return value;
  85. }
  86. if (fieldObject.getType().getSimpleName().equalsIgnoreCase("integer")) {
  87. return new Integer(value);
  88. }
  89. if (fieldObject.getType().getSimpleName().equalsIgnoreCase("double")) {
  90. return new Double(value);
  91. }
  92. return null;
  93. }
  94.  
  95. @Override
  96. public String toString() {
  97. StringBuilder stringBuilder = new StringBuilder();
  98. for (String key : json.keySet()) {
  99. stringBuilder.append(key);
  100. stringBuilder.append(":");
  101. stringBuilder.append(json.get(key));
  102. stringBuilder.append(",");
  103. }
  104. return stringBuilder.toString();
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement