Steps 1:
Source
Person.java(list address)
Address.java(User.java)
public class Person implements Serializable
{
private String firstName;
private String lastName;
private int age;
private List <Address> addressList;
}
public class Address implements Serializable
{
private int id;
private String address1;
private String address2;
private String city;
private String state;
private String country;
private String pincode;
private String contactNumber;
private String email;
private User user;
}
Target
PersonDTO.java (list addressDTO)
AddressDTO.java (User.java)
public class PersonDTO implements Serializable
{
private String firstName;
private int age;
private String lastName;
private List <AddressDTO> addressList;
}
public class AddressDTO implements Serializable
{
private int id;
private String address1;
private String address2;
private String city;
private String state;
private String country;
private String pincode;
private String contactNumber;
private String email;
private User user;
}
Step 2: Sample java class
package com.drvijay;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import com.product.base.utils.ObjectUtils;
import com.product.base.utils.ProductConstants;
/**
* @author drvijay
* @date Apr-20-2023 10:30 AM
*/
@SuppressWarnings ( "unused" )
public class UtilHelper
{
private static final long serialVersionUID = 1L;
public static void copy ( Object source, Object target ) throws IllegalArgumentException, IllegalAccessException
{
Class <?> sourceClass = source.getClass ();
Class <?> targetClass = target.getClass ();
Field [] sourceFields = sourceClass.getDeclaredFields ();
Field [] targetFields = targetClass.getDeclaredFields ();
for ( Field sourceField : sourceFields )
{
for ( Field targetField : targetFields )
{
// If the source and target fields have the same name and type
if ( sourceField.getName ().equals ( targetField.getName () ) && sourceField.getType ().equals ( targetField.getType () ) )
{
// Make the source and target field accessible
sourceField.setAccessible ( true );
targetField.setAccessible ( true );
// Copy the value from the source field to the target field
targetField.set ( target, sourceField.get ( source ) );
// Stop looping through the target fields
break;
}
}
}
}
public static void main ( String [] args ) throws Exception
{
// Create a source object
Person sourcePerson = new Person ();
sourcePerson.setFirstName ( "vijay" );
sourcePerson.setLastName ( "kumar" );
sourcePerson.setAge ( 30 );
User u = new User ();
u.setUA ( "ua" );
u.setUB ( "ub" );
List<Address> aList = new ArrayList();
Address address = new Address ();
address.setAddress1 ( "add 1" );
address.setAddress2 ( "add 2" );
address.setPincode ( "54534" );
address.setUser ( u );
aList.add ( address );
address = new Address ();
address.setAddress1 ( "add 11" );
address.setAddress2 ( "add 22" );
address.setPincode ( "545341" );
aList.add ( address );
sourcePerson.setAddressList ( aList );
// Create a target object with similar properties
PersonDTO targetPersonDTO = new PersonDTO ();
// Copy the properties from the source object to the target object
UtilHelper.copy ( sourcePerson, targetPersonDTO );
// Print the values of the target object
System.out.println ( targetPersonDTO.getFirstName () );
System.out.println ( targetPersonDTO.getAge () );
System.out.println ( sourcePerson.toString () );
System.out.println ( targetPersonDTO.toString () );
System.out.println ( targetPersonDTO.getAddressList ().toString () );
}
}
Output
vijay
30
Person(firstName=vijay, lastName=kumar, age=30, addressList=[Address(id=0, address1=add 1, address2=add 2, city=null, state=null, country=null, pincode=54534, contactNumber=null, email=null, user=User(uA=ua, uB=ub)), Address(id=0, address1=add 11, address2=add 22, city=null, state=null, country=null, pincode=545341, contactNumber=null, email=null, user=null)])
PersonDTO(firstName=vijay, age=30, lastName=kumar, addressList=[Address(id=0, address1=add 1, address2=add 2, city=null, state=null, country=null, pincode=54534, contactNumber=null, email=null, user=User(uA=ua, uB=ub)), Address(id=0, address1=add 11, address2=add 22, city=null, state=null, country=null, pincode=545341, contactNumber=null, email=null, user=null)])
[Address(id=0, address1=add 1, address2=add 2, city=null, state=null, country=null, pincode=54534, contactNumber=null, email=null, user=User(uA=ua, uB=ub)), Address(id=0, address1=add 11, address2=add 22, city=null, state=null, country=null, pincode=545341, contactNumber=null, email=null, user=null)]