Advertisement
Guest User

// Copy-paste all declared fields to new instance

a guest
Oct 15th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. package gof.prototype;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.Serializable;
  9. import java.lang.reflect.Constructor;
  10. import java.lang.reflect.Field;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. /**
  16.  *
  17.  * @author Wojciech
  18.  */
  19. public interface IPrototype extends Serializable{
  20.  
  21.     default public <T> T deepClone() {
  22.         T clone = null;
  23.         try {
  24.             ByteArrayOutputStream baOUT = new ByteArrayOutputStream();
  25.             ObjectOutputStream oOUT = new ObjectOutputStream(baOUT);
  26.             oOUT.writeObject(this);
  27.  
  28.             ByteArrayInputStream baIN = new ByteArrayInputStream(baOUT.toByteArray());
  29.             ObjectInputStream oIN = new ObjectInputStream(baIN);
  30.             clone = (T) oIN.readObject();
  31.         } catch (IOException | ClassNotFoundException e) {//Java 7 required
  32.             e.printStackTrace();
  33.         }
  34.         return clone;
  35.     }
  36.     default public <T> T shallowClone() {
  37.         T clone = null;
  38.         try {
  39. // Get object's constructor
  40.             Constructor[] constructors = this.getClass().getDeclaredConstructors();
  41.  
  42. // Check if constructor exists
  43.             if (constructors.length == 0) {
  44.                 throw new InstantiationException("No constructor was found.");
  45.             }
  46.  
  47. // Prepare null/zero constructor param list
  48.             List<Object> paramList = new ArrayList();
  49.  
  50. // Get constructor param types
  51.             Class[] paramTypes = constructors[0].getParameterTypes();
  52.  
  53. // Fill in param list
  54.             for (Class paramType : paramTypes) {
  55.                 if (paramType.isPrimitive()) {
  56.                     paramList.add(0);
  57.                 } else {
  58.                     paramList.add(null);
  59.                 }
  60.             }
  61.  
  62. // Create 'empty' instance
  63.             clone = (T) constructors[0].newInstance(paramList.toArray());
  64.  
  65. // Copy-paste all declared fields to new instance
  66.             Field[] fields = this.getClass().getDeclaredFields();
  67.             for (Field field : fields) {
  68.                 field.set(clone, field.get(this));
  69.             }
  70.  
  71.         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
  72.         }
  73.  
  74.         return clone;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement