Wednesday, April 30, 2014

Using Apache commons beanutils


Using apache-commons-beanutils to copy object state to another object:

Dependencies: 
1- commons-beanutils-1.8.0 or higher
2- commons-logging-1.1 or higher

TestBeanUtil.java

package usingBeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import org.apache.commons.beanutils.BeanUtils;

public class TestBeanUtil {

public static void main(String[] args) throws IllegalAccessException,
InvocationTargetException {
UserModel m = new UserModel();
UserForm f = new UserForm();

m.setAddress("noida");
m.setAge(25);
m.setEmail("abhinav@gmail.com");
m.setfName("abhinav");
m.setlName("mishra");
m.setRole("admin");
m.setSalary(20000);

ArrayList<String> hobbies = new ArrayList<String>();
hobbies.add("cricket");
hobbies.add("photography");
m.setHobbies(hobbies);

ArrayList<String> depts = new ArrayList<String>();
depts.add("IT");
depts.add("admin");
m.setDepts(depts);

ArrayList<String> comps = new ArrayList<String>();
comps.add("innodata");
comps.add("fiserv");
m.setComps(comps);

System.out.println(m);

BeanUtils.copyProperties(f, m);

System.out.println(f);

}

}

UserForm.java

package usingBeanUtils;

import java.util.List;

public class UserForm {
private int age;
private int salary;
private String address;
private String email;
private String fName;
private String lName;
private List<String> depts;
private List<String> comps;
private String role;
private String webpage;

public String getWebpage() {
return webpage;
}
public void setWebpage(String webpage) {
this.webpage = webpage;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}

public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public List<String> getDepts() {
return depts;
}
public void setDepts(List<String> depts) {
this.depts = depts;
}
public List<String> getComps() {
return comps;
}
public void setComps(List<String> comps) {
this.comps = comps;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "UserForm [age=" + age + ", salary=" + salary + ", address="
+ address + ", email=" + email + ", fName=" + fName
+ ", lName=" + lName + ", depts=" + depts + ", comps=" + comps
+ ", role=" + role + ", webpage=" + webpage + "]";
}

}

UserModel.java

package usingBeanUtils;

import java.util.List;

public class UserModel {

private int age;
private int salary;
private String address;
private String email;
private String fName;
private String lName;
private List<String> depts;
private List<String> comps;
private String role;
private List<String> hobbies;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public List<String> getDepts() {
return depts;
}
public void setDepts(List<String> depts) {
this.depts = depts;
}
public List<String> getComps() {
return comps;
}
public void setComps(List<String> comps) {
this.comps = comps;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "UserModel [age=" + age + ", salary=" + salary + ", address="
+ address + ", email=" + email + ", fName=" + fName
+ ", lName=" + lName + ", depts=" + depts + ", comps=" + comps
+ ", role=" + role + ", hobbies=" + hobbies + "]";
}
}


Result:

UserModel [age=25, salary=20000, address=noida, email=abhinav@gmail.com, fName=abhinav, lName=mishra, depts=[IT, admin], comps=[innodata, fiserv], role=admin, hobbies=[cricket, photography]]

UserForm [age=25, salary=20000, address=noida, email=abhinav@gmail.com, fName=abhinav, lName=mishra, depts=[IT, admin], comps=[innodata, fiserv], role=admin, webpage=null]