<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Jayway Team Blog » Programming</title>
	
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<pubDate>Wed, 18 Mar 2009 10:13:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/jayway/posts/programming" /><feedburner:info uri="jayway/posts/programming" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Configuring Timeout with Apache HttpClient 4.0</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/YkWOGvUzhS0/</link>
		<comments>http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 15:49:23 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[4.0]]></category>

		<category><![CDATA[apache]]></category>

		<category><![CDATA[configuration]]></category>

		<category><![CDATA[httpclient]]></category>

		<category><![CDATA[timeout]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1174</guid>
		<description><![CDATA[Great news everyone: just recently an all-new version of Apache HttpClient was released. HttpClient is now part of the new initiative Apache HttpComponents, which seems to aim for a complete approach to Http programming in Java; server side as well as client side.
I've used Commons HttpClient in older versions on several occasions in the past [...]]]></description>
			<content:encoded><![CDATA[<p>Great news everyone: just recently an all-new version of <a href="http://hc.apache.org/httpcomponents-client/index.html">Apache HttpClient</a> was released. HttpClient is now part of the new initiative <a href="http://hc.apache.org/">Apache HttpComponents</a>, which seems to aim for a complete approach to Http programming in Java; server side as well as client side.</p>
<p>I've used Commons HttpClient in older versions on several occasions in the past and have found it to be extremely useful. I've only just started working with the new version, but there seems to quite a lot of good stuff in there. For example I'm thrilled to see that the Template pattern, heavily used in Spring, has now made its way to the Apache world (e.g. <a href="http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/ResponseHandler.html">ResponseHandler</a>, a callback interface to be used with <a href="http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/HttpClient.html">HttpClient</a> for handling results).</p>
<p>Now, configuration of <code>HttpClient</code> has always been a little bit tricky. There are so many features in there, and most of these are configured using generic parameters (<code>HttpParams</code>), basically name-value pairs of a setting identifier and its value. This has not been made that much easier in the new version.</p>
<p>In my current project I was struggling to find the appropriate parameters for configuring timeouts with the new version of the framework. It turned out to be quite difficult to find clear information on this - the documentation on the 4.0 version of <code>HttpClient</code> is still quite sparse. Since it took me a while to find out how to do this I figured I wasn't alone, so here goes:</p>
<p>It seems that most of the <a href="http://hc.apache.org/httpclient-3.x/apidocs/constant-values.html">Constant Values for the 3.x version</a> are still applicable - I was just unable to find the corresponding document for 4.0. Now, to specify timeouts, I've used two separate parameters: <code>http.connection.timeout</code> and <code>http.socket.timeout</code>. The former is used for specifying the timeout to wait when acquiring a connection to the server; the latter is the socket timeout - this is the timeout used on each individual read operation on the target socket.<br />
Right, the code looks as follows:</p>
<pre class="java">&nbsp;
DefaultHttpClient httpClient = <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
HttpParams params = httpClient.<span style="color: #006600;">getParams</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
params.<span style="color: #006600;">setIntParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http.connection.timeout&quot;</span>, connectionTimeoutMillis<span style="color: #66cc66;">&#41;</span>;
params.<span style="color: #006600;">setIntParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http.socket.timeout&quot;</span>, socketTimeoutMillis<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Simple as that - hard to understand why that should take me so long to figure out <img src='http://blog.jayway.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/YkWOGvUzhS0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/</feedburner:origLink></item>
		<item>
		<title>Executable .jar, with onejar-maven-plugin</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/A8US80mAFeQ/</link>
		<comments>http://blog.jayway.com/2009/03/14/executable-jar-with-onejar-maven-plugin/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 16:25:10 +0000</pubDate>
		<dc:creator>Hugo Josefson</dc:creator>
		
		<category><![CDATA[Open Source]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Tools]]></category>

		<category><![CDATA[assembly]]></category>

		<category><![CDATA[executable]]></category>

		<category><![CDATA[jar]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[maven]]></category>

		<category><![CDATA[onejar]]></category>

		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=972</guid>
		<description><![CDATA[<a href="http://onejar-maven-plugin.googlecode.com/">Onejar-maven-plugin</a> collects your application including dependencies, into one executable jar. It's both easy and works well!

It lets all your dependency jars stay jars, and your code is in its own jar. All of those jars are put in a bigger jar, which is made executable.

<h4>Configuration</h4>
It may sound weird, but it's quite elegant! Just put this in your <code>pom.xml</code>'s <code>&#60;plugins&#62;</code> tag to make it work:...]]></description>
			<content:encoded><![CDATA[<p><a href="http://onejar-maven-plugin.googlecode.com/">Onejar-maven-plugin</a> collects your application including dependencies, into one executable jar. It's both easy and works well!</p>
<h3>Problem</h3>
<p>If you have ever tried doing this before with Maven, you have probably used <a href="http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies">maven-assembly-plugin</a>, which would leave the door open for classpath problems!</p>
<p>It would extract all your dependency jars in one directory together with all your class files and all other classpath resources. The problem is that everything ended up in a big mix, with classpath resources possibly overwriting each other. For example, if two dependencies each had a <code>log4j.properties</code> file in their jar, one <code>log4j.properties</code> would overwrite the other.</p>
<p>No more of that mess!</p>
<h3>Solution</h3>
<p>Enter maven-onejar-plugin. It lets all your dependency jars stay jars, and your code is in its own jar. All of those jars are put in a bigger jar, which is made executable.</p>
<h4>Configuration</h4>
<p>It may sound weird, but it's quite elegant! Just put this in your <code>pom.xml</code>'s <code>&lt;plugins&gt;</code> tag to make it work:</p>
<pre>&lt;plugin&gt;
  &lt;groupId&gt;org.dstovall&lt;/groupId&gt;
  &lt;artifactId&gt;onejar-maven-plugin&lt;/artifactId&gt;
  &lt;version&gt;1.2.3&lt;/version&gt;
  &lt;executions&gt;
    &lt;execution&gt;
      &lt;configuration&gt;
        &lt;mainClass&gt;your.package.YourMainClass&lt;/mainClass&gt;
      &lt;/configuration&gt;
      &lt;goals&gt;
        &lt;goal&gt;one-jar&lt;/goal&gt;
      &lt;/goals&gt;
    &lt;/execution&gt;
  &lt;/executions&gt;
&lt;/plugin&gt;</pre>
<p>You also need to add this to the pom:</p>
<pre>&lt;pluginRepositories&gt;
  &lt;pluginRepository&gt;
    &lt;id&gt;onejar-maven-plugin.googlecode.com&lt;/id&gt;
    &lt;url&gt;http://onejar-maven-plugin.googlecode.com/svn/mavenrepo&lt;/url&gt;
  &lt;/pluginRepository&gt;
&lt;/pluginRepositories&gt;</pre>
<p><em>Please note the new Maven repository URL. Me and onejar-maven-plugin founder Drew Stovall just recently moved the project to Google Code to make it more open and easier for others (like me!) to pitch in. Yesterday, I released version 1.2.3 of onejar-maven-plugin to our new Maven repo at Google Code. Make sure you get the <code>&lt;pluginRepository&gt;</code> URL right so that the latest version will be available to you!</em></p>
<h4>Use</h4>
<p>Then do this to build everything:</p>
<pre>mvn install</pre>
<p>That will build both your normal jar, as well as another jar. You will get these:</p>
<div id="attachment_1152" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.jayway.com/wp-content/uploads/2009/03/onejar-maven-plugin-screenshot.png"><img src="http://blog.jayway.com/wp-content/uploads/2009/03/onejar-maven-plugin-screenshot-300x150.png" alt="Standalone one-jar.jar file, along with the original .jar file." title="onejar-maven-plugin-screenshot" width="300" height="150" class="size-medium wp-image-1152" /></a><p class="wp-caption-text">Standalone one-jar.jar file, along with the original .jar file.</p></div>
<p><code>myApp.one-jar.jar</code> is the big executable that includes both <code>myApp.jar</code>, and all dependency jars.</p>
<p>You can run it standalone without any extra files, like this:</p>
<pre>java -jar target/myApp.one-jar.jar</pre>
<p><em>If you found this interesting, you might find <a href="/2008/11/28/executable-war-with-winstone-maven-plugin/">my post about winstone-maven-plugin</a> enlightning as well. It shows you how to make an executable .war file.</em></p>
<h4>Credits</h4>
<p>Thank you <a href="http://dstovall.com/">Drew Stovall</a> for creating this Maven plugin in the first place, and thank you to the <a href="http://onejar-maven-plugin.googlecode.com/svn/mavensite/history.html">other contributors</a> who have also submitted patches.</p>
<p>Project homepage: <a href="http://onejar-maven-plugin.googlecode.com/">http://onejar-maven-plugin.googlecode.com</a></p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/A8US80mAFeQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/03/14/executable-jar-with-onejar-maven-plugin/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/03/14/executable-jar-with-onejar-maven-plugin/</feedburner:origLink></item>
		<item>
		<title>Scala IDE support</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/Zx-Fo9a9fz8/</link>
		<comments>http://blog.jayway.com/2009/03/12/scala-ide-support/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 07:14:47 +0000</pubDate>
		<dc:creator>Michael Kober</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[ide]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1033</guid>
		<description><![CDATA[Today we wanted to test the scala java interoperabilty in different IDEs. We used the Scala plugin for Eclipse, Netbeans and IntelliJ. The intellij plugin worked out of the box. We managed to get the eclipse plugin working after some trouble as well. Although the netbeans plugin seems promising the interoperability between java and scala [...]]]></description>
			<content:encoded><![CDATA[<p>Today we wanted to test the scala java interoperabilty in different IDEs. We used the Scala plugin for Eclipse, Netbeans and IntelliJ. The intellij plugin worked out of the box. We managed to get the eclipse plugin working after some trouble as well. Although the netbeans plugin seems promising the interoperability between java and scala projects did not work.</p>
<p>The <a href="http://www.scala-lang.org/node/94">eclipse plugin</a> has pretty basic support for code completion, syntax highlihting, and some wizards to create projects, scala classes, objects and traits.<br />
We started out with one java project and one scala project. Testing with the latest release, it was a bit tricky to resolve our imported java classes at build and runtiime and vice versa:</p>
<p>To use a Scala class in your Java project:</p>
<ul>
<li>add the scala project to the build path of your java project (project properties -&gt; Java Build Path -&gt; Projects -&gt; Add...)</li>
<li>add the scala nature to your java project (rightclick your java project -&gt; Scala -&gt; add Scala nature)</li>
</ul>
<p>To use a Java class in your Scala project:</p>
<ul>
<li>import of java.* works fine (for example using java.util.Date in scala, like in the<a href="http://www.scala-lang.org/docu/files/ScalaTutorial.pdf"> Scala Tutorial for Java programmers</a>)</li>
<li>add the java project to the build path of your scala project</li>
</ul>
<p>With the current eclipse plugin it is not possible to use scala objects in your java classes. This seems to be a known issue.</p>
<p>It's even possible to create scala and java classes in the same project if your project has both scala and java nature. But the mixed project support in the plugin is still quite buggy: adding a java class to your scala project sometimes make eclipse think that your scala class is a java class. Closing and reopening your project is a way to get around this.</p>
<p>Overall working with the scala eclipse plugin is quite frustating, as it is still very buggy. Actually the first impression was that interoperability does not work at all in the plugin.</p>
<p>The <a href="http://www.jetbrains.net/confluence/display/SCA/Scala+Plugin+for+IntelliJ+IDEA">IntelliJ Scala plugin</a> 0.2.22340 had much better Scala support with all the above and then refactoring support (rename, move, introduce variable) and open declaration navigation. IntelliJ also supported Java and Scala interoperability even within the same module.</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/Zx-Fo9a9fz8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/03/12/scala-ide-support/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/03/12/scala-ide-support/</feedburner:origLink></item>
		<item>
		<title>Proxy Based AOP for Cocoa Touch</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/5u937TTStAE/</link>
		<comments>http://blog.jayway.com/2009/03/06/proxy-based-aop-for-cocoa-touch/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 14:46:33 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
		
		<category><![CDATA[Embedded]]></category>

		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[aop]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[objective-c]]></category>

		<category><![CDATA[proxy]]></category>

		<category><![CDATA[reflection]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1041</guid>
		<description><![CDATA[UITabBarController is generally used as is, no subclassing required. It creates a UITabBar and manages a list of UIViewControllers, keeping track of the tab in focus, UI creation and everything nice. UITabBarController has a delegate, the UITabBarControllerDelegate protocol. Unfortunately this is not a superset of the UITabBarDelegate protocol, and UITabBarController already implements the UITabBarDelegate protocol [...]]]></description>
			<content:encoded><![CDATA[<p><code>UITabBarController</code> is generally used as is, no subclassing required. It creates a <code>UITabBar</code> and manages a list of <code>UIViewControllers</code>, keeping track of the tab in focus, UI creation and everything nice. <code>UITabBarController</code> has a delegate, the <code>UITabBarControllerDelegate</code> protocol. Unfortunately this is not a superset of the <code>UITabBarDelegate</code> protocol, and <code>UITabBarController</code> already implements the <code>UITabBarDelegate</code> protocol itself. So how can I hook into and respond to delegate calls from the managed <code>UITabBar</code>?</p>
<p>Simply replacing the original delegate will break the default functionality. We need to somehow insert our own code to execute before the original delegates call. Or in AOP speak; a before advice.</p>
<h3>The Wanted Solution</h3>
<p>What we want is a proxy that can forward invocations both to our own delegate, and to the original delegate. The interface for said class looks looks like this:</p>
<pre class="objc"><span style="color: #0000ff;">@protocol</span> CWDelegateOverrideProxyDelegate
@required
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">BOOL</span><span style="color: #002200;">&#41;</span>shouldCallOriginalImplementationForSelector:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">SEL</span><span style="color: #002200;">&#41;</span>aSelector;
<span style="color: #0000ff;">@end</span>
&nbsp;
<span style="color: #0000ff;">@interface</span> CWDelegateOverrideProxy : <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSProxy.html"><span style="color: #0000ff;">NSProxy</span></a> <span style="color: #002200;">&#123;</span>
<span style="color: #0000ff;">@private</span>
  id&lt;NSObject&gt; _originalDelegate;
  id&lt;NSObject&gt; _overrideingDelegate;
  id&lt;CWDelegateOverrideProxyDelegate&gt; _delegate;
<span style="color: #002200;">&#125;</span>
&nbsp;
@property<span style="color: #002200;">&#40;</span>nonatomic, retain, readonly<span style="color: #002200;">&#41;</span> id&lt;NSObject&gt; originalDelegate;
@property<span style="color: #002200;">&#40;</span>nonatomic, retain, readonly<span style="color: #002200;">&#41;</span> id&lt;NSObject&gt; overridingDelegate;
@property<span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> id&lt;CWDelegateOverrideProxyDelegate&gt; delegate;
&nbsp;
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>initWithOriginalDelegate:<span style="color: #002200;">&#40;</span>id&lt;NSObject&gt;<span style="color: #002200;">&#41;</span>originalDelegate
     overridingDelegate:<span style="color: #002200;">&#40;</span>id&lt;NSObject&gt;<span style="color: #002200;">&#41;</span>overridingDelegate;
&nbsp;
<span style="color: #0000ff;">@end</span></pre>
<p>And then we could use this class to replace an existing delegate with a short piece fo code like this:</p>
<pre class="objc">UITabBar* tabBar = <span style="color: #002200;">&#40;</span>UITabBar*<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>self.tabBarController.view
    viewWithKindOfClass:<span style="color: #002200;">&#91;</span>UITabBar <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
CWDelegateOverrideProxy* proxy = <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CWDelegateOverrideProxy alloc<span style="color: #002200;">&#93;</span>
    initWithOriginalDelegate:tabBar.delegate overridingDelegate:self<span style="color: #002200;">&#93;</span>;
tabBar.delegate = <span style="color: #002200;">&#40;</span>id&lt;UITabBarDelegate&gt;<span style="color: #002200;">&#41;</span>proxy;</pre>
<p>See <a href="http://blog.jayway.com/2009/02/24/finding-subview-of-a-particular-class-in-cocoa/">my previous post</a> for the implementation of <code>viewWithKindOfClass:</code>.</p>
<p>Easy enough to use, only thing left is to implement <code>CWDelegateOverrideProxy</code>.</p>
<h3>Enter <code>NSProxy</code></h3>
<p>Java only have one root class; <code>java.lang.Object</code>. Objective-C can have many, and Cocoa defines two; <code>NSObject</code> and <code>NSProxy</code>. Both of them conforms to the protocol <code>NSObject</code>, wich can be confusing. What we need to know is that class names, and protocol names have two different name spaces in Objective-C. Imagine it as if you could have a class and an interface with the same name in Java; in Objective-C you can. <code>NSObject</code> class is the root class for all concrete classes, and <code>NSProxy</code> class is the root class for proxies, both implements <code>NSObject</code> protocol allowing them to be interchangeable.</p>
<p>Without much ado, here is the base implementation for the initializer:</p>
<pre class="objc"><span style="color: #339900;">#import &quot;CWDelegateOverrideProxy.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">@implementation</span> CWDelegateOverrideProxy
&nbsp;
@synthesize originalDelegate = _originalDelegate;
@synthesize overridingDelegate = _overridingDelegate;
@synthesize delegate = _delegate;
&nbsp;
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>initWithOriginalDelegate:<span style="color: #002200;">&#40;</span>id&lt;NSObject&gt;<span style="color: #002200;">&#41;</span>originalDelegate
     overridingDelegate:<span style="color: #002200;">&#40;</span>id&lt;NSObject&gt;<span style="color: #002200;">&#41;</span>overridingDelegate;
<span style="color: #002200;">&#123;</span>
  _originalDelegate = <span style="color: #002200;">&#91;</span>originalDelegate retain<span style="color: #002200;">&#93;</span>;
  _overridingDelegate = <span style="color: #002200;">&#91;</span>overridingDelegate retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">// More code goes here!</span>
&nbsp;
<span style="color: #0000ff;">@end</span></pre>
<h3>Introspection</h3>
<p>In Objective-C, just as in Java, an instance can be queried for its class, and conformance to protocols (interface in Java speak). But as a bonus an instance can also be queried for specific methods per instance.</p>
<p>Our <code>CWDelegateOverrideProxy</code> class inherits from <code>NSProxy</code>. And it makes a simple assumption; if the original delegate, or the overriding delegate is capable of something, then so it is. So if queried for type of class, protocol conformance, or implemented methods, then we reply with whatever our managed delegates replies.</p>
<p>The <code>NSObject</code> protocol defines methods, equivalent of the <code>java.lang.reflect</code> package, that allows us to implement this as such:</p>
<pre class="objc"><span style="color: #339900;">#pragma mark --- Testing Object Behavior, and Conformance</span>
&nbsp;
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">BOOL</span><span style="color: #002200;">&#41;</span>isKindOfClass:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">Class</span><span style="color: #002200;">&#41;</span>aClass;
<span style="color: #002200;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #002200;">&#91;</span>self.overridingDelegate isKindOfClass:aClass<span style="color: #002200;">&#93;</span> ||
         <span style="color: #002200;">&#91;</span>self.originalDelegate isKindOfClass:aClass<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">BOOL</span><span style="color: #002200;">&#41;</span>conformsToProtocol:<span style="color: #002200;">&#40;</span>Protocol *<span style="color: #002200;">&#41;</span>aProtocol;
<span style="color: #002200;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #002200;">&#91;</span>self.overridingDelegate conformsToProtocol:aProtocol<span style="color: #002200;">&#93;</span> ||
         <span style="color: #002200;">&#91;</span>self.originalDelegate conformsToProtocol:aProtocol<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">BOOL</span><span style="color: #002200;">&#41;</span>respondsToSelector:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">SEL</span><span style="color: #002200;">&#41;</span>aSelector;
<span style="color: #002200;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #002200;">&#91;</span>self.overridingDelegate respondsToSelector:aSelector<span style="color: #002200;">&#93;</span> ||
         <span style="color: #002200;">&#91;</span>self.originalDelegate respondsToSelector:aSelector<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre>
<h3>Invocation Forwarding</h3>
<p>The <code>NSObject</code> protocol also defines methods for functionality equivalent of <code>java.lang.reflect.InvocationHandler</code> and hooks to act as a <code>java.lang.reflect.Proxy</code>. The details are different as Objective-C do not enforce calls to only known methods at compile time, which means that at run-time unimplemented methods can be called.</p>
<p>When an unimplemented method is called the run-time will call <code>methodSignatureForSelector:</code>, that can return a <code>NSMethodSignature</code> instance, sort of the equivalent of a <code>java.lang.reflect.Method</code>. The run-time will then use the information from the <code>NSMethodSignature</code> instance to create a correct <code>NSInvocation</code> (other half of a <code>java.lang.reflect.Method</code>) instance, and set the correct arguments. This NSInvocation instance is then used as argument to call <code>forwardInvocation:</code>.</p>
<p>And thus our implementation is completed with:</p>
<pre class="objc"><span style="color: #339900;">#pragma mark --- Handling Proxy Methods</span>
&nbsp;
-<span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSMethodSignature.html"><span style="color: #0000ff;">NSMethodSignature</span></a>*<span style="color: #002200;">&#41;</span>methodSignatureForSelector:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">SEL</span><span style="color: #002200;">&#41;</span>aSelector;
<span style="color: #002200;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self.overridingDelegate respondsToSelector:aSelector<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #002200;">&#91;</span>self.overridingDelegate methodSignatureForSelector:aSelector<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self.originalDelegate respondsToSelector:aSelector<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #002200;">&#91;</span>self.originalDelegate methodSignatureForSelector:aSelector<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #002200;">&#91;</span>super methodSignatureForSelector:aSelector<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #002200;">&#41;</span>forwardInvocation:<span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSInvocation.html"><span style="color: #0000ff;">NSInvocation</span></a>*<span style="color: #002200;">&#41;</span>anInvocation;
<span style="color: #002200;">&#123;</span>
  <span style="color: #0000ff;">BOOL</span> shouldCallOriginal = YES;
  <span style="color: #0000ff;">SEL</span> aSelector = <span style="color: #002200;">&#91;</span>anInvocation selector<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self.overridingDelegate respondsToSelector:aSelector<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>anInvocation setTarget:self.overridingDelegate<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>anInvocation invoke<span style="color: #002200;">&#93;</span>;
    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>self.delegate != <span style="color: #0000ff;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      shouldCallOriginal = <span style="color: #002200;">&#91;</span>self.delegate shouldCallOriginalImplementationForSelector:aSelector<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>shouldCallOriginal &amp;&amp; <span style="color: #002200;">&#91;</span>self.originalDelegate respondsToSelector:aSelector<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>anInvocation setTarget:self.originalDelegate<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>anInvocation invoke<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre>
<p>And that rounds up how to implement a class that handles proxy based AOP, in order to introduce a before advice for delegate methods. For a more complex code base you can look at <a href="http://sourceforge.net/projects/hessiankit/">HessianKit</a>, a framework that uses proxies, invocation forwarding and dynamic class creation in order to use a web service as distributed objects.</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/5u937TTStAE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/03/06/proxy-based-aop-for-cocoa-touch/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/03/06/proxy-based-aop-for-cocoa-touch/</feedburner:origLink></item>
		<item>
		<title>Finding Subview of a Particular Class in Cocoa</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/WKUU7SNDf6k/</link>
		<comments>http://blog.jayway.com/2009/02/24/finding-subview-of-a-particular-class-in-cocoa/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 16:52:17 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
		
		<category><![CDATA[Embedded]]></category>

		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[mobile]]></category>

		<category><![CDATA[objective-c]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1008</guid>
		<description><![CDATA[Many of the UIView subclasses in Cocoa, and especially Cocoa Touch are created by combining many different simple views into a more complex view. UITableViewCell is a good example, concisting of almost a dozen subviews, UIImageView for the image, UILabel for the text, etc.
It is often nice to be able to reuse existing functionality when [...]]]></description>
			<content:encoded><![CDATA[<p>Many of the <code>UIView</code> subclasses in Cocoa, and especially Cocoa Touch are created by combining many different simple views into a more complex view. <code>UITableViewCell</code> is a good example, concisting of almost a dozen subviews, <code>UIImageView</code> for the image, <code>UILabel</code> for the text, etc.</p>
<p>It is often nice to be able to reuse existing functionality when you subclass <code>UITableViewCell</code>. Properties such as image and <code>textColor</code> maps directly to the embedded <code>UILabel</code> and <code>UIImageView</code> subviews, and reimplementing them all when introducing a custom layout is tedious and error prone. Would it not be better to be able to get access to the original subview instances and just add a custom layout to them?</p>
<h3>You can find subviews by class</h3>
<p>Would it not be nice if the Cocoa Touch framework had a method such as this:</p>
<pre class="objc">- <span style="color: #002200;">&#40;</span>UIView*<span style="color: #002200;">&#41;</span>viewWithKindOfClass:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">Class</span><span style="color: #002200;">&#41;</span>aClass;</pre>
<p>Kind of like what <code>viewWithTag:</code>, does but instead of searching for a subview matching a tag, it searches for a subview matching a class.</p>
<p>Good news for us is that this is very easy to implement, by just adding a new category to <code>UIView</code>. Add a new .h/.m file pair to your Xcode project and type this down in the header file:</p>
<pre class="objc"><span style="color: #339900;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">@interface</span> UIView <span style="color: #002200;">&#40;</span>CWViewWithKindOfClass<span style="color: #002200;">&#41;</span>
&nbsp;
- <span style="color: #002200;">&#40;</span>UIView*<span style="color: #002200;">&#41;</span>viewWithKindOfClass:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">Class</span><span style="color: #002200;">&#41;</span>aClass;
&nbsp;
<span style="color: #0000ff;">@end</span></pre>
<p>Now we have to implement it. The existing <code>viewWithTag:</code> method is not documented as such, but my experimentation tells me that it uses a breadth first search algorithm. Quite logical as the wanted subview most commonly is close to the root view, and we want to find it fast. Since <code>viewWithTag:</code> always returns the first found matching view, we can do the same assumption.</p>
<p>The quite short implementation goes like this:</p>
<pre class="objc"><span style="color: #339900;">#import &quot;UIView+CWViewWithKindOfClass.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">@implementation</span> UIView <span style="color: #002200;">&#40;</span>CWViewWithKindOfClass<span style="color: #002200;">&#41;</span>
&nbsp;
- <span style="color: #002200;">&#40;</span>UIView*<span style="color: #002200;">&#41;</span>viewWithKindOfClass:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">Class</span><span style="color: #002200;">&#41;</span>aClass;
<span style="color: #002200;">&#123;</span>
  <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSMutableArray.html"><span style="color: #0000ff;">NSMutableArray</span></a>* nodeQueue = <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSMutableArray.html"><span style="color: #0000ff;">NSMutableArray</span></a> arrayWithObject:self<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>nodeQueue count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #0000dd;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    UIView* node = <span style="color: #002200;">&#91;</span>nodeQueue objectAtIndex:<span style="color: #0000dd;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>nodeQueue removeObjectAtIndex:<span style="color: #0000dd;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>node isKindOfClass:aClass<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #0000ff;">return</span> node;
    <span style="color: #002200;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #002200;">&#91;</span>nodeQueue addObjectsFromArray:node.subviews<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">@end</span></pre>
<p>And that is it, not you can use it with something like for example this:</p>
<pre class="objc">UITabBar* tabBar = <span style="color: #002200;">&#91;</span>myTabBarController.view viewWithKindOfClass:<span style="color: #002200;">&#91;</span>UITabBar <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre>
<p>Wich is an introduction to a future post, where I will discuss how to access the managed <code>UITabBar</code> of a <code>UITabBarController</code>, and introduce a <code>UITabBarDelegate</code> without interfering with the delegate already in place from the Cocoa Touch framework.</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/WKUU7SNDf6k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/02/24/finding-subview-of-a-particular-class-in-cocoa/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/02/24/finding-subview-of-a-particular-class-in-cocoa/</feedburner:origLink></item>
		<item>
		<title>Fixing Image#getRGB()</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/dREH6cYdDA0/</link>
		<comments>http://blog.jayway.com/2009/01/17/fixing-imagegetrgb/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 11:47:25 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
		
		<category><![CDATA[Embedded]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[midp]]></category>

		<category><![CDATA[mobile]]></category>

		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=813</guid>
		<description><![CDATA[I have encountered a problem when implementing a nine-patch painter for MIDP 2.0, most of my graphics where left transparent for no-reason. A nine-patch is an image with a flexible center row, and a flexible center column. Most useful when doing themed UI, as a single image can be used to draw buttons, and boxes [...]]]></description>
			<content:encoded><![CDATA[<p>I have encountered a problem when implementing a nine-patch painter for MIDP 2.0, most of my graphics where left transparent for no-reason. A nine-patch is an image with a flexible center row, and a flexible center column. Most useful when doing themed UI, as a single image can be used to draw buttons, and boxes of any sizes. </p>
<p>The four corners are coped as is, the four borders scaled in a single direction, and the center is scaled in both directions. Since MIDP do not support image scaling this must be implemented by hand using the ARGB values fetched using <code>Image#getRGB()</code>. The documentation is quite straight forward, but fail to mention one pecularity in many implementation...</p>
<h3>Clearing unwanted data</h3>
<p>I want to create the target image in a single <code>int[]</code> array to reduce memory usage. No problem copying image data into this structure, it is just what the quite straightforward API is designed to do:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> getRGB<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> rgbData,
                   <span style="color: #993333;">int</span> offset, <span style="color: #993333;">int</span> scanlength,
                   <span style="color: #993333;">int</span> x, <span style="color: #993333;">int</span> y,
                   <span style="color: #993333;">int</span> width, <span style="color: #993333;">int</span> height<span style="color: #66cc66;">&#41;</span></pre>
<p>Unfortunately the documentation fails to mention that many implementations will not as expected just ignore data in <code>rgbData</code> that is not copied. Instead zeroes, or transparent pixels, will be filled in for any image data that has not been requested on a per row basis. I dare anyone to find where this is mentioned in the <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Image.html">MIDP spec</a>.</p>
<h3>A solution</h3>
<p>The solution is to write your own wrapper method for <code>Image#getRGB()</code> that copies just a single row of image data at a time, and the requested scanline length must be lied about to avoid having the remainder of lines filled with zeroes. Here is tho code ready for copy and paste:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageUtils <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">void</span> getRGBFromImage<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AImage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Image</span></a> image, <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> argbData, <span style="color: #993333;">int</span> offset,
            <span style="color: #993333;">int</span> scanline, <span style="color: #993333;">int</span> x, <span style="color: #993333;">int</span> y, <span style="color: #993333;">int</span> width, <span style="color: #993333;">int</span> height<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> row = <span style="color: #cc66cc;">0</span>; row &lt; height; row++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            image.<span style="color: #006600;">getRGB</span><span style="color: #66cc66;">&#40;</span>argbData, offset, width, x, y + row, width, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
            offset += scanline;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/dREH6cYdDA0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/01/17/fixing-imagegetrgb/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/01/17/fixing-imagegetrgb/</feedburner:origLink></item>
		<item>
		<title>Mocking Eclipse IResource.accept()</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/daoVWhhTq_M/</link>
		<comments>http://blog.jayway.com/2009/01/15/mocking-eclipse-iresourceaccept/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 08:39:48 +0000</pubDate>
		<dc:creator>Tobias Södergren</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Testing]]></category>

		<category><![CDATA[eclipse]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[junit]]></category>

		<category><![CDATA[mock]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=761</guid>
		<description><![CDATA[I had a junit test situation where I wanted to mock an Eclipse IResource instance but still be able to test a call-back implementation given as parameter to the mocked IResource.accept(IResourceVisitor visit) method. By default, mocking an interface gives you "call count" and expected return values but no code is executed. In order to test the implementation of IResourceVisitor, something more had to be done. Here is how I did it.]]></description>
			<content:encoded><![CDATA[<p>I am currently involved in making the ClearCase plug-in for Eclipse, the one hosted at SourceForge, a bit better. Part of the job consists of creating unit tests to make sure that my fixes will stay fixed.</p>
<p>One interesting problem was that in some of my tests, I had to mock the <code>org.eclipse.core.resources.IResource</code> interface. Nothing too hard about that, just write the following code, using EasyMock 2.4:</p>
<pre class="java">&nbsp;
@Override
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">setUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
   resource1 = createMock<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;resource1&quot;</span>, IResource.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
   resource2 = createMock<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;resource2&quot;</span>, IResource.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now to my problem. First, I will discuss what the IResource.accept(IResourceVisitor visitor) metod does:<br />
The IResource.accept(...) method traverses the directory structure under the resource. For each resource found, starting with the IResource on which accept() was called, the visitor.visit(IResource resource) method is called. If the visit(...) method returns true, the directory structure under the resource is traversed, otherwise traversing stops for that specific resource. This way parts of the directory structure under an IResource can be visited.</p>
<p>The problem I encountered was that since I have an IResource mock, there is no code for the accept() method. The mock will just register that there has been a call for accept(), but the code in the IResourceVisitor.visit(IResource resource), which I want tested, is never called.</p>
<p>What to do? There are different solutions, one example is to use an implementation of IResource, for example <code>org.eclipse.core.internal.resources.Folder</code>, and create a partially mocked object and keep the code for accept(). Not a nice solution though, my test would depend on internal code in the Eclipse project. What I did instead was to use the <code>org.easymock.EasyMock.expectLastCall().andAnswer(IAnswer answer)</code> functionality. The andAnswer() method takes an IAnswer callback implementation, like this:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testCollectRefreshStatus<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> CoreException <span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">// Normal mock expects</span>
   expect<span style="color: #66cc66;">&#40;</span>resource1.<span style="color: #006600;">getLocation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">andReturn</span><span style="color: #66cc66;">&#40;</span>iPath1<span style="color: #66cc66;">&#41;</span>;
   expect<span style="color: #66cc66;">&#40;</span>iPath1.<span style="color: #006600;">toOSString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">andReturn</span><span style="color: #66cc66;">&#40;</span>RESOURCE_PATH_1<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">times</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #808080; font-style: italic;">// Provide code for accept(...)</span>
   resource1.<span style="color: #006600;">accept</span><span style="color: #66cc66;">&#40;</span>isA<span style="color: #66cc66;">&#40;</span>IResourceVisitor.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
   expectLastCall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">andAnswer</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> IAnswer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</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: #aaaadd; font-weight: bold;">Object</span></a> answer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Throwable</span></a> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// 1.</span>
        IResourceVisitor visitor = <span style="color: #66cc66;">&#40;</span>IResourceVisitor<span style="color: #66cc66;">&#41;</span> EasyMock.<span style="color: #006600;">getCurrentArguments</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// 2.</span>
        visitor.<span style="color: #006600;">visit</span><span style="color: #66cc66;">&#40;</span>resource2<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 3.</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
   expect<span style="color: #66cc66;">&#40;</span>resource2.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">andReturn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;file.txt&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 4.</span>
&nbsp;
   replayMocks<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #808080; font-style: italic;">// Perform test</span>
   myTestTarget.<span style="color: #006600;">someMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #808080; font-style: italic;">// Verify test</span>
   assertEquals<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, myTestTarget.<span style="color: #006600;">getNumberOfResources</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
   verifyMocks<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Here are some comments to the marked lines in the code above:</p>
<p>1. The answer() method is dictated by the IAnswer interface.<br />
2. EasyMock provides a method to get access to the objects sent as parameters for the expected call. In this case, the call is resource1.accept(...) and the parameter is the IResourceVisitor implementation defined in myTestTarget, which is the code that should be tested.<br />
3. Here the test code has control of the call to the IResourceVisitor.visit() and a mock IResource is provided as parameter. To make the test even better, the boolean return value from the visit() call should be saved and asserted for the correct value to ensure that the intended traversing works as expected.<br />
4. Here the expectations on the mock IResource, for the visit(IResource ...) call, is defined. In this case myTestTarget calls getName() on IResource for some reason, so expectations for that is defined together with a resulting return value that would steer the logic in some direction.</p>
<p>This way the test has full control of the visitor implementation and it is fairly easy to test all the criterias that makes the IResourceVisitor.visit(...) implementation return true or false. Also, the visit() method often changes state of something else, and here it is also possible to test that the state is changed correctly by simulating multiple visit(...) calls, by adding more calls to visit at 3. in the code above. </p>
<p>/Tobias S&ouml;dergren</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/daoVWhhTq_M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/01/15/mocking-eclipse-iresourceaccept/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2009/01/15/mocking-eclipse-iresourceaccept/</feedburner:origLink></item>
		<item>
		<title>UIButton troubles, a better solution</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/_pRQCa1Fyjo/</link>
		<comments>http://blog.jayway.com/2008/12/17/uibutton-troubles-a-better-solution/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 17:10:33 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<category><![CDATA[Embedded]]></category>

		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[objective-c]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=651</guid>
		<description><![CDATA[I described in my previous post how you can change the class of a live object instance. The use-case was a workaround for bug in Cocoa Touch's UIKit. What if I could fix the bug, instead of applying a work around in my sub-class?
Update: Buttons created with UIButtonTypeRoundedRect is a special case returning a private [...]]]></description>
			<content:encoded><![CDATA[<p>I described in my <a href="http://blog.jayway.com/2008/12/12/uibutton-troubles-and-obj-c-magic/">previous post</a> how you can change the class of a live object instance. The use-case was a workaround for bug in Cocoa Touch's UIKit. What if I could fix the bug, instead of applying a work around in my sub-class?</p>
<p><strong>Update:</strong> Buttons created with <code>UIButtonTypeRoundedRect</code> is a special case returning a private subclass of <code>UIButton</code>, this bugfix can therefor not safely be used with rounded buttons. Good news is that they already have a large nice touch area.</p>
<h3>With Objective-C you can</h3>
<p>In short the problem is that the implementation of the factory method <code>buttonWithType:</code> in <code>UIButton</code> do not respect subclasses, so it will always return instances of <code>UIButton</code>. This is bad since it is the only method available to use to get custom buttons. Without access to the source code for <code>UIButton</code> changes can not be made to the implementation, so in order to fix the bug in UIKit we must wrap the original implementation of <code>buttonWithType:</code> with our own method that applies the bugfix. </p>
<p>Adding a category with a new implementation will not work, since that would shadow the original implementation. What is need is some sort of <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a>, and Objective-C gives us enough of this but under the name of <a href="http://www.cocoadev.com/index.pl?MethodSwizzling">method-swizzling</a>.</p>
<p>We can intercept method calls to <code>buttonWithType</code>: by swizzling the implementation with the implementation of our own method. In Objective-C all method implementations are called by name, that name is what is know as a selector. By swizzling the implementations we can make messages with the old selector call our new implementation, and vice versa.</p>
<p>We do this by adding a category to UIButton with this class method:</p>
<pre class="objc"><span style="color: #339900;">#import &lt;objc/runtime.h&gt;</span>
+<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #002200;">&#41;</span>load;
<span style="color: #002200;">&#123;</span>
  Method oldMethod = class_getClassMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #0000ff;">@selector</span><span style="color: #002200;">&#40;</span>buttonWithType:<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
  Method newMethod = class_getClassMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #0000ff;">@selector</span><span style="color: #002200;">&#40;</span>__buttonWithType:<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
  method_exchangeImplementations<span style="color: #002200;">&#40;</span>oldMethod, newMethod<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre>
<p>The class method <code>load</code> is called once for each class and category as they are loaded into the run-time, and is the optimal place to add initialization code. Think of it as static initializer blocks in Java.</p>
<p>And that is it, apart from implementing the actual bugfix in our <code>__buttonWithType:</code> method.</p>
<h3>A better bugfix</h3>
<p>The fix I did in my previous post only worked if the new subclass of <code>UIBUtton</code> did not introduce any new instance variables. This is a limitation, and not quite as obvious to new users of our bugfix, so this time around the bugfix will work with any subclass.</p>
<p>The trick here is allocate a new instance of the correct subclass if needed, and copy over the instance variables. Implementation:</p>
<pre class="objc">+<span style="color: #002200;">&#40;</span>UIButton*<span style="color: #002200;">&#41;</span>__buttonWithType:<span style="color: #002200;">&#40;</span>UIButtonType<span style="color: #002200;">&#41;</span>type;
<span style="color: #002200;">&#123;</span>
  UIButton* button = <span style="color: #002200;">&#91;</span>self __buttonWithType:type<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">Class</span> buttonClass = <span style="color: #002200;">&#91;</span>button <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>button &amp;&amp; buttonClass != self &amp;&amp; buttonClass == <span style="color: #002200;">&#91;</span>UIButton <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #0000ff;">size_t</span> oldSize = class_getInstanceSize<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>button <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #0000ff;">size_t</span> newSize = class_getInstanceSize<span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span>;
    button-&gt;isa = self;
    <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>oldSize &lt; newSize<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      UIButton* newButton = <span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span>;
      <a href="http://www.opengroup.org/onlinepubs/009695399/functions/memcpy.html"><span style="color: #0000dd;">memcpy</span></a><span style="color: #002200;">&#40;</span>newButton, button, oldSize<span style="color: #002200;">&#41;</span>;
      button = <span style="color: #002200;">&#91;</span>newButton autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #0000ff;">return</span> button;
<span style="color: #002200;">&#125;</span></pre>
<p>At first glance the first statement might look like a recursive call, but remember that we have swizzled the implementations so it will call the original implementation of <code>buttonWithType:</code>.</p>
<p>Also note that we must autorelease the new instance that we allocated, and that we must not release the original instance as that one is already owned by an auto release pool.</p>
<p>And that is how you go about fixing bugs in Objective-C for code that you do not have access to the source code for.</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/_pRQCa1Fyjo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/12/17/uibutton-troubles-a-better-solution/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2008/12/17/uibutton-troubles-a-better-solution/</feedburner:origLink></item>
		<item>
		<title>UIButton troubles, and Obj-C magic</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/ULXxviBGBg8/</link>
		<comments>http://blog.jayway.com/2008/12/12/uibutton-troubles-and-obj-c-magic/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 13:54:56 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
		
		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Open Source]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[objective-c]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=557</guid>
		<description><![CDATA[When developing SC68 Player for the iPhone I came upon a quite peculiar problem regarding the UIButton. 
First the preface
I wanted to add a star button to the right side of each row, subclassing UIButton that can manage selected states, images and all I needed, and use it as the accessory view of the UITableViewCell [...]]]></description>
			<content:encoded><![CDATA[<p>When developing <a href="http://www.peylow.se/sc68player.html">SC68 Player</a> for the iPhone I came upon a quite peculiar problem regarding the <code>UIButton</code>. </p>
<h3>First the preface</h3>
<p>I wanted to add a star button to the right side of each row, subclassing UIButton that can manage selected states, images and all I needed, and use it as the accessory view of the <code>UITableViewCell</code> felt natural. It works and is the right thing to do. The simple factory method is:</p>
<pre class="objc">+<span style="color: #002200;">&#40;</span>CWFavouritesButton*<span style="color: #002200;">&#41;</span>favouritesButtonWithTarget:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>target action:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">SEL</span><span style="color: #002200;">&#41;</span>action;
<span style="color: #002200;">&#123;</span>
  CWFavouritesButton* button = <span style="color: #002200;">&#91;</span>self buttonWithType:UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>button<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;
    button.frame = CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">29</span>, <span style="color: #0000dd;">31</span><span style="color: #002200;">&#41;</span>;
    button.backgroundColor = <span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>button setImage:<span style="color: #002200;">&#91;</span>UIImage imageNamed:@<span style="color: #666666;">&quot;star-deselected.png&quot;</span><span style="color: #002200;">&#93;</span> forState:UIControlStateNormal<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>button setImage:<span style="color: #002200;">&#91;</span>UIImage imageNamed:@<span style="color: #666666;">&quot;star-selected.png&quot;</span><span style="color: #002200;">&#93;</span> forState:UIControlStateSelected<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #0000ff;">return</span> button;
<span style="color: #002200;">&#125;</span></pre>
<p>Now if this had been all then, simply adding the factory method with a category on <code>UIButton</code> would have been enough. But as it is, a <code>UIButton</code> uses the alpha mask of its' image as hit target. That hit target is quite small, at least 44 pixels should be used with fingers on a touch screen. Therefor I subclass <code>UIButton</code> so that I can override the hit test as this:</p>
<pre class="objc">-<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">BOOL</span><span style="color: #002200;">&#41;</span>pointInside:<span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point withEvent:<span style="color: #002200;">&#40;</span>UIEvent *<span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
  CGRect bounds = self.bounds;
  bounds = CGRectInset<span style="color: #002200;">&#40;</span>bounds, <span style="color: #0000dd;">-32</span>, <span style="color: #0000dd;">0</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #0000ff;">return</span> CGRectContainsPoint<span style="color: #002200;">&#40;</span>bounds, point<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre>
<p>This would allow for an extra 32 pixels to hit on the left and right side of my star button. But now problems arise! Turns out that the overridden implementation of <code>pointInside:withEvent:</code> is never called.</p>
<p>Many hours of troubleshooting later I figured out that the <code>buttonWithType:</code> factory method is the culprit; it do not respect subclasses. Meaning that it will always return instances of <code>UIButton</code>, and thus always call the original implementation of <code>pointInside:withEvent:</code>. Unfortunetely this is the only public factory or init method that can be used to create a button with images only. What to do?</p>
<h3>Objective-C to the rescue</h3>
<p>With Objective-C we can change the class of an already live object instance, one nice feature of having a dynamic run-time.</p>
<p>All books will tell you that all classes inherits from <code>NSObject</code>. This is not quite true, there is among others <code>NSProxy</code> as well, and you could actually introduce your own root classes if you like. All of them will inherit from the run-time class <code>id</code>. The run-time class <code>id</code> do not implement a single class method, nor instance methods, all it has is a single instance variable: <code>isa</code> of type <code>Class</code>.</p>
<p>As we all should know classes in Objective-C are also object instances, instances of their meta-classes. The <code>isa</code> instance variable is the reference to the object instance's class instance. And here is the gold nugget; it is write-able. So we can change an object instance's class by simply assigning a new value to <code>isa</code>.</p>
<p>So lets update the previous factory method to this:</p>
<pre class="objc">+<span style="color: #002200;">&#40;</span>CWFavouritesButton*<span style="color: #002200;">&#41;</span>favouritesButtonWithTarget:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>target action:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">SEL</span><span style="color: #002200;">&#41;</span>action;
<span style="color: #002200;">&#123;</span>
  CWFavouritesButton* button = <span style="color: #002200;">&#91;</span>self buttonWithType:UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>button<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    button-&gt;isa = <span style="color: #002200;">&#91;</span>CWFavouritesButton <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #ff0000;">// More init code...</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #0000ff;">return</span> button;
<span style="color: #002200;">&#125;</span></pre>
<h3>Some precaution</h3>
<p>This might look dangerous, and it is. But not so dangerous that you should shun it, the Cocoa frameworks uses it to it's advantage, and so should you. The only thing you need to be aware of is that the instance size of the original and new classes must be the same. This is easily done by importing <code>runtime.h</code>, and use <code>class_getInstanceSize()</code>.</p>
<pre class="objc"><span style="color: #339900;">#import &lt;objc/runtime.h&gt;</span>
+<span style="color: #002200;">&#40;</span>CWFavouritesButton*<span style="color: #002200;">&#41;</span>favouritesButtonWithTarget:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">id</span><span style="color: #002200;">&#41;</span>target action:<span style="color: #002200;">&#40;</span><span style="color: #0000ff;">SEL</span><span style="color: #002200;">&#41;</span>action;
<span style="color: #002200;">&#123;</span>
  CWFavouritesButton* button = <span style="color: #002200;">&#91;</span>self buttonWithType:UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;
  <span style="color: #0000ff;">if</span> <span style="color: #002200;">&#40;</span>button &amp;&amp; <span style="color: #002200;">&#40;</span>class_getInstanceSize<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>button <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> == class_getInstanceSize<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CWFavouritesButton <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    button-&gt;isa = <span style="color: #002200;">&#91;</span>CWFavouritesButton <span style="color: #0000ff;">class</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #ff0000;">// More init code...</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #0000ff;">return</span> button;
<span style="color: #002200;">&#125;</span></pre>
<p>And finally our factory method safely return a new button of the correct subclass, and <code>pointInside:withEvent:</code> is called as expected.</p>
<p>The full source code for SC68 Player where I implemented this is available for <a href="http://www.peylow.se/SC68Player101_src.zip">download here</a>.</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/ULXxviBGBg8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/12/12/uibutton-troubles-and-obj-c-magic/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2008/12/12/uibutton-troubles-and-obj-c-magic/</feedburner:origLink></item>
		<item>
		<title>iPhone Tech Talks - Stockholm</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/programming/~3/KV9dFz4qfNs/</link>
		<comments>http://blog.jayway.com/2008/12/09/iphone-tech-talks-stockholm/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 08:03:51 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
		
		<category><![CDATA[Embedded]]></category>

		<category><![CDATA[Events]]></category>

		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Usability]]></category>

		<category><![CDATA[apple]]></category>

		<category><![CDATA[design]]></category>

		<category><![CDATA[iphone]]></category>

		<category><![CDATA[mobile]]></category>

		<category><![CDATA[sdk]]></category>

		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=535</guid>
		<description><![CDATA[Apple has arranged a world tour of iPhone Tech Talks, and in cold december it was Scandinavia's turn and a crew of software evangelists from Apple turned up at KTH in Stockholm for a full day of sessions.
The sessions are confidential so I will keep this post vague on the technical parts, not revealing any [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.apple.com">Apple</a> has arranged a world tour of <a href="http://developer.apple.com/events/iphone/techtalks/">iPhone Tech Talks</a>, and in cold december it was Scandinavia's turn and a crew of software evangelists from Apple turned up at <a href="http://www.kth.se/">KTH</a> in Stockholm for a full day of sessions.</p>
<p>The sessions are confidential so I will keep this post vague on the technical parts, not revealing any thing that might or might not come to an iPhone or iPod touch near you in the future. And I can not mention any specific sales numbers, but I do can say that any predicted numbers you might have seen; is probably an underestimation with a margin and then some.</p>
<p>The sessions where on a high level, not going too deep on technical details. Instead the sessions concentrated on the overall design philosophies, and common gotchas. For the nitty gritty details Apple engineers where available, and I missed half a session discussing the ins and outs of the UIToolbar class with Alberto Arauz. Alberto is one of the european software evangelist, and was most impressed, and interested, when I described the successful <a href="http://www.oredev.org/topmenu/program/trackmobile20.4.2c4b8bee11a31fca49e8000319.html">Mobile 2.0 track</a> at <a href="http://www.oredev.org">Øredev 2008</a>. Apple's overall impression with Scandinavia has so far been a mostly cold hand regarding anything with Apple, going as far as only six people registering for a Tech Talk in Oslo two years ago.</p>
<p>Apart from the speakers and engineers, Apple also brought a video-team. So yours truly has been intervjued about the <a href="http://www.peylow.se/">applications I have</a> on App Store, and my experience with the iPhone SDK. The video will be cut and edited for promotional purposes, and hopefully me mentioning both Malmö and Jayway will not be cut out. The odds of my short talk being included is quite good, as most developers seems to be focusing on games, not utilities as I and Jayway has done.</p>
<p>The best part to take home is not technical, just solid advice for any development, not even under NDA, and the sole point repeated by Apple over and over: <strong>Developing great applications is not about adding features!</strong> The trick is not to cram in as many features as you can, the trick is to know what features to leave out, so that time can be well spend on the key features that do make the cut. And this is tenfold as important on a mobile device where real estate is limited. A quote from Mark Twain:</p>
<blockquote><p>I apologize for writing you a long letter, but I did not have the time to write a short.</p></blockquote>
<p>Writing short, and cutting features is hard. How to decide on the features to drop or implement? Apple uses a simple technique, called a product statement. The product statement for the Photos application is for example; <em>"An easy to use photo sharing application for all iPhone users"</em>. Any feature that do not help fulfilling this statement do not make it. How about simple photo-editing? Nope, does not comply with statement. The wording of the product statement is important, by simply changing it to <em>"An easy to use photo sharing application for <u>professional photographers</u>"</em>, would be a completely new application. Only when the product statement is agreed upon, can an approved list of features be made. An omnipresent Steve Jobs approving and dismissing features, is an exaggeration, this simple method is what do the trick.</p>
<p>Now this might sound both obvious, and counterintuitive. Surely adding that last nice feature would add value? Not at the expense of a cluttered user interface, that is meant to be used while standing in line for 30 seconds. Features thought to be must haves, most of the time are not. Some features thought to be must haves are MMS and video recording. Looking at the raging success of the iPhone, it is very obvious that they are not must haves. We as software engineers must at long last learn that it truly is quality over quantity.</p>
<p>And lastly a personal reflection; from a developer's point of view the best part about iPhone SDK is that it is not an UI framework, it is an Application framework. It do not settle with providing the tools for collections, persisting data, displaying a UI, and then leave the rest of the architecture to the developers. The iPhone SDK supports all three parts of the Model-View-Controller paradigm equally well, and that is what makes it so productive.</p>
<img src="http://feeds.feedburner.com/~r/jayway/posts/programming/~4/KV9dFz4qfNs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/12/09/iphone-tech-talks-stockholm/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.jayway.com/2008/12/09/iphone-tech-talks-stockholm/</feedburner:origLink></item>
	</channel>
</rss>
