<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaChen on JavaReflection</title>
	<atom:link href="http://www.javachen.com/tag/reflection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javachen.com</link>
	<description>Just some sharing about Java open source and life</description>
	<lastBuildDate>Mon, 06 Feb 2012 12:16:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>commons-lang-1.0中的EqualsBuilder和HashCodeBuilder</title>
		<link>http://www.javachen.com/2011/02/commons-lang-1-0-equalsbuilder-hashcodebuilder/</link>
		<comments>http://www.javachen.com/2011/02/commons-lang-1-0-equalsbuilder-hashcodebuilder/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 11:49:10 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java Core]]></category>
		<category><![CDATA[CompareTo]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hashCode]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=1948</guid>
		<description><![CDATA[CompareToBuilder是对jdk中compareTo的一个扩展，提供了一个很好的compareTo()方法，来比较两个对象是否相等。 同样EqualsBuilder和HashCodeBuilder分别是对equals和hashCode方法的扩展。其实，参照CompareToBuilder的实现方式应该很容易理解EqualsBuilder和HashCodeBuilder方式，他们三者都是一种Builder，差别的是具体的实现方式或功能不一样而已，抽象出不同点，他们三者在结构上就是大同小异了。 EqualsBuilder类的核心代码： ?View Code JAVA1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="commons-lang-1.0中的CompareToBuilder" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">commons-lang-1.0中的CompareToBuilder</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Apache Commons Lang ToStringStyle" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F12%2Fapache-commons-lang-tostringstyle%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Apache Commons Lang ToStringStyle</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java中比较两个对象是否相等" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-equals-method%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java中比较两个对象是否相等</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java 反射机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java 反射机制</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>CompareToBuilder是对jdk中compareTo的一个扩展，提供了一个很好的compareTo()方法，来比较两个对象是否相等。<br />
同样EqualsBuilder和HashCodeBuilder分别是对equals和hashCode方法的扩展。其实，参照CompareToBuilder的实现方式应该很容易理解EqualsBuilder和HashCodeBuilder方式，他们三者都是一种Builder，差别的是具体的实现方式或功能不一样而已，抽象出不同点，他们三者在结构上就是大同小异了。<br />
<span id="more-1948"></span></p>
<p>EqualsBuilder类的核心代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1948code3'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p19483"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td class="code" id="p1948code3"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EqualsBuilder <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isEquals<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> EqualsBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        isEquals <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> reflectionEquals<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> lhs, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> reflectionEquals<span style="color: #009900;">&#40;</span>lhs, rhs, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> reflectionEquals<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> lhs, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> rhs, 
            <span style="color: #000066; font-weight: bold;">boolean</span> testTransients<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> rhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">Class</span> c1 <span style="color: #339933;">=</span> lhs.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>c1.<span style="color: #006633;">isInstance</span><span style="color: #009900;">&#40;</span>rhs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Field</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> fields <span style="color: #339933;">=</span> c1.<span style="color: #006633;">getDeclaredFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Field</span></a>.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span>fields, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        EqualsBuilder equalsBuilder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EqualsBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> fields.<span style="color: #006633;">length</span> <span style="color: #339933;">&amp;&amp;</span> equalsBuilder.<span style="color: #006633;">isEquals</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Field</span></a> f <span style="color: #339933;">=</span> fields<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testTransients <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amodifier+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Modifier</span></a>.<span style="color: #006633;">isTransient</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">getModifiers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amodifier+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Modifier</span></a>.<span style="color: #006633;">isStatic</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">getModifiers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                        equalsBuilder.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>lhs<span style="color: #009900;">&#41;</span>, f.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>rhs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalaccessexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalAccessException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//this can't happen. Would get a Security exception instead</span>
                        <span style="color: #666666; font-style: italic;">//throw a runtime exception in case the impossible happens.</span>
                        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainternalerror+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InternalError</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unexpected IllegalAccessException&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> equalsBuilder.<span style="color: #006633;">isEquals</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> EqualsBuilder append<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> lhs, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isEquals <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> rhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            isEquals <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">Class</span> lhsClass <span style="color: #339933;">=</span> lhs.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>lhsClass.<span style="color: #006633;">isArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//the simple case, not an array, just test the element </span>
            isEquals <span style="color: #339933;">=</span> lhs.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//'Switch' on type of array, to dispatch to the correct handler</span>
            <span style="color: #666666; font-style: italic;">// This handles multi dimensional arrays</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">short</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">short</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">short</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Not an array of primitives</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>将上段代码和CompareToBuilder的对应代码相比较，你会发现他们基本上是相同的，唯一不同的是在于具体的append方法，我们来看看他们两者的各自的append是怎么样的.</p>
<p>CompareToBuilder的append方法：
</pre>
<pre lang='java' escaped="true">
    public CompareToBuilder append(long lhs, long rhs) {
        if (comparison != 0) {
            return this;
        }
        comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
        return this;
    }
</pre>
<p>EqualsBuilder 的append方法：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1948code4'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p19484"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p1948code4"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> EqualsBuilder append<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> lhs, <span style="color: #000066; font-weight: bold;">long</span> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isEquals <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        isEquals <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这样就能够发现他们的相同点和不同点了，同样，调用EqualsBuilder的方法和CompareToBuilder类似，你应该知道了。</p>
<p><strong>总结：</strong><br />
1.三者都用到了反射、链式结构来实现各自的逻辑，整体的代码结构是一样的。反射的方法名都是reflectionXXX，望名知义，实现具体操作逻辑的方法都叫append，从方法名可以联想到StringBuffer类，很容易想到该方法使用了链式结构。<br />
2.类名都叫XXXBuilder，为什么这样起名？是否与Builder模式相关？其实，这样取名只是说明这三个类都是构造类，体现命名的规范。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="commons-lang-1.0中的CompareToBuilder" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">commons-lang-1.0中的CompareToBuilder</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Apache Commons Lang ToStringStyle" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F12%2Fapache-commons-lang-tostringstyle%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Apache Commons Lang ToStringStyle</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java中比较两个对象是否相等" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-equals-method%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java中比较两个对象是否相等</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java 反射机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java 反射机制</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/02/commons-lang-1-0-equalsbuilder-hashcodebuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>commons-lang-1.0中的CompareToBuilder</title>
		<link>http://www.javachen.com/2011/02/commons-lang-1-0%e4%b8%ad%e7%9a%84comparetobuilder/</link>
		<comments>http://www.javachen.com/2011/02/commons-lang-1-0%e4%b8%ad%e7%9a%84comparetobuilder/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 11:20:29 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java Core]]></category>
		<category><![CDATA[CompareTo]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=1945</guid>
		<description><![CDATA[CompareToBuilder是对jdk中compareTo的一个扩展，提供了一个很好的compareTo()方法，来比较两个对象是否相等。 这个类通常与EqualsBuilder（对equals方法的扩展）和HashCodeBuilder（对hashcode的扩展）两个类配合使用。 用这个类比较两个对象是否相等的时候，static和transient成员变量会被忽略。 该类中有一个reflectionCompare方法，使用反射决定哪些属性需要被用于测试或是比较两个对象是否相等。对于对象的私有属性，需要用Field.setAccessible()方法修改属性的可见性。除非拥有合适的权限，否则在java的权限管理器下比较会失败。当然，使用反射会比直接比较两个对象是否相等要慢。 该类的核心代码： ?View Code JAVA1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="commons-lang-1.0中的EqualsBuilder和HashCodeBuilder" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">commons-lang-1.0中的EqualsBuilder和HashCodeBuilder</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Apache Commons Lang ToStringStyle" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F12%2Fapache-commons-lang-tostringstyle%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Apache Commons Lang ToStringStyle</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java 反射机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java 反射机制</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java5中的注释Annotation" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java5中的注释Annotation</font>
                    </a>
                </td>
        </tr>
        <br/>
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">来自无觅网络的相关文章：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="一条评论引发的思考。" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwx.si%2Freflections-of-a-comment.gov&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/08/11431888.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">一条评论引发的思考。 (@wx)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="社交化搜索 Google +1" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ftumutanzi.com%2Farchives%2F1876&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/18/31927850.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">社交化搜索 Google +1 (@tumutanzi)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="超1亿用户密码被黑客公开泄露 中国互联网史上最大泄密事件" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.laixijin.com%2F92&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/12/06/11985041.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">超1亿用户密码被黑客公开泄露 中国互联网史上最大泄密事件 (@laixijin)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="熊府" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.xiongx.in%2Fposts%2F221.html&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/11/16929559.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">熊府 (@xiongx)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="《尼基塔》(Nikita)第1季第13集" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.immtime.com%2Fbbs%2Fthread-1208-1-1.html&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/01/18/14130517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">《尼基塔》(Nikita)第1季第13集 (@immtime)</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>CompareToBuilder是对jdk中compareTo的一个扩展，提供了一个很好的compareTo()方法，来比较两个对象是否相等。<br />
这个类通常与EqualsBuilder（对equals方法的扩展）和HashCodeBuilder（对hashcode的扩展）两个类配合使用。<br />
用这个类比较两个对象是否相等的时候，static和transient成员变量会被忽略。<br />
该类中有一个reflectionCompare方法，使用反射决定哪些属性需要被用于测试或是比较两个对象是否相等。对于对象的私有属性，需要用Field.setAccessible()方法修改属性的可见性。除非拥有合适的权限，否则在java的权限管理器下比较会失败。当然，使用反射会比直接比较两个对象是否相等要慢。<br />
<span id="more-1945"></span><br />
该类的核心代码：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1945code8'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p19458"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
</pre></td><td class="code" id="p1945code8"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CompareToBuilder <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> comparison<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> CompareToBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        comparison <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> reflectionCompare<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> lhs, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> reflectionCompare<span style="color: #009900;">&#40;</span>lhs, rhs, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> reflectionCompare<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> lhs, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> rhs, 
            <span style="color: #000066; font-weight: bold;">boolean</span> testTransients<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//判断是否相等</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">//是否有空指针</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> rhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NullPointerException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">//类型是否一致</span>
        <span style="color: #000000; font-weight: bold;">Class</span> c1 <span style="color: #339933;">=</span> lhs.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>c1.<span style="color: #006633;">isInstance</span><span style="color: #009900;">&#40;</span>rhs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aclasscastexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ClassCastException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Field</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> fields <span style="color: #339933;">=</span> c1.<span style="color: #006633;">getDeclaredFields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//修改属性的可见性</span>
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Field</span></a>.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span>fields, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//内部创建一个CompareToBuilder对象，构成链式结构</span>
        CompareToBuilder compareToBuilder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CompareToBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//只有当当前的两个属性相等时候（comparison == 0），才继续往下比较</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> fields.<span style="color: #006633;">length</span> <span style="color: #339933;">&amp;&amp;</span> compareToBuilder.<span style="color: #006633;">comparison</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afield+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Field</span></a> f <span style="color: #339933;">=</span> fields<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">//是否是临时属性</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testTransients <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amodifier+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Modifier</span></a>.<span style="color: #006633;">isTransient</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">getModifiers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            	<span style="color: #666666; font-style: italic;">//是否是静态属性</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amodifier+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Modifier</span></a>.<span style="color: #006633;">isStatic</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">getModifiers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                        compareToBuilder.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>lhs<span style="color: #009900;">&#41;</span>, f.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>rhs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aillegalaccessexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IllegalAccessException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//this can't happen. Would get a Security exception instead</span>
                        <span style="color: #666666; font-style: italic;">//throw a runtime exception in case the impossible happens.</span>
                        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainternalerror+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InternalError</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unexpected IllegalAccessException&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> compareToBuilder.<span style="color: #006633;">toComparison</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> CompareToBuilder append<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> lhs, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>comparison <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> rhs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> rhs <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NullPointerException</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">Class</span> lhsClass <span style="color: #339933;">=</span> lhs.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>lhsClass.<span style="color: #006633;">isArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//the simple case, not an array, just test the element </span>
            comparison <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomparable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Comparable</span></a><span style="color: #009900;">&#41;</span> lhs<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span>rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//'Switch' on type of array, to dispatch to the correct handler</span>
            <span style="color: #666666; font-style: italic;">// This handles multi dimensional arrays</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">short</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">short</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">short</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lhs <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Not an array of primitives</span>
                append<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> lhs, <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> rhs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>该类的使用方法：<br />
1.重写一个类的comapareTo方法,该类需要继承Comparable接口。这种方法是比较两个对象所有属性是否相等来判断两个对象是否相等：<br />
<</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1945code9'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p19459"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1945code9"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> comapareTo<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     MyClass rhs <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>MyClass<span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> CompareToBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>field1, rhs.<span style="color: #006633;">field1</span><span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>field2, rhs.<span style="color: #006633;">field2</span><span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">appendb</span><span style="color: #009900;">&#40;</span>field3, rhs.<span style="color: #006633;">field3</span><span style="color: #009900;">&#41;</span>
                  .<span style="color: #006633;">toComparison</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>2.直接比较两个对象是否相等，需要用到CompareToBuilder的reflectionCompare反射方法，这种方法自动去判断两个对象的所有属性是否相等。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1945code10'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p194510"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1945code10"><pre class="java" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compareTo<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> CompareToBuilder.<span style="color: #006633;">reflectionCompare</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>最后需要注意的是compareTo的逻辑需要与equals和hashcode保持一致性。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="commons-lang-1.0中的EqualsBuilder和HashCodeBuilder" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0-equalsbuilder-hashcodebuilder%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">commons-lang-1.0中的EqualsBuilder和HashCodeBuilder</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Apache Commons Lang ToStringStyle" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F12%2Fapache-commons-lang-tostringstyle%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Apache Commons Lang ToStringStyle</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java 反射机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java 反射机制</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java5中的注释Annotation" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java5中的注释Annotation</font>
                    </a>
                </td>
        </tr>
        <br/>
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">来自无觅网络的相关文章：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="一条评论引发的思考。" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwx.si%2Freflections-of-a-comment.gov&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/06/08/11431888.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">一条评论引发的思考。 (@wx)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="社交化搜索 Google +1" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Ftumutanzi.com%2Farchives%2F1876&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/18/31927850.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">社交化搜索 Google +1 (@tumutanzi)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="超1亿用户密码被黑客公开泄露 中国互联网史上最大泄密事件" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.laixijin.com%2F92&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/12/06/11985041.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">超1亿用户密码被黑客公开泄露 中国互联网史上最大泄密事件 (@laixijin)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="熊府" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.xiongx.in%2Fposts%2F221.html&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/07/11/16929559.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">熊府 (@xiongx)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="《尼基塔》(Nikita)第1季第13集" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.immtime.com%2Fbbs%2Fthread-1208-1-1.html&from=http%3A%2F%2Fwww.javachen.com%2F2011%2F02%2Fcommons-lang-1-0%25E4%25B8%25AD%25E7%259A%2584comparetobuilder%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/01/18/14130517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">《尼基塔》(Nikita)第1季第13集 (@immtime)</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2011/02/commons-lang-1-0%e4%b8%ad%e7%9a%84comparetobuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java5中的注释Annotation</title>
		<link>http://www.javachen.com/2010/01/java5-annotation/</link>
		<comments>http://www.javachen.com/2010/01/java5-annotation/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 02:11:43 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java Core]]></category>
		<category><![CDATA[Annotation]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=951</guid>
		<description><![CDATA[注释是java5中的新特性，谈到注释，先的谈谈Java元数据（metadata）。元数据，就是“关于数据的数据”。Java元数据有3种基本类型，还有3个Java内置注释类型，另外还有4中元注释类型。你可能用过Javadoc的注释自动生成文档。这就是元数据功能的一种。总的来说，元数据可以用来创建文档，跟踪代码的依赖性，执行编译时格式检查，代替已有的配置文件（如Hibernate也提供了注释配置）。 注释有3中基本类型 a.标记注释 &#8211;没有变量，只有名称标识。例如 @annotation b.单一值注释 &#8211;在标记注释的基础上提供一段数据。如 @annotation（“javachen”） c.完整注释 &#8211;可以包括多个数据成员，每个数据成员由名称和值构成。@annotation(site=”www.javachen.com”,author=”javachen”) Java中提供3个内置注释类型 a. Override ，只能用于方法（不能用于类，包声明或者其他构造） 作用：可以保证编译时候Override函数的声明正确性 用法： @Override public void fun(){..} b.Deprecated 同样只能作用与方法 作用：对不应再使用的方法进行注解 用法：@Deprecated public void fun{&#8230;} c.SupressWarnings 可以注释一段代码 作用：关闭特定的警告信息，例如你在使用泛型的时候未指定类型 用法： @SupressWarnings(value={“unchecked”}) Java中还提供了四种元注释，专门负责注释其他的注释 @Target 表示该注释可以用于什么地方。可用的ElementType参数包括： CONSTRUCTOR : 构造器的声明 FIELD : 域声明（包括enum实例） LOCAL_VARIABLE : 局部变量声明 METHOD : 方法声明 PACKAGE : 包声明 PARAMETER : 参数声明 TYPE : [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Java Concurrency Annotations" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F06%2Fjava-concurrency-annotations%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Concurrency Annotations</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java 反射机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java 反射机制</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Enumeration" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fjava-enumeration%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Enumeration</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Generic Types" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-generic-types%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Generic Types</font>
                    </a>
                </td>
        </tr>
        <br/>
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">来自无觅网络的相关文章：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Java的枚举和接口" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.hui-wang.info%2F2012%2F01%2F08%2Fjava-enum-and-polymorphism%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java的枚举和接口 (@hui-wang)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java验证码系列(6):英文字母与数字混合验证码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F637.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/30/3607913.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java验证码系列(6):英文字母与数字混合验证码 (@pihai)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java获取当天后的N天日期" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F821.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java获取当天后的N天日期 (@pihai)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java验证码系列(5):纯数字验证码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F635.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/30/3606074.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java验证码系列(5):纯数字验证码 (@pihai)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java验证码系列(7):中文验证码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F639.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/16/3608117.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java验证码系列(7):中文验证码 (@pihai)</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>注释是java5中的新特性，谈到注释，先的谈谈Java元数据（metadata）。元数据，就是“关于数据的数据”。Java元数据有3种基本类型，还有3个Java内置注释类型，另外还有4中元注释类型。你可能用过Javadoc的注释自动生成文档。这就是元数据功能的一种。总的来说，元数据可以用来创建文档，跟踪代码的依赖性，执行编译时格式检查，代替已有的配置文件（如Hibernate也提供了注释配置）。<br />
<span id="more-951"></span><br />
<strong><span style="color: #0000ff;">注释有3中基本类型</span></strong><br />
a.标记注释      &#8211;没有变量，只有名称标识。例如 @annotation<br />
b.单一值注释    &#8211;在标记注释的基础上提供一段数据。如 @annotation（“javachen”）<br />
c.完整注释      &#8211;可以包括多个数据成员，每个数据成员由名称和值构成。@annotation(site=”www.javachen.com”,author=”javachen”)</p>
<p><span style="color: #0000ff;"><strong>Java中提供3个内置注释类型</strong></span><br />
a. Override ，只能用于方法（不能用于类，包声明或者其他构造）<br />
作用：可以保证编译时候Override函数的声明正确性<br />
用法：<br />
@Override<br />
public void fun(){..}</p>
<p>b.Deprecated  同样只能作用与方法<br />
作用：对不应再使用的方法进行注解<br />
用法：@Deprecated public void fun{&#8230;}</p>
<p>c.SupressWarnings 可以注释一段代码<br />
作用：关闭特定的警告信息，例如你在使用泛型的时候未指定类型<br />
用法： @SupressWarnings(value={“unchecked”})</p>
<p>Java中还提供了四种元注释，专门负责注释其他的注释</p>
<p>@Target   表示该注释可以用于什么地方。可用的ElementType参数包括：<br />
CONSTRUCTOR : 构造器的声明<br />
FIELD : 域声明（包括enum实例）<br />
LOCAL_VARIABLE : 局部变量声明<br />
METHOD : 方法声明<br />
PACKAGE : 包声明<br />
PARAMETER : 参数声明<br />
TYPE : 类、接口 （包括注解类型) 或enum声明</p>
<p>@Retention 表示需要在什么级别保存该注释信息。可选的RetentionPoicy参数包括：<br />
SOURCE : 注释将被编译器丢掉<br />
CLASS : 注释在class文件中可用，但会被VM丢弃<br />
RUNTIME : VM将在运行时也保留注释，因此可以通过反射机制读取注释的信息。</p>
<p>@Documented 将注释包含在JavaDoc中<br />
@Inheried  允许子类继承父类中的注释。</p>
<p><span style="color: #0000ff;"><strong>在Java中定义自己的注释</strong></span></p>
<p>Java语言支持一种新的类型——注释类型（annotation type），跟普通类差不多，在类中以符号（ @ ）的形式注释其他 Java 代码，用@interface 申明自定义注释类型。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p951code13'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p95113"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p951code13"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.javachen.annotation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Documented</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.ElementType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Inherited</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Retention</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.RetentionPolicy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Target</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 用户自定义标签,带有成员变量的MyTag
 */</span>
@Documented <span style="color: #666666; font-style: italic;">//将注释包含在JavaDoc中</span>
@Inherited <span style="color: #666666; font-style: italic;">//允许子类继承父类中的注释。</span>
@Target<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>ElementType.<span style="color: #006633;">METHOD</span>,ElementType.<span style="color: #006633;">CONSTRUCTOR</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">//标注这个注释使用的范围</span>
@Retention<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">//要想使用反射得到注释信息，这个注释必须使用</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> MyTag <span style="color: #009900;">&#123;</span>
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;javachen&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//给自定义注释类的成员加上默认值</span>
	<span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span style="color: #ff0000;">注意：在自定义注释中只有一个成员时，方法名应该为value</span><br />
使用标签最终是为了帮助开发人员提取注释信息，然后根据不同需求做进一步处理，下面我们来看看如何获取注释信息。</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p951code14'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p95114"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p951code14"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.javachen.annotation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Annotation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TagTest <span style="color: #009900;">&#123;</span>
	@MyTag<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MyTag&quot;</span>, age <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override <span style="color: #666666; font-style: italic;">//可以保证编译时候Override函数的声明正确性</span>
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Deprecated <span style="color: #666666; font-style: italic;">//对不应再使用的方法进行注解</span>
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> notToUse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		TagTest tt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TagTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aannotation+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Annotation</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> annotation <span style="color: #339933;">=</span> tt.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span>
					.<span style="color: #006633;">getAnnotations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aannotation+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Annotation</span></a> tag <span style="color: #339933;">:</span> annotation<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Tag is:&quot;</span> <span style="color: #339933;">+</span> tag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tag.name()&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTag<span style="color: #009900;">&#41;</span> tag<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tag.age()&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>MyTag<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>tag<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">age</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anosuchmethodexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NoSuchMethodException</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>需要注意的一点是，在执行这段代码之前我们还有一点小工作要做，还需要给我们的自定义标签MyTag加上一个说明标签，@ Retention, 表明注释信息将可以在运行时刻通过反射机制得到。如果不加入这个标签，上面的代码将没有任何输出。</p>
<p><strong><span style="color: #0000ff;">如何使用反射读取注释</span></strong></p>
<p>在以前的JDK版本中，我们可以使用反射得到类的方法、方法的参数以及其它的类成员等信息。那么在J2SE5.0中同样也可以象方法一样得到注释的各种信息。<br />
在使用反射之前必须使用import java.lang.reflect.* 来导入和反射相关的类。</p>
<p>如果要得到某一个类或接口的注释信息，可以使用如下代码：<br />
Annotation annotation = TestAnnotation.class.getAnnotation(MyAnnotation.class);</p>
<p>如果要得到全部的注释信息可使用如下语句：<br />
Annotation[] annotations = TestAnnotation.class.getAnnotations();<br />
或<br />
Annotation[] annotations = TestAnnotation.class.getDeclaredAnnotations();</p>
<p>getDeclaredAnnotations与getAnnotations类似，但它们不同的是getDeclaredAnnotations 得到的是当前成员所有的注释，不包括继承的。而getAnnotations得到的是包括继承的所有注释。</p>
<p>如果要得到其它成员的注释，可先得到这个成员，然后再得到相应的注释。如得到myMethod的注释。<br />
Method method = TestAnnotation.class.getMethod(“myMethod”, null);<br />
Annotation annotation = method.getAnnotation(MyAnnotation.class);</p>
<p>注：要想使用反射得到注释信息，这个注释必须使用<br />
@Retention(value = RetentionPolicy.RUNTIME)进行注释。</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Java Concurrency Annotations" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2011%2F06%2Fjava-concurrency-annotations%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Concurrency Annotations</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java 反射机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java 反射机制</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Enumeration" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fjava-enumeration%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Enumeration</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Generic Types" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-generic-types%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Generic Types</font>
                    </a>
                </td>
        </tr>
        <br/>
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">来自无觅网络的相关文章：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Java的枚举和接口" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.hui-wang.info%2F2012%2F01%2F08%2Fjava-enum-and-polymorphism%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java的枚举和接口 (@hui-wang)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java验证码系列(6):英文字母与数字混合验证码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F637.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/30/3607913.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java验证码系列(6):英文字母与数字混合验证码 (@pihai)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java获取当天后的N天日期" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F821.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java获取当天后的N天日期 (@pihai)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java验证码系列(5):纯数字验证码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F635.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/30/3606074.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java验证码系列(5):纯数字验证码 (@pihai)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java验证码系列(7):中文验证码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.pihai.me%2Farchives%2F639.html&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/16/3608117.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java验证码系列(7):中文验证码 (@pihai)</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>By the time  your rss reader get this post here is <strong> 2 </strong>comments ,Welcome you come to leave your opinion !]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2010/01/java5-annotation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java 反射机制</title>
		<link>http://www.javachen.com/2010/01/java-reflection/</link>
		<comments>http://www.javachen.com/2010/01/java-reflection/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 02:25:35 +0000</pubDate>
		<dc:creator>JavaChen</dc:creator>
				<category><![CDATA[Java Core]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.javachen.com/?p=933</guid>
		<description><![CDATA[反射的概念是由Smith在1982年首次提出的，主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。 Java中，反射是一种强大的工具。它使您能够创建灵活的代码，这些代码可以在运行时装配，无需在组件之间进行源代表链接。反射允许我们在编写与执行时，使我们的程序代码能够接入装载到JVM中的类的内部信息，而不是源代码中选定的类协作的代码。这使反射成为构建灵活的应用的主要工具。但需注意的是：如果使用不当，反射的成本很高。 java反射所需要的类 1.Class类：它的实例表示正在运行的java应用程序中的类和接口 2.Constructor类：提供 关于类的单个构造方法的信息以及对他的访问权限。 3.Method类：提供有关类或接口的单独的某个方法的信息 4.Array类：提 供动态创建数组或访问数组的静态方法。 5.Field类：提供有关类或接口的属性的信息，以及对他的动态访问权限。 “ Class ” class Class class 十分特殊。它和一般 classes 一样继承自 Object ，其实体用以表达 Java 程序运行时的 classes 和 interfaces ， 也用来表达 enum 、 array 、 primitive Java types （ boolean, byte, char, short, int, long, float, double ）以及关键词 void 。当一个 class 被加载，或当加载器（ class loader ）的 defineClass() 被 JVM [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Java5中的注释Annotation" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java5中的注释Annotation</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Enumeration" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fjava-enumeration%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Enumeration</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java interface RandomAccess" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F10%2Fjava-interface-randomaccess%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java interface RandomAccess</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript和Java的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F09%2Fjavascript_compareto_java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript和Java的区别</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>反射的概念是由Smith在1982年首次提出的，主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。</p>
<p>Java中，反射是一种强大的工具。它使您能够创建灵活的代码，这些代码可以在运行时装配，无需在组件之间进行源代表链接。反射允许我们在编写与执行时，使我们的程序代码能够接入装载到JVM中的类的内部信息，而不是源代码中选定的类协作的代码。这使反射成为构建灵活的应用的主要工具。但需注意的是：如果使用不当，反射的成本很高。<span id="more-933"></span></p>
<p>java反射所需要的类<br />
1.Class类：它的实例表示正在运行的java应用程序中的类和接口<br />
2.Constructor类：提供 关于类的单个构造方法的信息以及对他的访问权限。<br />
3.Method类：提供有关类或接口的单独的某个方法的信息<br />
4.Array类：提 供动态创建数组或访问数组的静态方法。<br />
5.Field类：提供有关类或接口的属性的信息，以及对他的动态访问权限。</p>
<p><strong>“ </strong><strong>Class </strong><strong>” </strong><strong>class</strong></p>
<p>Class class 十分特殊。它和一般 classes 一样继承自 Object ，其实体用以表达 Java 程序运行时的 classes 和 interfaces ， 也用来表达 enum 、  array  、 primitive  Java types （ boolean,  byte, char, short, int, long, float, double ）以及关键词 void 。当一个 class 被加载，或当加载器（ class loader ）的 defineClass() 被 JVM 调用， JVM 便自动产生一个 Class object  。如果您想借由 “ 修改 Java 标准库源码 ” 来 观察 Class object 的实际生成时机（例如在 Class 的 constructor 内添加一个 println() ），不能够！因为 Class 并没有 public constructor</p>
<p><strong>“ </strong><strong>Class </strong><strong>” </strong><strong>object </strong><strong>的取得途径 </strong><strong> </strong></p>
<p>Java 允许我们从多种管道为一个 class 生成对应的 Class object 。下图是 一份整理。</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="187" valign="top">Class object 诞生管道</td>
<td width="381" valign="top">示 例</td>
</tr>
<tr>
<td width="187" valign="top">运 用 getClass()<br />
注： 每个 class 都有此函数</td>
<td width="381" valign="top">String str = “abc”;<br />
Class c1 = str.getClass();</td>
</tr>
<tr>
<td width="187" valign="top">运 用<br />
Class.getSuperclass() <sup>2 </sup></td>
<td width="381" valign="top">Button b = new Button();<br />
Class c1 = b.getClass();<br />
Class c2 = c1.getSuperclass();</td>
</tr>
<tr>
<td width="187" valign="top">运 用 static method<br />
Class.forName()（最 常被使用）</td>
<td width="381" valign="top">Class c1 = Class.forName (“java.lang.String”);<br />
Class c2 = Class.forName (“java.awt.Button”);<br />
Class c3 = Class.forName  (“java.util.LinkedList$Entry”);<br />
Class c4 = Class.forName (“I”);<br />
Class c5 = Class.forName (“[I");</td>
</tr>
<tr>
<td width="187" valign="top">运 用.class 语法</td>
<td width="381" valign="top">Class c1 = String.class;<br />
Class c2 = java.awt.Button.class;<br />
Class c3 = Main.InnerClass.class;<br />
Class c4 = int.class;<br />
Class c5 = int[].class;</td>
</tr>
<tr>
<td width="187" valign="top">运 用<br />
primitive wrapper classes<br />
的  TYPE 语法</td>
<td width="381" valign="top">Class c1 = Boolean.TYPE;<br />
Class c2 = Byte.TYPE;<br />
Class c3 = Character.TYPE;<br />
Class c4 = Short.TYPE;<br />
Class c5 = Integer.TYPE;<br />
Class c6 = Long.TYPE;<br />
Class c7 = Float.TYPE;<br />
Class c8 = Double.TYPE;<br />
Class c9 = Void.TYPE;</td>
</tr>
</tbody>
</table>
<p><strong>Java classes </strong><strong>各成份所对应的 </strong><strong>Reflection APIs </strong></p>
<p>其中出现的 Package 、 Method 、 Constructor 、Field  等等 classes ，都定义于 java.lang.reflect 。</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="115" valign="top">Java class 内部模块</td>
<td width="168" valign="top">Java class 内部模块说明</td>
<td width="159" valign="top">相 应之 Reflection API ，多半为 Class methods 。</td>
<td width="117" valign="top">返 回值类型 (return type)</td>
</tr>
<tr>
<td width="115" valign="top">(1) package</td>
<td width="168" valign="top">class 隶属哪个 package</td>
<td width="159" valign="top">getPackage()</td>
<td width="117" valign="top">Package</td>
</tr>
<tr>
<td width="115" valign="top">(2) import</td>
<td width="168" valign="top">class 导入哪些 classes</td>
<td width="159" valign="top">无 直接对应之 API 。</td>
<td width="117" valign="top"></td>
</tr>
<tr>
<td width="115" valign="top">(3) modifier</td>
<td width="168" valign="top">class （或 methods, fields ）的属性</td>
<td width="159" valign="top">int getModifiers()<br />
Modifier.toString(int)<br />
Modifier.isInterface(int)</td>
<td width="117" valign="top">int<br />
String<br />
bool</td>
</tr>
<tr>
<td width="115" valign="top">(4) class name or interface name</td>
<td width="168" valign="top">class/interface</td>
<td width="159" valign="top">名 称 getName()</td>
<td width="117" valign="top">String</td>
</tr>
<tr>
<td width="115" valign="top">(5) type parameters</td>
<td width="168" valign="top">参 数化类型的名称</td>
<td width="159" valign="top">getTypeParameters()</td>
<td width="117" valign="top">TypeVariable &lt;Class&gt;[]</td>
</tr>
<tr>
<td width="115" valign="top">(6) base class</td>
<td width="168" valign="top">base class （只可能一个）</td>
<td width="159" valign="top">getSuperClass()</td>
<td width="117" valign="top">Class</td>
</tr>
<tr>
<td width="115" valign="top">(7) implemented interfaces</td>
<td width="168" valign="top">实 现有哪些 interfaces</td>
<td width="159" valign="top">getInterfaces()</td>
<td width="117" valign="top">Class[]</td>
</tr>
<tr>
<td width="115" valign="top">(8) inner classes</td>
<td width="168" valign="top">内 部 classes</td>
<td width="159" valign="top">getDeclaredClasses()</td>
<td width="117" valign="top">Class[]</td>
</tr>
<tr>
<td width="115" valign="top">(8&#8242;) outer class</td>
<td width="168" valign="top">如 果我们观察的 class 本身是  inner  classes ，那么相对它就会有个  outer  class 。</td>
<td width="159" valign="top">getDeclaringClass()</td>
<td width="117" valign="top">Class</td>
</tr>
<tr>
<td width="115" valign="top">(9) constructors</td>
<td width="168" valign="top">构 造函数 getDeclaredConstructors()</td>
<td width="159" valign="top">不 论 public 或  private  或其它 access level  ，皆可获得。另有功能近似之取得函数。</td>
<td width="117" valign="top">Constructor[]</td>
</tr>
<tr>
<td width="115" valign="top">(10) methods</td>
<td width="168" valign="top">操 作函数 getDeclaredMethods()</td>
<td width="159" valign="top">不 论 public 或  private  或其它 access level  ，皆可获得。另有功能近似之取得函数。</td>
<td width="117" valign="top">Method[]</td>
</tr>
<tr>
<td width="115" valign="top">(11) fields</td>
<td width="168" valign="top">字 段（成员变量）</td>
<td width="159" valign="top">getDeclaredFields() 不论 public 或 private 或其它 access level ，皆可获得。另有功能近似之取得函数。</td>
<td width="117" valign="top">Field[]</td>
</tr>
</tbody>
</table>
<p>Reflection 的另三个动态性质： (1) 运行时生成 instances ， (2) 执行 期唤起 methods ，  (3) 运行时改动 fields 。</p>
<p><span style="color: #0000ff;">在网上找了一些关于反射的相关讲解，没有发现用详细代码加以注释说明反射的一些用法，看来还是不得不自己深入理解之后，自己写一些详细的使用方法，那时候 再把代码贴出来。</span></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Java5中的注释Annotation" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava5-annotation%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java5中的注释Annotation</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="java.lang.reflect.Array" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F08%2Fjava-lang-reflect-array%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">java.lang.reflect.Array</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java Enumeration" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F03%2Fjava-enumeration%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java Enumeration</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Java interface RandomAccess" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2010%2F10%2Fjava-interface-randomaccess%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Java interface RandomAccess</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="JavaScript和Java的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.javachen.com%2F2009%2F09%2Fjavascript_compareto_java%2F&from=http%3A%2F%2Fwww.javachen.com%2F2010%2F01%2Fjava-reflection%2F">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript和Java的区别</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>Here is no comments yet by the time  your rss reader get this, Do you want to be the first commentor? Hurry up ]]></content:encoded>
			<wfw:commentRss>http://www.javachen.com/2010/01/java-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.javachen.com/tag/reflection/feed/ ) in 8.44181 seconds, on Feb 7th, 2012 at 8:38 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 7th, 2012 at 9:38 pm UTC -->
