How to use reflection to copy properties from Pojo to other Java Beans

Sometimes we need copy properties from a Java class to other, we can do it this manually or with our own reflection implementation, but in this case use us reflection for automate it with a utility from apache

Requirements

  1. commons-beanutils , you can download from here http://commons.apache.org/beanutils/
  2. commons-loging , you can download from here http://commons.apache.org/logging/
  3. Eclipse
  4. JDK 1.6

2. Hands on

  1. Create a “Java Project” in eclipse.
  2. Project name: CopyProperties, and click on “Finish” button.
  3. Unzip both common-beanutils-xxx.zip and commons-logging-xxx.zip.
  4. Add commons-beanutils-xxx.jar and commons-logging-xxx.jar to the classpath of your project.

Create a new class “Person” in the package pojo.from


package pojo.from;

public class Person {

        private String name;
        private int age;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }

}

Create a class “OthePerson” in pojo.to package with same fields


package pojo.to;

public class OthePerson {

        private String name;
        private int age;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }
}

3. Testing

Create a test class in pojo.test package with main method, and test commons-beanutils.


package pojo.test;

import org.apache.commons.beanutils.BeanUtils;

import pojo.from.Person;
import pojo.to.OthePerson;

/**
 * Class for test copy properties
 *
 * @author Rene Enriquez
 * @date 23/07/2012
 *
 */
public class Test {

        /**
         * Main method
         *
         * @param args
         */
        public static void main(String[] args) throws Exception {
		Person person = new Person();
		person.setAge(15);
		person.setName("rene");

		OtherPerson othePerson = new OtherPerson();

		System.out.println("*** Before BeanUtils.copyProperties ***");

		System.out.println("Person");
		System.out.println(person.getAge());
		System.out.println(person.getName());

		System.out.println("othePerson");
		System.out.println(othePerson.getAge());
		System.out.println(othePerson.getName());

		//copy properties from (target, source)
		BeanUtils.copyProperties(othePerson, person);

		System.out.println("\n*** After BeanUtils.copyProperties ***");

		System.out.println("Person");
		System.out.println(person.getAge());
		System.out.println(person.getName());

		System.out.println("othePerson");
		System.out.println(othePerson.getAge());
		System.out.println(othePerson.getName());

	}
}

Output


*** Before BeanUtils.copyProperties ***
Person
15
rene
othePerson
0
null

*** After BeanUtils.copyProperties ***
Person
15
rene
othePerson
15
rene

Download Source Code

Download it – CopyProperties.zip (6 KB)

References

  1. commons-beanutils official website
  2. commons-logging official website

11 comments on “How to use reflection to copy properties from Pojo to other Java Beans

  1. this only works with objects which are having public fields or private fields with both getters and setters..this does not apply to objects we creating using builder pattern as they don’t have public setters.

    Reply
  2. What is the difference between shallow copy and deep copy?

    Reply
    1. What you mean with shallow copy and deep copy?…references and values????

      Reply
    1. I was also thinking about Dozer when I saw this post. Great solution and I used it several times already.

      Reply
  3. Is this also known as “Prototype” pattern in GO4?
    I guess this is good for “Audit Trail” purpose for Enterprise applications.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *