Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. public class CloneHelper {
  2.  
  3. public static <T> T clone(T object) {
  4. return new Clone<T>(object).getClone();
  5. }
  6.  
  7. private static class Clone<T> {
  8. T object;
  9.  
  10. private Clone(T object) {
  11. this.object = object;
  12. }
  13.  
  14. private T getClone() {
  15. String s = serializeObject(object);
  16. return (T) unSerializeObject(s, object);
  17. }
  18.  
  19. private String serializeObject(T o) {
  20. Gson gson = new Gson();
  21. String serializedObject = gson.toJson(o);
  22. return serializedObject;
  23. }
  24.  
  25. private Object unSerializeObject(String s, Object o){
  26. Gson gson = new Gson();
  27. return gson.fromJson(s, o.getClass());
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement