<?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>Shadow &#38; Honnix</title>
	<atom:link href="http://honnix.com/blog/feed" rel="self" type="application/rss+xml" />
	<link>http://honnix.com/blog</link>
	<description>she&#039;s growing up</description>
	<lastBuildDate>Sun, 15 Jan 2012 10:44:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>在虚拟机里安装openstack</title>
		<link>http://honnix.com/blog/archives/762</link>
		<comments>http://honnix.com/blog/archives/762#comments</comments>
		<pubDate>Sun, 15 Jan 2012 10:44:00 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[openstack]]></category>
		<category><![CDATA[VirtualBox]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=762</guid>
		<description><![CDATA[在VirtualBox的虚拟机里面按照这个文档把openstack装了一把。 准备工作： 创建虚拟机server1和server2，Ubuntu 64位 两块网卡，eth0 NAT，eth1 Internal Network server1分配两块硬盘，第二块专作nova的volume（需要特别指出，openstack的文档对这里没作任何说明，我是做到那步的时候才发现，sda6是神马东西！当然也可以单独分个区，我是没办法了才挂上一块新的硬盘，得亏是虚拟机） 安装OS，没有特别的 然后就是安装文档一步步下去。大部分地方都完全没有问题，有几点需要说一下。 1. 网络配置 VirtualBox虚拟出来的网关是10.0.2.2，不是10.0.2.1。 VirutalBox NAT模式下，两个虚拟机之间实际上是不通的，所以在server2上配置ntp以及nova的时候一定要使用192.168.3.1来连接server1。这一点实际上官方文档写得并不好，已经有了内部网络，为什么还要使用外部的呢？无论是从security还是从逻辑上都说不通。 server1： # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 10.0.2.14 netmask 255.255.255.0 broadcast 10.0.2.255 gateway 10.0.2.2 auto eth1 iface eth1 inet static address 192.168.3.1&#8230;]]></description>
			<content:encoded><![CDATA[<p>在VirtualBox的虚拟机里面按照<a title="starter" href="http://docs.openstack.org/diablo/openstack-compute/starter/content/" target="_blank">这个文档</a>把openstack装了一把。</p>
<p>准备工作：</p>
<ol>
<li>创建虚拟机server1和server2，Ubuntu 64位</li>
<li>两块网卡，eth0 NAT，eth1 Internal Network</li>
<li>server1分配两块硬盘，第二块专作nova的volume（需要特别指出，openstack的文档对这里没作任何说明，我是做到那步的时候才发现，sda6是神马东西！当然也可以单独分个区，我是没办法了才挂上一块新的硬盘，得亏是虚拟机）</li>
<li>安装OS，没有特别的</li>
</ol>
<p>然后就是安装文档一步步下去。大部分地方都完全没有问题，有几点需要说一下。</p>
<p>1. 网络配置</p>
<ul>
<li>VirtualBox虚拟出来的网关是10.0.2.2，不是10.0.2.1。</li>
<li>VirutalBox NAT模式下，两个虚拟机之间实际上是不通的，所以在server2上配置ntp以及nova的时候一定要使用192.168.3.1来连接server1。这一点实际上官方文档写得并不好，已经有了内部网络，为什么还要使用外部的呢？无论是从security还是从逻辑上都说不通。</li>
</ul>
<p>server1：</p>
<blockquote><p>
# The loopback network interface</p>
<p>auto lo</p>
<p>iface lo inet loopback</p>
<p># The primary network interface</p>
<p>auto eth0</p>
<p>iface eth0 inet static</p>
<p>address 10.0.2.14</p>
<p>netmask 255.255.255.0</p>
<p>broadcast 10.0.2.255</p>
<p>gateway 10.0.2.2</p>
<p>auto eth1</p>
<p>iface eth1 inet static</p>
<p>address 192.168.3.1</p>
<p>netmask 255.255.255.0</p>
<p>network 192.168.3.0</p>
<p>broadcast 192.168.3.255</p></blockquote>
<p>server2</p>
<blockquote><p># The loopback network interface</p>
<p>auto lo</p>
<p>iface lo inet loopback</p>
<p># The primary network interface</p>
<p>auto eth0</p>
<p>iface eth0 inet static</p>
<p>address 10.0.2.15</p>
<p>netmask 255.255.255.0</p>
<p>broadcast 10.0.2.255</p>
<p>gateway 10.0.2.2</p>
<p>auto eth1</p>
<p>iface eth1 inet static</p>
<p>address 192.168.3.2</p>
<p>netmask 255.255.255.0</p>
<p>network 192.168.3.0</p>
<p>broadcast 192.168.3.255</p></blockquote>
<p>2. 数据库</p>
<p>为了不跟Oracle扯到一起，没有用MySQL，安装文档上的步骤PostgreSQL完全没有问题。</p>
<p>3. ntp一定要配置正确，server2需要跟server1同步</p>
<p>ntp我不是很懂，配好之后怎么都不同步，把ntpd停掉之后手工跑了一把ntpdate 192.168.3.1，才搞定。应该是我哪里配置的不对。</p>
<p>4. 重启service</p>
<p>诡异得很，sudo restart nova-xxx或者sudo service nova-xxx restart经常不工作，用最土的方法解决：sudo /etc/init.d/nova-xxx restart</p>
<p>5. server2上面需要安装PostgreSQL的python module</p>
<p>sudo apt-get install python-psycopg2</p>
<p>否则nova-compute起不来</p>
<p>6. 试图安装Horizon，未果</p>
<p>Ubuntu上有openstack-dashboard这个包，装好之后按照<a title="OpenStackDashboard" href="http://wiki.openstack.org/OpenStackDashboard" target="_blank">这里</a>配置，还是没起来，应该是python的virtual env没有弄，不懂。</p>
<p>反正Horizon还在猛烈开发中，不怎么稳定，功能也有限，暂时不搞了吧。</p>
<p>7. 貌似server2根本不是必须的</p>
<p>估计就是为了nova-compute自己的分布，对于管理少量的虚拟机来说，一台机器应该足够了。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/762/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>宽带升级光纤</title>
		<link>http://honnix.com/blog/archives/759</link>
		<comments>http://honnix.com/blog/archives/759#comments</comments>
		<pubDate>Tue, 22 Nov 2011 15:10:09 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[光猫]]></category>
		<category><![CDATA[光纤]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=759</guid>
		<description><![CDATA[一直以为住的小区很土，没有光纤，最近意外接到电信电话，说是要升级了，2M变4M不用加钱。升！ 家里的光纤出口在外间屋，二奶机在里间，整个挪出来工程太大，而且没地方放，小P随时可能把二奶机砸坏。同事推荐买了电力猫，285，小贵。 晚上回家折腾，问题开始冒出来了。 两个电力猫始终无法互联。google了半天，才发现是pair那个按钮按的时间太长了。应该按5秒钟的样子就松开，这样电源灯闪烁并开始配对。说明书些得太烂了。可能光盘上的好一些，可是是小盘，还得把外置光驱拿出来，麻烦。 光猫没有管理员权限，默认只给了useradmin。同事说可以找安装的小工要，开始装的时候我不在家，后来打电话过去，关机了，估计下班之后就不接电话了。还是求助于万能的google，发现我这个光猫的型号太容易找出密码了。这个也很快搞定。 如何把光猫变成纯粹的拨号器。其实倒不复杂，只是不确定之前adsl的用户名密码现在还能不能用，网上有人说是一样的，有人说是不一样的，纠结。最后痛下决心，删除已有的配置，改成桥接模式，在路由器上拨号。还好，密码没变，成功了。 之前还尝试过DMZ，不过没搞定，更可怕的是光猫上红灯亮起，告警！别这么寸，刚装好就搞坏了。算了，放弃。 没有IPTV的需求，也没有VoIP的需求，剩下那两个连接就留在那里吧。还有一个问题，远程管理不知道怎么关掉，也不知道能不能关掉，万一哪天电信抽风连上来把我的配置删了就完蛋了。如果光猫被打上patch，拿不到管理员密码就更麻烦了。]]></description>
			<content:encoded><![CDATA[<p>一直以为住的小区很土，没有光纤，最近意外接到电信电话，说是要升级了，2M变4M不用加钱。升！</p>
<p>家里的光纤出口在外间屋，二奶机在里间，整个挪出来工程太大，而且没地方放，小P随时可能把二奶机砸坏。同事推荐买了电力猫，285，小贵。</p>
<p>晚上回家折腾，问题开始冒出来了。</p>
<ol>
<li>两个电力猫始终无法互联。google了半天，才发现是pair那个按钮按的时间太长了。应该按5秒钟的样子就松开，这样电源灯闪烁并开始配对。说明书些得太烂了。可能光盘上的好一些，可是是小盘，还得把外置光驱拿出来，麻烦。</li>
<li>光猫没有管理员权限，默认只给了useradmin。同事说可以找安装的小工要，开始装的时候我不在家，后来打电话过去，关机了，估计下班之后就不接电话了。还是求助于万能的google，发现我这个光猫的型号太容易找出密码了。这个也很快搞定。</li>
<li>如何把光猫变成纯粹的拨号器。其实倒不复杂，只是不确定之前adsl的用户名密码现在还能不能用，网上有人说是一样的，有人说是不一样的，纠结。最后痛下决心，删除已有的配置，改成桥接模式，在路由器上拨号。还好，密码没变，成功了。</li>
<li>之前还尝试过DMZ，不过没搞定，更可怕的是光猫上红灯亮起，告警！别这么寸，刚装好就搞坏了。算了，放弃。</li>
</ol>
<p>没有IPTV的需求，也没有VoIP的需求，剩下那两个连接就留在那里吧。还有一个问题，远程管理不知道怎么关掉，也不知道能不能关掉，万一哪天电信抽风连上来把我的配置删了就完蛋了。如果光猫被打上patch，拿不到管理员密码就更麻烦了。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/759/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scala与PowerMock</title>
		<link>http://honnix.com/blog/archives/753</link>
		<comments>http://honnix.com/blog/archives/753#comments</comments>
		<pubDate>Sat, 27 Aug 2011 12:30:38 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mockStatic]]></category>
		<category><![CDATA[PowerMock]]></category>
		<category><![CDATA[PrepareForTest]]></category>
		<category><![CDATA[RunWith]]></category>
		<category><![CDATA[ScalaTest]]></category>
		<category><![CDATA[Specs]]></category>
		<category><![CDATA[单元测试]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=753</guid>
		<description><![CDATA[这个题目对搜索引擎应该是比较友好的，虽然土了些。 PowerMock是个好东西，在某种程度上拯救了架构设计，使得我们不用过分考虑如何用复杂的架构设计来达到可以单元测试的目的。 Java里面如何使用PowerMock就不用多说了，随便google一下满屏幕都是，如果是同事还可以跟我要codekata的材料。但是这事儿换成Scala，情况就完全不同了。 首先要解决的问题是Scala用什么测试框架。比较流行的有ScalaTest和Specs，二者都支持BDD、ATDD，也就是这个BDD、ATDD把我折磨了一个晚上。 撇开ATDD不谈，我非常想认真尝试一下BDD，但是ScalaTest的BDD需要使用它自己的Runner，Specs的虽然支持JUnit，但是也需要用一个特殊的Runner，这对于PowerMock来说几乎是致命的。PowerMock＋Junit，必须使用PowerMockRunner才能mockStatic，mockNew，PowerMock＋TestNG倒是不需要Runner，但是ScalaTest不支持用TestNG来做BDD，而Specs根本就不支持TestNG。 有点儿混乱，总之一句话，现阶段，Scala＋BDD＋PowerMock这条路完全走不通。如果我的判断有问题，望指正。 那么就退一步，还是JUnit。从一个例子来说明我遭受到的痛苦以及整个解决过程。 1 2 3 4 5 6 7 8 9 10 11 12 public class MyStatic &#123; public static String hello&#40;&#41; &#123; // connect to twitter for example return tweet; &#125; &#125; &#160; public class HelloWorld &#123; public String helloWorld&#40;&#41; &#123; return MyStatic.hello&#40;&#41;; &#125; &#125; 上面一段代码，Java＋PowerMock很容易处理： 1 2 3&#8230;]]></description>
			<content:encoded><![CDATA[<p>这个题目对搜索引擎应该是比较友好的，虽然土了些。</p>
<p><a title="PowerMock" href="http://code.google.com/p/powermock/" target="_blank">PowerMock</a>是个好东西，在某种程度上<a title="单元测试拯救了架构设计？" href="http://honnix.com/blog/archives/747" target="_blank">拯救了架构设计</a>，使得我们不用过分考虑如何用复杂的架构设计来达到可以单元测试的目的。</p>
<p>Java里面如何使用PowerMock就不用多说了，随便google一下满屏幕都是，如果是同事还可以跟我要codekata的材料。但是这事儿换成Scala，情况就完全不同了。</p>
<p>首先要解决的问题是Scala用什么测试框架。比较流行的有<a title="ScalaTest" href="http://www.scalatest.org/" target="_blank">ScalaTest</a>和<a title="Specs" href="http://code.google.com/p/specs/" target="_blank">Specs</a>，二者都支持BDD、ATDD，也就是这个BDD、ATDD把我折磨了一个晚上。</p>
<p>撇开ATDD不谈，我非常想认真尝试一下BDD，但是ScalaTest的BDD需要使用它自己的Runner，Specs的虽然支持JUnit，但是也需要用一个特殊的Runner，这对于PowerMock来说几乎是致命的。PowerMock＋Junit，必须使用PowerMockRunner才能mockStatic，mockNew，PowerMock＋TestNG倒是不需要Runner，但是ScalaTest不支持用TestNG来做BDD，而Specs根本就不支持TestNG。</p>
<p>有点儿混乱，总之一句话，现阶段，Scala＋BDD＋PowerMock这条路完全走不通。如果我的判断有问题，望指正。</p>
<p>那么就退一步，还是JUnit。从一个例子来说明我遭受到的痛苦以及整个解决过程。</p>

<div class="wp_codebox"><table><tr id="p75315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p753code15"><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> MyStatic <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</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> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// connect to twitter for example</span>
        <span style="color: #000000; font-weight: bold;">return</span> tweet<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #009900;">&#123;</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> helloWorld<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> MyStatic.<span style="color: #006633;">hello</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></pre></td></tr></table></div>

<p>上面一段代码，Java＋PowerMock很容易处理：</p>

<div class="wp_codebox"><table><tr id="p75316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p753code16"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>PowerMockRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@PrepareForTest<span style="color: #009900;">&#40;</span>MyStatic.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> HelloWorldTest <span style="color: #009900;">&#123;</span>
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> testHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        mockStatic<span style="color: #009900;">&#40;</span>MyStatic.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ....
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>如果是Scala，问题就接踵而至了。先看功能相似的Scala代码片段：</p>

<div class="wp_codebox"><table><tr id="p75317"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p753code17"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> MyStatic <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> hello <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// connect to twitter for example</span>
    tweet
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> HelloWorld <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> helloWorld<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> MyStatic.<span style="color: #000000;">hello</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>@RunWith没有问题：</p>

<div class="wp_codebox"><table><tr id="p75318"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code18"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>RunWith<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>PowerMockRunner<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>@PrepareForTest就开始出状况了。</p>
<p>首先貌似Scala对annotation的解释有些不太好，不能简单地传一个东西给@PrepareForTest，因为这玩意儿有两个参数，每个都是数组</p>

<div class="wp_codebox"><table><tr id="p75319"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p753code19"><pre class="java" style="font-family:monospace;">@Target<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> ElementType.<span style="color: #006633;">TYPE</span>, ElementType.<span style="color: #006633;">METHOD</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Documented
@Inherited
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> PrepareForTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> IndicateReloadClass.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
&nbsp;
    <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> fullyQualifiedNames<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;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>所以不能：</p>

<div class="wp_codebox"><table><tr id="p75320"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code20"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>SomeClass<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>必须要：</p>

<div class="wp_codebox"><table><tr id="p75321"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code21"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>Array<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>SomeClass<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>这个还算是比较容易解决，但是对于Scala里的object，情况又有变化。object不支持classOf这个运算符，classOf[MyStatic]是非法的；MyStatic.getClass倒是可以拿到类，但这个类是另一个，名字是MyStatic$（后面会详细解释这两个类以及它们之间的关系）。但即使getClass得到的类是正确的，也没法直接传给annotation，因为annotation要求参数必须是一个constant，不能是一个方法调用，因此@PrepareForTest(Array(MyStatic.getClass))也是不行的。</p>
<p>怎么办呢？还好@PrepareForTest还有第二个参数，我们可以这样：</p>

<div class="wp_codebox"><table><tr id="p75322"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code22"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>fullyQualifiedNames <span style="color: #000080;">=</span> Array<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;com.honnix.test.MyStatic&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>虽然比较恶心，而且不利于重构，但是聊胜于无吧。</p>
<p>然后就是mockStatic的问题。怎么写呢？</p>

<div class="wp_codebox"><table><tr id="p75323"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code23"><pre class="scala" style="font-family:monospace;">mockStatic<span style="color: #F78811;">&#40;</span>MyStatic.<span style="color: #000000;">getClass</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>这肯定不行，因为之前说了getClass得到的类是MyStatic$。那么怎么样才能mock MyStatic呢？答案就是，反射。</p>

<div class="wp_codebox"><table><tr id="p75324"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p753code24"><pre class="scala" style="font-family:monospace;">mockStatic<span style="color: #F78811;">&#40;</span>Class.<span style="color: #000000;">forName</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;com.honnix.test.MyStatic&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>没有其他任何办法得到一个object的class信息，这里有一个很古老的<a title="annotation error" href="https://issues.scala-lang.org/browse/SI-2453" target="_blank">bug</a>。</p>
<p>好吧，两个问题都“解决”了，跑跑看。结果却是我想mock的东西根本就没有被mock。要命了。</p>
<p>事情还得从头说起。Scala对于一个object编译的结果是生成两个类，一个MyStatic，一个MyStatic$：</p>

<div class="wp_codebox"><table><tr id="p75325"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p753code25"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> MyStatic
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</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> hello<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> MyStatic$.<span style="color: #006633;">MODULE</span>$.<span style="color: #006633;">hello</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></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p75326"><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="p753code26"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> MyStatic$
  <span style="color: #000000; font-weight: bold;">implements</span> ScalaObject
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span>  MODULE$<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</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> <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> hello<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: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> MyStatic$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    MODULE$ <span style="color: #339933;">=</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>这是反汇编的结果，稍微有点乱，凑合着看吧。不用多解释，代码已经很明白了。</p>
<p>问题是，我对MyStatic进行mock，理论上说也应该可以的，也就是mock之后就无视MyStatic$了。为什么不行呢？还得来看看调用地方的反汇编代码：</p>

<div class="wp_codebox"><table><tr id="p75327"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p753code27"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> testHelloWorld <span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #006633;">MyStatic</span>$.<span style="color: #006633;">MODULE</span>$.<span style="color: #006633;">hello</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>这里面压根儿就没有MyStatic的事儿了！MyStatic被华丽的忽视了！</p>
<p>到此为止终于真相大白了。咱们得这样来mock。</p>

<div class="wp_codebox"><table><tr id="p75328"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p753code28"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>RunWith<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>PowerMockRunner<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #000080;">@</span>PrepareForTest<span style="color: #F78811;">&#40;</span>fullyQualifiedNames <span style="color: #000080;">=</span> Array<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;com.honnix.test.MyStatic$&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> HelloWorldTest <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>Test
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> testStartAndStop<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> mockMyStatic <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#40;</span>MyStatic.<span style="color: #000000;">getClass</span><span style="color: #F78811;">&#41;</span>
    Whitebox.<span style="color: #000000;">setInternalState</span><span style="color: #F78811;">&#40;</span>MyStatic.<span style="color: #000000;">getClass</span>, mockMyStatic<span style="color: #F78811;">&#41;</span>
    ...
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>想法很简单，把MyStatic$里面的MODULE$给换喽。</p>
<p>代码很丑陋，所以需要强调一下PowerMock应该被用在那些确实不需要复杂的依赖注入的地方来帮助简化架构设计，对于需要做依赖注入的地方，我们还是绝对不能松懈的，不能因为PowerMock啥都能mock就乱写代码。</p>
<p>顺便说一句，Specs对于Mockito已经有很好的支持了，加上Scala那种类似DSL的表达式，用起来很爽。希望PowerMock的作者能考虑支持Scala。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/753/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java调用Scala的一个真实案例</title>
		<link>http://honnix.com/blog/archives/750</link>
		<comments>http://honnix.com/blog/archives/750#comments</comments>
		<pubDate>Fri, 12 Aug 2011 15:26:23 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[invoke]]></category>
		<category><![CDATA[调用]]></category>
		<category><![CDATA[转换]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=750</guid>
		<description><![CDATA[Scala调用Java，完全没有问题；但是反过来总有那么一点不爽，或者必须要修改。这里是项目中一个真实的案例，拿出来分享一下。 1. 类型转换 1 2 3 4 5 object AsnLoader &#123; ... def batchLoadFile&#40;files: List&#91;String&#93;&#41;: Map&#91;String, AsnModule&#93; = files.foldLeft&#40;Map&#91;String, AsnModule&#93;&#40;&#41;&#41;&#40;&#40;x, y&#41; =&#62; x ++ load&#40;y&#41;&#41; ... &#125; 这里的List是Scala里的类，从Java里面调这个方法是不行的，所以需要弄一个wrapper方法： 1 2 3 import collection.JavaConversions._ ... def batchLoadFile&#40;files: java.util.List&#91;String&#93;&#41;: java.util.Map&#91;String, AsnModule&#93; = batchLoadFile&#40;asScalaBuffer&#40;files&#41;.toList&#41; 虽然asScalaBuffer本身就是个implicit，但是这里仍需要显示调用一下，否则编译器会认为batchLoadFile(file: java.util.List[String])更符合调用匹配规则，结果就变成了无限递归…… Java里面这样调用： 1 AsnLoader.batchLoadFile&#40;list&#41;; 这样看起来解决问题了，但是Eclipse的Scala插件会报一个莫名其妙的函数签名错误，其实根本就没错，直接maven或者用Intellij都是对的，Eclipse插件太烂或者太老。无奈只好把函数名也一起改了：batchLoadFileJ，这样就可以了。如果还不是很明白，去看看JavaConversions里面的implicit。 把Java的Map转换成Scala的immutale map稍微曲折了些，先转换成mutable map，再调用toMap方法转换成immutable map。 2. 构造方法 1 2&#8230;]]></description>
			<content:encoded><![CDATA[<p>Scala调用Java，完全没有问题；但是反过来总有那么一点不爽，或者必须要修改。这里是项目中一个真实的案例，拿出来分享一下。</p>
<p>1. 类型转换</p>

<div class="wp_codebox"><table><tr id="p75035"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p750code35"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> AsnLoader <span style="color: #F78811;">&#123;</span>
  ...
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> batchLoadFile<span style="color: #F78811;">&#40;</span>files<span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> files.<span style="color: #000000;">foldLeft</span><span style="color: #F78811;">&#40;</span>Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>x, y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> x ++ load<span style="color: #F78811;">&#40;</span>y<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>这里的List是Scala里的类，从Java里面调这个方法是不行的，所以需要弄一个wrapper方法：</p>

<div class="wp_codebox"><table><tr id="p75036"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p750code36"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">import</span></a> collection.<span style="color: #000000;">JavaConversions</span>.<span style="color: #000080;">_</span>
...
<a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> batchLoadFile<span style="color: #F78811;">&#40;</span>files<span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">List</span><span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">Map</span><span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> batchLoadFile<span style="color: #F78811;">&#40;</span>asScalaBuffer<span style="color: #F78811;">&#40;</span>files<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toList</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>虽然asScalaBuffer本身就是个implicit，但是这里仍需要显示调用一下，否则编译器会认为batchLoadFile(file: java.util.List[String])更符合调用匹配规则，结果就变成了无限递归……</p>
<p>Java里面这样调用：</p>

<div class="wp_codebox"><table><tr id="p75037"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p750code37"><pre class="scala" style="font-family:monospace;">AsnLoader.<span style="color: #000000;">batchLoadFile</span><span style="color: #F78811;">&#40;</span>list<span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span></pre></td></tr></table></div>

<p>这样看起来解决问题了，但是Eclipse的Scala插件会报一个莫名其妙的函数签名错误，其实根本就没错，直接maven或者用Intellij都是对的，Eclipse插件太烂或者太老。无奈只好把函数名也一起改了：batchLoadFileJ，这样就可以了。如果还不是很明白，去看看JavaConversions里面的implicit。</p>
<p>把Java的Map转换成Scala的immutale map稍微曲折了些，先转换成mutable map，再调用toMap方法转换成immutable map。</p>
<p>2. 构造方法</p>

<div class="wp_codebox"><table><tr id="p75038"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p750code38"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">class</span></a> DefaultTransformer<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> asnModule<span style="color: #000080;">:</span> AsnModule, <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> dependencies<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Map.<span style="color: #000000;">empty</span>,
                         <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> repository<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Map.<span style="color: #000000;">empty</span>,
                         <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">private</span></a> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">val</span></a> messageHandler<span style="color: #000080;">:</span> MessageHandler <span style="color: #000080;">=</span> ConsoleMessageHandler<span style="color: #F78811;">&#40;</span><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">false</span></a><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  ...
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>这里面的Map都是Scala的Map，所以从Java里不能直接调用，而Scala的构造方法又比较特别，重载的话只能减少参数个数而不能改变类型。这时候就需要companion object上场了。</p>

<div class="wp_codebox"><table><tr id="p75039"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p750code39"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">object</span></a> DefaultTransformer <span style="color: #F78811;">&#123;</span>
  <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">def</span></a> apply<span style="color: #F78811;">&#40;</span>asnModule<span style="color: #000080;">:</span> AsnModule,
                 dependencies<span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">Map</span><span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span>,
                 repository<span style="color: #000080;">:</span> java.<span style="color: #000000;">util</span>.<span style="color: #000000;">Map</span><span style="color: #F78811;">&#91;</span>String, AsnModule<span style="color: #F78811;">&#93;</span>,
                 messageHandler<span style="color: #000080;">:</span> MessageHandler<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <a href="http://scala-lang.org"><span style="color: #0000ff; font-weight: bold;">new</span></a> DefaultTransformer<span style="color: #F78811;">&#40;</span>asnModule, dependencies, repository, messageHandler<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Java这样调用：</p>

<div class="wp_codebox"><table><tr id="p75040"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p750code40"><pre class="scala" style="font-family:monospace;">Transformer transformer <span style="color: #000080;">=</span> DefaultTransformer.<span style="color: #000000;">apply</span><span style="color: #F78811;">&#40;</span>...<span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span></pre></td></tr></table></div>

<p>3. Scala的bug</p>
<p>嗯，XML在什么语言里都是让人恶心的东西，即使Scala已经尽力做好了：<a href="https://issues.scala-lang.org/browse/SI-4865">https://issues.scala-lang.org/browse/SI-4865</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/750/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>单元测试拯救了架构设计？</title>
		<link>http://honnix.com/blog/archives/747</link>
		<comments>http://honnix.com/blog/archives/747#comments</comments>
		<pubDate>Sun, 31 Jul 2011 15:43:02 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[依赖注入]]></category>
		<category><![CDATA[单元测试]]></category>
		<category><![CDATA[架构设计]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=747</guid>
		<description><![CDATA[经常听到这样一种说法：单元测试迫使你不得不进行仔细的架构设计。 这说法没错，如果没有好的架构设计，代码几乎是不能做单元测试的：到处都在new，静态方法随手调用，等等等等，如此这般。 但通常这样做的结果是引入了不必要的过度设计。本来new一个对象就用了，现在要做依赖注入，无论是用spring之类的基于xml的配置注入，还是guice那样的annotation注入，总之一但有了这些，代码几乎就变得不可读了，因为你从代码逻辑里你根本看不出某个对象是什么，是怎么构造出来的；为了弄明白一段代码究竟是怎么工作的，你需要找到做依赖注入的地方：xml或者annotation，或者随便什么。总之如果没有一个好的IDE帮你在各种不同的文件中穿梭，没有一个详细的设计文档，你需要相当长的时间来整明白一个前辈设计的优秀系统是如何被粘在一起工作的。 也许你同样有优于常人的头脑、敏锐的洞察力，可以在很短的时间内把东西看懂，然后继续开发，但并不是所有人都能做到这一点。这样问题就来了，一个所谓优秀的架构设计能维持多久？也许换了几拨人之后，最初的设计思想就完全没人能理解了，然后新人会在代码里恶狠狠地写下shit之类的注释，或者把看不懂的代码拷贝一份出来修修补补，或者经过一番痛苦的思想斗争之后完全重写，替换成他所熟悉的依赖注入框架。天呐，这是在做优秀的架构设计吗？不觉得是在自掘坟墓吗？ 先别准备开口反驳，或者破口大骂，我一点儿反对依赖注入的意思都没有。我非常赞成在系统关键部位使用依赖注入，特别是那些模块接口的地方。这就像中医的针灸，针总是要扎在关键的位置，而不是插满全身，前者治病救人，后者纯粹就是解恨。 而选择依赖注入的方式，能用annotation就别用xml。推崇xml的人最喜欢说的一句话就是：用xml配置一下，可以修改系统行为而不用重新编译，特别是对于Java这样的静态语言。不错，xml是可以做到这一点，但是仔细回想一下，究竟有多少次我们这样干过？究竟有多少是真正的需求，而不是开发人员或者系统设计师一厢情愿的天真想法？注意，我们是在谈依赖注入的配置，而不是单纯的参数配置。必须弄明白的是，用作依赖注入的xml也是代码的一部分，也就是说，如果脱离了xml只看代码，你压根儿就读不懂。而且xml这玩意儿信息冗余得很，你得练就一双法眼，透过tag看本质。顺便说一句，Java里面的xml实现是我见过的最恶心的设计，简直可以作为Java语言使用的优秀典范，因为它可能用到了所有的Java语言特性。 同样的过度设计问题在AOP里也存在，不过这可能完全是我的个人喜好，可以任意批判。我根本无法理解为什么为了省掉一些异常处理的代码，而引入一堆一堆屎一样的xml配置。你可以说这是为了提炼出系统的本质，让人很容易看懂代码逻辑是什么而不用纠缠于异常情况，但是哥们儿，代码里50%以上都是在处理异常，异常根本就是逻辑的一部分，作为一个开发或者维护人员，我需要知道异常时候究竟干了什么，而不是忽略。如果需要业务人员也能很容易明白代码做了什么，我们需要的是DSL，而不是AOP。搞Java的人就喜欢弄出一个个看起来很酷的缩写，AOP，SOA，JEE，JSE，EJB，JCA，JAXP，JAXB，JMX，JMS……我已经晕了，不过也没什么，也就是一帮人躲在屋子里折腾出来的一些规范而已。 在需要依赖注入的地方用annotation的方式注入，在该简洁的地方毫不犹豫地直接调用，允许一定的代码冗余，把架构设计做得让中等水平的人可以很快看懂并且继续开发，让水平次点儿的人也能明白是怎么回事儿，这样的系统也许会具有更强的生命力。 但是一些耦合度很强的类怎样做单元测试呢？其实在Java世界里这个问题早就解决了，有一票儿的单元测试mock工具：JMock，Mockito，Powermock，等等。别再纠结于“做个依赖注入吧，否则怎么做单元测试”这种问题了，依赖注入不是为了解决如何做单元测试的，不能为了单元测试刻意做一些不必要的复杂设计，因为单元测试可能也就是芝麻而已。]]></description>
			<content:encoded><![CDATA[<p>经常听到这样一种说法：单元测试迫使你不得不进行仔细的架构设计。</p>
<p>这说法没错，如果没有好的架构设计，代码几乎是不能做单元测试的：到处都在new，静态方法随手调用，等等等等，如此这般。</p>
<p>但通常这样做的结果是引入了不必要的过度设计。本来new一个对象就用了，现在要做依赖注入，无论是用spring之类的基于xml的配置注入，还是guice那样的annotation注入，总之一但有了这些，代码几乎就变得不可读了，因为你从代码逻辑里你根本看不出某个对象是什么，是怎么构造出来的；为了弄明白一段代码究竟是怎么工作的，你需要找到做依赖注入的地方：xml或者annotation，或者随便什么。总之如果没有一个好的IDE帮你在各种不同的文件中穿梭，没有一个详细的设计文档，你需要相当长的时间来整明白一个前辈设计的优秀系统是如何被粘在一起工作的。</p>
<p>也许你同样有优于常人的头脑、敏锐的洞察力，可以在很短的时间内把东西看懂，然后继续开发，但并不是所有人都能做到这一点。这样问题就来了，一个所谓优秀的架构设计能维持多久？也许换了几拨人之后，最初的设计思想就完全没人能理解了，然后新人会在代码里恶狠狠地写下shit之类的注释，或者把看不懂的代码拷贝一份出来修修补补，或者经过一番痛苦的思想斗争之后完全重写，替换成他所熟悉的依赖注入框架。天呐，这是在做优秀的架构设计吗？不觉得是在自掘坟墓吗？</p>
<p>先别准备开口反驳，或者破口大骂，我一点儿反对依赖注入的意思都没有。我非常赞成在系统关键部位使用依赖注入，特别是那些模块接口的地方。这就像中医的针灸，针总是要扎在关键的位置，而不是插满全身，前者治病救人，后者纯粹就是解恨。</p>
<p>而选择依赖注入的方式，能用annotation就别用xml。推崇xml的人最喜欢说的一句话就是：用xml配置一下，可以修改系统行为而不用重新编译，特别是对于Java这样的静态语言。不错，xml是可以做到这一点，但是仔细回想一下，究竟有多少次我们这样干过？究竟有多少是真正的需求，而不是开发人员或者系统设计师一厢情愿的天真想法？注意，我们是在谈依赖注入的配置，而不是单纯的参数配置。必须弄明白的是，用作依赖注入的xml也是代码的一部分，也就是说，如果脱离了xml只看代码，你压根儿就读不懂。而且xml这玩意儿信息冗余得很，你得练就一双法眼，透过tag看本质。顺便说一句，Java里面的xml实现是我见过的最恶心的设计，简直可以作为Java语言使用的优秀典范，因为它可能用到了所有的Java语言特性。</p>
<p>同样的过度设计问题在AOP里也存在，不过这可能完全是我的个人喜好，可以任意批判。我根本无法理解为什么为了省掉一些异常处理的代码，而引入一堆一堆屎一样的xml配置。你可以说这是为了提炼出系统的本质，让人很容易看懂代码逻辑是什么而不用纠缠于异常情况，但是哥们儿，代码里50%以上都是在处理异常，异常根本就是逻辑的一部分，作为一个开发或者维护人员，我需要知道异常时候究竟干了什么，而不是忽略。如果需要业务人员也能很容易明白代码做了什么，我们需要的是DSL，而不是AOP。搞Java的人就喜欢弄出一个个看起来很酷的缩写，AOP，SOA，JEE，JSE，EJB，JCA，JAXP，JAXB，JMX，JMS……我已经晕了，不过也没什么，也就是一帮人躲在屋子里折腾出来的一些规范而已。</p>
<p>在需要依赖注入的地方用annotation的方式注入，在该简洁的地方毫不犹豫地直接调用，允许一定的代码冗余，把架构设计做得让中等水平的人可以很快看懂并且继续开发，让水平次点儿的人也能明白是怎么回事儿，这样的系统也许会具有更强的生命力。</p>
<p>但是一些耦合度很强的类怎样做单元测试呢？其实在Java世界里这个问题早就解决了，有一票儿的单元测试mock工具：JMock，Mockito，Powermock，等等。别再纠结于“做个依赖注入吧，否则怎么做单元测试”这种问题了，依赖注入不是为了解决如何做单元测试的，不能为了单元测试刻意做一些不必要的复杂设计，因为单元测试可能也就是芝麻而已。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/747/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JRuby和Java的互操作</title>
		<link>http://honnix.com/blog/archives/742</link>
		<comments>http://honnix.com/blog/archives/742#comments</comments>
		<pubDate>Wed, 22 Jun 2011 15:05:42 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[interoperation]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[调用]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=742</guid>
		<description><![CDATA[我试图让新的BL框架支持所有流行的JVM语言。Scala很简单，天衣无缝，但是Ruby和Python就比较周折了。不谈Python，先看看Ruby，确切地说是JRuby。 jrubyc可以把一个Ruby的class编译成Java的，看看下面的例子： Bar.java 1 2 3 4 5 package com.honnix.foo; &#160; interface Bar &#123; int getValue&#40;&#41;; &#125; BarImpl.rb 1 2 3 4 5 6 7 8 9 10 11 java_package 'com.honnix.foo' require 'java' &#160; class BarImpl include Java::com.honnix.foo.Bar &#160; java_signature 'int getValue()' def get&#40;&#41; 1 end end > javac Bar.java > jrubyc --javac -cp com.honnix.foo&#8230;]]></description>
			<content:encoded><![CDATA[<p>我试图让新的BL框架支持所有流行的JVM语言。Scala很简单，天衣无缝，但是Ruby和Python就比较周折了。不谈Python，先看看Ruby，确切地说是JRuby。</p>
<p>jrubyc可以把一个Ruby的class编译成Java的，看看下面的例子：</p>
<p>Bar.java</p>

<div class="wp_codebox"><table><tr id="p74243"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p742code43"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.honnix.foo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> Bar <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> getValue<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>BarImpl.rb</p>

<div class="wp_codebox"><table><tr id="p74244"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p742code44"><pre class="ruby" style="font-family:monospace;">java_package <span style="color:#996600;">'com.honnix.foo'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'java'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> BarImpl
  <span style="color:#9966CC; font-weight:bold;">include</span> Java::com.<span style="color:#9900CC;">honnix</span>.<span style="color:#9900CC;">foo</span>.<span style="color:#9900CC;">Bar</span>
&nbsp;
  java_signature <span style="color:#996600;">'int getValue()'</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><code>> javac Bar.java</code><br />
<code>> jrubyc --javac -cp com.honnix.foo BarImpl.rb</code></p>
<p>这样基本上就差不多了。</p>
<p>不过看看生产的Java代码，就知道这样玩儿的效率恐怕成问题。其实就是先build出来一个Ruby代码的巨大字符串，然后给交给Ruby引擎去跑，最后返回结果。</p>
<p>不管怎么说，只是这是一个方案，接下来就是要考虑怎么把Java的Document转成Ruby喜欢的格式，再转回来。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/742/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java的范型方法</title>
		<link>http://honnix.com/blog/archives/740</link>
		<comments>http://honnix.com/blog/archives/740#comments</comments>
		<pubDate>Fri, 03 Jun 2011 13:06:00 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[generic method]]></category>
		<category><![CDATA[范型]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=740</guid>
		<description><![CDATA[Java的范型一直让我头疼，也一直没仔细研究，总之感觉不好用。这两天折腾了一下，把范型方法整明白了。 一个简单的例子： 1 2 3 4 private static &#60;T&#62; T test&#40;T a&#41; &#123; Person&#60;T&#62; person = new Person&#60;T&#62;&#40;a&#41;; return person.getName&#40;&#41;; &#125; 调用的时候这样： 1 test&#40;1&#41;; 是不是认为编译器编译的时候会自动将T转换成Integer？我们来看看javap的结果： 1 invokestatic #22; //Method test:(Ljava/lang/Object;)Ljava/lang/Object; 看到什么了？没有Integer，只有Object！ 如果这样设计test()： 1 2 3 4 private static &#60;T extends Number&#62; T test&#40;T a&#41; &#123; Person&#60;T&#62; person = new Person&#60;T&#62;&#40;a&#41;; return person.getName&#40;&#41;; &#125; 结果就是： 1&#8230;]]></description>
			<content:encoded><![CDATA[<p>Java的范型一直让我头疼，也一直没仔细研究，总之感觉不好用。这两天折腾了一下，把范型方法整明白了。</p>
<p>一个简单的例子：</p>

<div class="wp_codebox"><table><tr id="p74051"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p740code51"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> T test<span style="color: #009900;">&#40;</span>T a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Person<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">getName</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>调用的时候这样：</p>

<div class="wp_codebox"><table><tr id="p74052"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p740code52"><pre class="java" style="font-family:monospace;">test<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>是不是认为编译器编译的时候会自动将T转换成Integer？我们来看看javap的结果：</p>

<div class="wp_codebox"><table><tr id="p74053"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p740code53"><pre class="java" style="font-family:monospace;">invokestatic	#<span style="color: #cc66cc;">22</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method test:(Ljava/lang/Object;)Ljava/lang/Object;</span></pre></td></tr></table></div>

<p>看到什么了？没有Integer，只有Object！</p>
<p>如果这样设计test()：</p>

<div class="wp_codebox"><table><tr id="p74054"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p740code54"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> Number<span style="color: #339933;">&gt;</span> T test<span style="color: #009900;">&#40;</span>T a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Person<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">getName</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>结果就是：</p>

<div class="wp_codebox"><table><tr id="p74055"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p740code55"><pre class="java" style="font-family:monospace;">invokestatic	#<span style="color: #cc66cc;">23</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method test:(Ljava/lang/Number;)Ljava/lang/Number;</span></pre></td></tr></table></div>

<p>所以是不是可以得出结论，javac根本就不看调用的时候传入什么类型的东西，只看方法定义？</p>
<p>你完全可以这样写：</p>

<div class="wp_codebox"><table><tr id="p74056"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p740code56"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</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> test<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> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Person<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> person.<span style="color: #006633;">getName</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>只是前者看上去比较牛逼罢了，其实效果完全相同。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/740/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>maven-assembly-plugin处理依赖的时候感觉不大对头</title>
		<link>http://honnix.com/blog/archives/733</link>
		<comments>http://honnix.com/blog/archives/733#comments</comments>
		<pubDate>Wed, 25 May 2011 04:52:25 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[dependency]]></category>
		<category><![CDATA[dependencySet]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven-assembly-plugin]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[moduleSet]]></category>
		<category><![CDATA[依赖]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=733</guid>
		<description><![CDATA[按照maven推荐的方式组装binary： 建一个子module packaging类型为pom 依赖需要被组装的子module &#60;useAllReactorProjects&#62;true&#60;/useAllReactorProjects&#62; 但是在写assemble文件的时候，有个诡异的地方：似乎moduleSet的dependencySet并不是选择的某个module所依赖的。 比如有三个module： com.honnix.demo:demo-core com.honnix.demo:demo-api com.honnix:demo:demo-service 其中demo-core runtime依赖slf4j，另外两个provided依赖；demo-core不依赖com.honnix.foo:bar，而另外两个compile依赖。 assemble文件大概是下面这个样子： 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 &#60;assembly&#62; ...&#8230;]]></description>
			<content:encoded><![CDATA[<p>按照maven<a title="module-binaries" href="http://maven.apache.org/plugins/maven-assembly-plugin/faq.html#module-binaries" target="_blank">推荐的方式</a>组装binary：</p>
<ol>
<li>建一个子module</li>
<li>packaging类型为pom</li>
<li>依赖需要被组装的子module</li>
<li>&lt;useAllReactorProjects&gt;true&lt;/useAllReactorProjects&gt;</li>
</ol>
<p>但是在写assemble文件的时候，有个诡异的地方：似乎moduleSet的dependencySet并不是选择的某个module所依赖的。</p>
<p><span id="more-733"></span></p>
<p>比如有三个module：</p>
<ul>
<li>com.honnix.demo:demo-core</li>
<li>com.honnix.demo:demo-api</li>
<li>com.honnix:demo:demo-service</li>
</ul>
<p>其中demo-core runtime依赖slf4j，另外两个provided依赖；demo-core不依赖com.honnix.foo:bar，而另外两个compile依赖。</p>
<p>assemble文件大概是下面这个样子：</p>

<div class="wp_codebox"><table><tr id="p73360"><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
</pre></td><td class="code" id="p733code60"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;assembly<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;moduleSets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        ...
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/dependencies<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0644<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        ...
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/dependencies<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0644<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.foo:bar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/moduleSets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/assembly<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>如果像上面这样写，slf4j最终会被当作runtime的依赖，而com.honnix.foo:bar也会被当作compile的依赖，尽管我已经声明exclude掉。</p>
<p>先说第二个问题，由于前面所说，dependenceSet包括了所有的依赖（包括这个assemble模块的依赖，以及所有传递依赖），所以即使分成两个moduleSet也不行，必须要为所有的moduleSet加上exclude：</p>

<div class="wp_codebox"><table><tr id="p73361"><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="p733code61"><pre class="xml" style="font-family:monospace;">    ...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        ...
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/dependencies<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0644<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.foo:bar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...</pre></td></tr></table></div>

<p>第一个问题其实也不是问题，这正是我期望的，slf4j被runtime依赖。但是换一种写法，如果只写一个moduleSet，结果就不同了：</p>

<div class="wp_codebox"><table><tr id="p73362"><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
</pre></td><td class="code" id="p733code62"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;assembly<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;moduleSets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/useAllReactorProjects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.demo:demo-service<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        ...
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/dependencies<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0644<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.honnix.foo:bar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencySets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binaries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/moduleSet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/moduleSets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/assembly<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>像上面这样，slf4j最终会被当作provided依赖，而默认情况下provided依赖不会被组装，这就跟我想要的完全不同了。</p>
<p>没想明白为什么，但似乎maven-assembly-plugin处理moduleSet的依赖是这样的：</p>
<ul>
<li>依赖包括include的module的依赖，以及assemble module本身的依赖</li>
<li>include的module的依赖合并，算出一个依赖集，再加上assemble module本身的依赖</li>
<li>一个moduleSet和多个在计算合并依赖的时候算法不同（这个有点废话，但不知道确定的行为是什么样子）</li>
</ul>
<p>差不多就是这样吧，我已经写晕了。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/733/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对于cheater-node的总结——underscore篇</title>
		<link>http://honnix.com/blog/archives/726</link>
		<comments>http://honnix.com/blog/archives/726#comments</comments>
		<pubDate>Sun, 24 Apr 2011 08:31:03 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[underscore]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=726</guid>
		<description><![CDATA[javascript确确实实是一个functional的语言，但问题是它本身并没有提供什么方便使用的库。不过还好我们有社区，有无数英雄们默默无闻地做贡献（眼泪花花儿的）。 介绍一下underscore。点过去看看就知道了，亲切得很。 安装很简单，直接npm就行了。不知道npm是什么的，或者google，或者参考这里。 虽然那里有无数的例子，但这里还是给一个吧： 1 2 3 4 5 6 7 8 9 10 11 12 13 var loginInfo = function &#40;&#41; &#123; var info = &#91; &#91;'tz_offset', '480'&#93;, &#91;'username', 'xxx'&#93;, &#91;'password', 'yyy'&#93;, &#91;'realm', 'Zoning'&#93; &#93;; var _ = require&#40;'underscore'&#41;; &#160; return _&#40;info&#41;.map&#40;function &#40;item&#41; &#123; return item&#91;0&#93; + '=' + item&#91;1&#93;; &#125;&#41;.join&#40;'&#38;amp;'&#41;; &#125;; “_(info)”返回一个对象，包含一个名叫_wrapped的property。 &#62;&#8230;]]></description>
			<content:encoded><![CDATA[<p>javascript确确实实是一个functional的语言，但问题是它本身并没有提供什么方便使用的库。不过还好我们有社区，有无数英雄们默默无闻地做贡献（眼泪花花儿的）。</p>
<p>介绍一下<a title="underscore" href="http://documentcloud.github.com/underscore/" target="_blank">underscore</a>。点过去看看就知道了，亲切得很。</p>
<p>安装很简单，直接npm就行了。不知道npm是什么的，或者google，或者参考<a title="npm安装东西时候的困惑" href="http://honnix.com/blog/archives/711">这里</a>。</p>
<p>虽然那里有无数的例子，但这里还是给一个吧：</p>

<div class="wp_codebox"><table><tr id="p72665"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p726code65"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> loginInfo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> info <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tz_offset'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'480'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'username'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'xxx'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'password'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'yyy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'realm'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Zoning'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _ <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'underscore'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> _<span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>“_(info)”返回一个对象，包含一个名叫_wrapped的property。</p>
<pre>&gt; var _ = require('underscore');
&gt; var w = _([1, 2, 3]);
&gt; w
{ _wrapped: [ 1, 2, 3 ] }</pre>
<p>有了这个东西之后一切都变得美好了，跟所有functional的用法一样，点啊点得就干了很多事儿。不过需要注意的是_(info).map()之后返回的东西就是一个数组了。嗯，不太好……</p>
<p>这个问题人家早就考虑过了：</p>

<div class="wp_codebox"><table><tr id="p72666"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p726code66"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> loginInfo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> info <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'tz_offset'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'480'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'username'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'xxx'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'password'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'yyy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'realm'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Zoning'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> _ <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'underscore'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> _<span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">chain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;amp;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</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: #339933;">;</span></pre></td></tr></table></div>

<p>先chain一下，玩儿完之后再value一把。这回爽了吧？</p>
<p>顺便说一下，functional这么好的东西为什么会有人跳出来反对呢？</p>
<p>再多废一句话啊。如果你在node的交互式终端里面使用“_”，你会发现诡异的问题，原因是这个终端默认使用“_”表示上一个命令的返回值。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/726/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对于cheater-node的总结——继承篇</title>
		<link>http://honnix.com/blog/archives/722</link>
		<comments>http://honnix.com/blog/archives/722#comments</comments>
		<pubDate>Sun, 24 Apr 2011 07:58:31 +0000</pubDate>
		<dc:creator>honnix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[EventEmitter]]></category>
		<category><![CDATA[inherit]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://honnix.com/blog/?p=722</guid>
		<description><![CDATA[javascript的对象模型诡异得很，我等半脑残几乎无法领会其精要，仅继承这件简单的事情就有无数种实现方法。不过既然用nodejs，就尽量按照它的标准来走。 Cheater这个类需要触发以及监听事件，所以需要继承EventEmitter这个类。 1 2 3 4 5 6 7 8 var events = require&#40;'events'&#41;; &#160; var Cheater = function &#40;&#41; &#123; events.EventEmitter.call&#40;this&#41;; ... &#125;; &#160; require&#40;'sys'&#41;.inherits&#40;Cheater, events.EventEmitter&#41;; 代码倒是简单得很，不过背后还是做了很多事情的。这里写得很详细，就不废话了：http://blog.nodejitsu.com/using-sys-inherits-in-node-js。 &#160; 更新：sys这个module在nodejs 0.3之后的版本改成了util，为了保持兼容，sys仍可以用，但是不推荐。]]></description>
			<content:encoded><![CDATA[<p>javascript的对象模型诡异得很，我等半脑残几乎无法领会其精要，仅继承这件简单的事情就有无数种实现方法。不过既然用nodejs，就尽量按照它的标准来走。</p>
<p>Cheater这个类需要触发以及监听事件，所以需要继承EventEmitter这个类。</p>

<div class="wp_codebox"><table><tr id="p72268"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p722code68"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> events <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'events'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Cheater <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    events.<span style="color: #660066;">EventEmitter</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inherits</span><span style="color: #009900;">&#40;</span>Cheater<span style="color: #339933;">,</span> events.<span style="color: #660066;">EventEmitter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>代码倒是简单得很，不过背后还是做了很多事情的。这里写得很详细，就不废话了：<a title="nodejs inherit" href="http://blog.nodejitsu.com/using-sys-inherits-in-node-js" target="_blank">http://blog.nodejitsu.com/using-sys-inherits-in-node-js</a>。</p>
<p>&nbsp;</p>
<p>更新：sys这个module在nodejs 0.3之后的版本改成了util，为了保持兼容，sys仍可以用，但是不推荐。</p>
]]></content:encoded>
			<wfw:commentRss>http://honnix.com/blog/archives/722/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

