Jul 142011
 

Recently a friend of mine asked me “What the hell is Java Reflection? What is it used for ?” After a couple of minutes explaining for him that Java Reflection was invented to “introspect” Java class by giving away meta-data that explain the Java class itself and blah blah…but seemed like he didn’t understand at all, I decide to write a blog, and here it goes. OK suppose that you have a Man class with following declaration:

package copy;

/**
 * Java Reflection Object Copier by http://java.searchdaily.net
 * 
 * @author namnvhue
 * 
 */
public class Man {
	private String copyName; // can be copied
	private String copyEmail; // can be copied
	private String copyAddress; // can be copied
	private Integer nationalIdentityNumber; // cannot be copied
	private String bankAccount;

	public Man() {
		// default value
		this.copyName = "Robin Hood";
		this.copyAddress = "123 The Earth";
		this.copyEmail = "robin@hood.com";
		this.nationalIdentityNumber = 12345;
		this.bankAccount = "B12345";
	}

	public void gotoSchool() {

	}

	public void goFishing() {

	}

	public String getCopyName() {
		return copyName;
	}

	public void setCopyName(String copyName) {
		this.copyName = copyName;
	}

	public String getCopyEmail() {
		return copyEmail;
	}

	public void setCopyEmail(String copyEmail) {
		this.copyEmail = copyEmail;
	}

	public String getCopyAddress() {
		return copyAddress;
	}

	public void setCopyAddress(String copyAddress) {
		this.copyAddress = copyAddress;
	}

	public Integer getNationalIdentityNumber() {
		return nationalIdentityNumber;
	}

	public void setNationalIdentityNumber(Integer nationalIdentityNumber) {
		this.nationalIdentityNumber = nationalIdentityNumber;
	}

	public String getBankAccount() {
		return bankAccount;
	}

	public void setBankAccount(String bankAccount) {
		this.bankAccount = bankAccount;
	}

}
 


And then you have a SuperMan class as follow:

package copy;

/**
 * Java Reflection Object Copier by http://java.searchdaily.net
 * 
 * @author namnvhue
 * 
 */
public class SuperMan {
	private String copyName; // can be copied
	private String copyEmail; // can be copied
	private String copyAddress; // can be copied
	private Integer nationalIdentityNumber; // cannot be copied
	private String bankAccount;
	private String superWeapon;

	public SuperMan() {
		// default value
		this.copyName = "Peter Pan";
		this.copyAddress = "123 Never Land";
		this.copyEmail = "peter@pan.com";
		this.nationalIdentityNumber = 54321;
		this.bankAccount = "S54321";
		this.superWeapon = "LightSpeed Pistol";
	}

	public void gotoSchool() {

	}

	public void goFishing() {

	}

	public String getCopyName() {
		return copyName;
	}

	public void setCopyName(String copyName) {
		this.copyName = copyName;
	}

	public String getCopyEmail() {
		return copyEmail;
	}

	public void setCopyEmail(String copyEmail) {
		this.copyEmail = copyEmail;
	}

	public String getCopyAddress() {
		return copyAddress;
	}

	public void setCopyAddress(String copyAddress) {
		this.copyAddress = copyAddress;
	}

	public Integer getNationalIdentityNumber() {
		return nationalIdentityNumber;
	}

	public void setNationalIdentityNumber(Integer nationalIdentityNumber) {
		this.nationalIdentityNumber = nationalIdentityNumber;
	}

	public String getBankAccount() {
		return bankAccount;
	}

	public void setBankAccount(String bankAccount) {
		this.bankAccount = bankAccount;
	}

	public String getSuperWeapon() {
		return superWeapon;
	}

	public void setSuperWeapon(String superWeapon) {
		this.superWeapon = superWeapon;
	}
}

 


OK, first I’ll show you how to use Java Reflection to examine object.

Continue reading »