Deep-copies the values from one object to the other : Clone « Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Class » CloneScreenshots 
Deep-copies the values from one object to the other
        
//package com.ryanm.util;

import java.lang.reflect.Field;
import java.util.Random;

/**
 * I can't think of anywhere else to put them
 
 @author ryanm
 */
public class Util
{
  /**
   * Deep-copies the values from one object to the other
   
   @param <T>
   @param from
   *           the source of the copied data
   @param to
   *           The destination of the copied data
   */
  public static <T> void copyFieldsT from, T to )
  {
    forField f : from.getClass().getFields() )
    {
      try
      {
        ifisPrimitivishf.getType() ) )
        {
          f.setto, f.getfrom ) );
        }
        else
        {
          copyFieldsf.getfrom ), f.getto ) );
        }
      }
      catchIllegalArgumentException e )
      {
        e.printStackTrace();
      }
      catchIllegalAccessException e )
      {
        e.printStackTrace();
      }
    }
  }

  private static boolean isPrimitivishClass )
  {
    return c.isPrimitive() || c == String.class || c == Boolean.class
        || c == Byte.class || c == Short.class || c == Character.class
        || c == Integer.class || c == Float.class || c == Double.class
        || c == Long.class;
  }

}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.A Cloning Example
2.Class is declared to be cloneable.
3.Arrays are automatically cloneable
4.Clone objects
5.Creating a Deep Copy
6.Shallow Copy TestShallow Copy Test
7.Deep Copy TestDeep Copy Test
8.Uses serialization to perform deep copy cloning.
9.Tests cloning to see if destination of references are also clonedTests cloning to see if destination of references are also cloned
10.Creating local copies with cloneCreating local copies with clone
11.You can insert Cloneability at any level of inheritance
12.Cloning a composed objectCloning a composed object
13.Serializable and cloneSerializable and clone
14.Go through a few gyrations to add cloning to your own classGo through a few gyrations to add cloning to your own class
15.Checking to see if a reference can be clonedChecking to see if a reference can be cloned
16.The clone operation works for only a few items in the standard Java libraryThe clone operation works for only a few items in the standard Java library
17.Demonstration of cloning
18.Simple demo of avoiding side-effects by using Object.cloneSimple demo of avoiding side-effects by using Object.clone
19.Clone an object with clone method from parent
20.Manipulate properties after clone operation
21.Deep clone ObjectDeep clone Object
22.Serializable Clone
23.Utility for object cloning
24.Clone Via Serialization
25.Clone demo
26.Deep clone serializing/de-serializng Clone
27.Implements a pool of internalized objects
28.A collection of utilities to workaround limitations of Java clone framework
29.This program demonstrates cloning
30.Returns a copy of the object, or null if the object cannot be serialized.Returns a copy of the object, or null if the object cannot be serialized.
31.Object Deep copy
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.