---
title: "java commons-lang"
author: "goahead2010"
date: "2013-07-11T20:18:46Z"
tags: []
categories: ["编程语言"]
slug: "java-commons-lang"
draft: false
lastmod: "2013-07-11T20:18:46Z"
id: 1904014
url: "https://goahead2010.iteye.com/blog/1904014"
---

### [Jakarta Commnos lang学习（1）——ReflectionToStringBuilder](http://blog.csdn.net/gnehzuil/article/details/1559610)

分类： [Java技术](http://blog.csdn.net/gnehzuil/article/category/292253)2007-04-10 20:29 2032人阅读 [评论](http://blog.csdn.net/gnehzuil/article/details/1559610#comments)(0) [收藏]() [举报](http://blog.csdn.net/gnehzuil/article/details/1559610#report "举报")

[import](http://blog.csdn.net/tag/details.html?tag=import)[class](http://blog.csdn.net/tag/details.html?tag=class)[junit](http://blog.csdn.net/tag/details.html?tag=junit)[java](http://blog.csdn.net/tag/details.html?tag=java)[测试](http://blog.csdn.net/tag/details.html?tag=%E6%B5%8B%E8%AF%95)

今天开始学习Jakarta Commons Lang，看的是O'Reilly的《Jakarta Commons Cookbook》。  
ReflectionToStringBuilder类是用来实现类中的toString()方法的类，它采用Java反射机制（Reflection），通过reflection包中的AccessibleObject类绕过访问控制而直接存取对象的私有成员。因此在使用该类时，要注意运行环境的安全策略。  
例（取自《Jakarta Commons Cookbook》）：  
PoliticalCandidate类

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package sample;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.math.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)public class PoliticalCandidate {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String lastName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String firstName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Date dateOfBirth;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private BigDecimal moneyRaised;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private State homeState;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String toString() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return ReflectionToStringBuilder.toString(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public PoliticalCandidate(String lastName, String firstName,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)            Date dob, BigDecimal money, State s) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.lastName = lastName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.firstName = firstName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.dateOfBirth = dob;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.moneyRaised = money;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.homeState = s;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param lastName the lastName to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setLastName(String lastName) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.lastName = lastName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the lastName  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getLastName() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return lastName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param firstName the firstName to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setFirstName(String firstName) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.firstName = firstName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the firstName  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getFirstName() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return firstName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param dateOfBirth the dateOfBirth to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setDateOfBirth(Date dateOfBirth) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.dateOfBirth = dateOfBirth;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the dateOfBirth  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public Date getDateOfBirth() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return dateOfBirth;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param moneyRaised the moneyRaised to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setMoneyRaised(BigDecimal moneyRaised) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.moneyRaised = moneyRaised;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the moneyRaised  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public BigDecimal getMoneyRaised() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return moneyRaised;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param homeState the homeState to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setHomeState(State homeState) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.homeState = homeState;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the homeState  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public State getHomeState() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return homeState;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

State类

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package sample;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)public class State {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String shortName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private String name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public State(String shortName, String name) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.shortName = shortName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.name = name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String toString() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return ReflectionToStringBuilder.toString(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param shortName the shortName to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setShortName(String shortName) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.shortName = shortName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the shortName  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getShortName() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return shortName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param name the name to set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void setName(String name) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.name = name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @return the name  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public String getName() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

测试采用的是JUnit3.81

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) *   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package sample;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.math.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import junit.framework.TestCase;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)/\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @author gnehzuil  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) *  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)public class TestToStirng extends TestCase {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * Test method for {@link sample.PoliticalCandidate#toString()}.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)    public void testToString() {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        State va = new State("VA", "Virginia");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Calendar calendar = new GregorianCalendar();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        calendar.set(Calendar.YEAR, 1743);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        calendar.set(Calendar.MONTH, Calendar.APRIL);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        calendar.set(Calendar.DAY\_OF\_MONTH, 13);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Date dob = calendar.getTime();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BigDecimal moneyRaised = new BigDecimal(293829292.93);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        PoliticalCandidate candidate =   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            new PoliticalCandidate("Jefferson", "Thomas",   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    dob, moneyRaised, va);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println(candidate);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

输出:

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