今天开始学习Jakarta Commons Lang,看的是O'Reilly的《Jakarta Commons Cookbook》。
ReflectionToStringBuilder类是用来实现类中的toString()方法的类,它采用Java反射机制(Reflection),通过reflection包中的AccessibleObject类绕过访问控制而直接存取对象的私有成员。因此在使用该类时,要注意运行环境的安全策略。
例(取自《Jakarta Commons Cookbook》):
PoliticalCandidate类
ReflectionToStringBuilder类是用来实现类中的toString()方法的类,它采用Java反射机制(Reflection),通过reflection包中的AccessibleObject类绕过访问控制而直接存取对象的私有成员。因此在使用该类时,要注意运行环境的安全策略。
例(取自《Jakarta Commons Cookbook》):
PoliticalCandidate类
package
sample;
import java.math. * ;
import java.util. * ;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
public class PoliticalCandidate {
private String lastName;
private String firstName;
private Date dateOfBirth;
private BigDecimal moneyRaised;
private State homeState;
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
public PoliticalCandidate(String lastName, String firstName,
Date dob, BigDecimal money, State s) {
this.lastName = lastName;
this.firstName = firstName;
this.dateOfBirth = dob;
this.moneyRaised = money;
this.homeState = s;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param dateOfBirth the dateOfBirth to set
*/
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
/**
* @return the dateOfBirth
*/
public Date getDateOfBirth() {
return dateOfBirth;
}
/**
* @param moneyRaised the moneyRaised to set
*/
public void setMoneyRaised(BigDecimal moneyRaised) {
this.moneyRaised = moneyRaised;
}
/**
* @return the moneyRaised
*/
public BigDecimal getMoneyRaised() {
return moneyRaised;
}
/**
* @param homeState the homeState to set
*/
public void setHomeState(State homeState) {
this.homeState = homeState;
}
/**
* @return the homeState
*/
public State getHomeState() {
return homeState;
}
}
State类
import java.math. * ;
import java.util. * ;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
public class PoliticalCandidate {
private String lastName;
private String firstName;
private Date dateOfBirth;
private BigDecimal moneyRaised;
private State homeState;
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
public PoliticalCandidate(String lastName, String firstName,
Date dob, BigDecimal money, State s) {
this.lastName = lastName;
this.firstName = firstName;
this.dateOfBirth = dob;
this.moneyRaised = money;
this.homeState = s;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param dateOfBirth the dateOfBirth to set
*/
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
/**
* @return the dateOfBirth
*/
public Date getDateOfBirth() {
return dateOfBirth;
}
/**
* @param moneyRaised the moneyRaised to set
*/
public void setMoneyRaised(BigDecimal moneyRaised) {
this.moneyRaised = moneyRaised;
}
/**
* @return the moneyRaised
*/
public BigDecimal getMoneyRaised() {
return moneyRaised;
}
/**
* @param homeState the homeState to set
*/
public void setHomeState(State homeState) {
this.homeState = homeState;
}
/**
* @return the homeState
*/
public State getHomeState() {
return homeState;
}
}
package
sample;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
public class State {
private String shortName;
private String name;
public State(String shortName, String name) {
this.shortName = shortName;
this.name = name;
}
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
/**
* @param shortName the shortName to set
*/
public void setShortName(String shortName) {
this.shortName = shortName;
}
/**
* @return the shortName
*/
public String getShortName() {
return shortName;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
}
测试采用的是JUnit3.81
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
public class State {
private String shortName;
private String name;
public State(String shortName, String name) {
this.shortName = shortName;
this.name = name;
}
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
/**
* @param shortName the shortName to set
*/
public void setShortName(String shortName) {
this.shortName = shortName;
}
/**
* @return the shortName
*/
public String getShortName() {
return shortName;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
}
/**
*
*/
package sample;
import java.math. * ;
import java.util. * ;
import junit.framework.TestCase;
/**
* @author gnehzuil
*
*/
public class TestToStirng extends TestCase {
/**
* Test method for {@link sample.PoliticalCandidate#toString()}.
*/
public void testToString() {
State va = new State("VA", "Virginia");
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR, 1743);
calendar.set(Calendar.MONTH, Calendar.APRIL);
calendar.set(Calendar.DAY_OF_MONTH, 13);
Date dob = calendar.getTime();
BigDecimal moneyRaised = new BigDecimal(293829292.93);
PoliticalCandidate candidate =
new PoliticalCandidate("Jefferson", "Thomas",
dob, moneyRaised, va);
System.out.println(candidate);
}
}
*
*/
package sample;
import java.math. * ;
import java.util. * ;
import junit.framework.TestCase;
/**
* @author gnehzuil
*
*/
public class TestToStirng extends TestCase {
/**
* Test method for {@link sample.PoliticalCandidate#toString()}.
*/
public void testToString() {
State va = new State("VA", "Virginia");
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR, 1743);
calendar.set(Calendar.MONTH, Calendar.APRIL);
calendar.set(Calendar.DAY_OF_MONTH, 13);
Date dob = calendar.getTime();
BigDecimal moneyRaised = new BigDecimal(293829292.93);
PoliticalCandidate candidate =
new PoliticalCandidate("Jefferson", "Thomas",
dob, moneyRaised, va);
System.out.println(candidate);
}
}
输出:
PoliticalCandidate[lastName=Jefferson,firstName=Thomas,dateOfBirth=Sat Apr 13 20:16:44 CST 1743,moneyRaised=293829292.930000007152557373046875,homeState=State[shortName=VA,name=Virginia]]
Process finished with exit code 0