title: Code duplication caused by primitive types: How to avoid insanity?
body: <p>In one of my Java projects I am plagued by code repetition due to the way Java handles <a href="https://stackoverflow.com/q/2721546/507519">(not)</a> primitives. After having to manually copy the same change to four different locations (<code>int</code>, <code>long</code>, <code>float</code>, <code>double</code>) <em>again</em>, for the <em>third</em> time, <em>again</em> and <em>again</em> I came really close (?) to snapping.</p>
  
  <p>In various forms, this issue has been brought up now and then on StackOverflow:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/2337170/507519">Managing highly repetitive code and documentation in Java</a></li>
  <li><a href="https://stackoverflow.com/q/2480207/507519">How to avoid repetition when working with primitive types?</a></li>
  <li><a href="https://stackoverflow.com/q/8350129/507519">Passing dynamic list of primitives to a Java method</a></li>
  </ul>
  
  <p>The consensus seemed to converge to two possible alternatives:</p>
  
  <ul>
  <li>Use some sort of code generator.</li>
  <li>What can you do? <em>C'est la vie!</em></li>
  </ul>
  
  <p>Well, the second <em>solution</em> is what I am doing now and it is slowly becoming dangerous for my sanity, much like the <a href="http://en.wikipedia.org/wiki/Chinese_water_torture" rel="nofollow noreferrer">well known torture technique</a>.</p>
  
  <p>Two years have passed since these questions were asked and Java 7 came along. I am, therefore, hopeful for an easier and/or more standard solution.</p>
  
  <ul>
  <li><p>Does Java 7 have any changes that might ease the <em>strain</em> in such cases? I could not find anything in the condensed change summaries, but perhaps there is some obscure new feature somewhere?</p></li>
  <li><p>While source code generation is an alternative, I'd prefer a solution supported using the standard JDK feature set. Sure, using <a href="http://en.wikipedia.org/wiki/C_preprocessor" rel="nofollow noreferrer"><code>cpp</code></a> or another code generator would work, but it adds more dependencies and requires changes to the build system.</p>
  
  <p>The only code generation system of sorts that seems to be supported by the JDK is via the annotations mechanism. I envision a processor that would expand source code like this:</p>
  
  <pre><code>@Primitives({ "int", "long", "float", "double" })
  @PrimitiveVariable
  int max(@PrimitiveVariable int a, @PrimitiveVariable int b) {
      return (a &gt; b)?a:b;
  }
  </code></pre>
  
  <p>The ideal output file would contain the four requested variations of this method, preferrably with associated Javadoc comments e.t.c. Is there somewhere an annotation processor to handle this case? If not, what would it take to build one?</p></li>
  <li><p>Perhaps some other trick that has popped up recently?</p></li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p>An important note: I would not be using primitive types unless I had a reason. Even now there is a very real performance and memory impact by the use of boxed types in some applications.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Using <code>max()</code> as an example allows the use of the <code>compareTo()</code> method that is available in all numeric boxed types. This is a bit trickier:</p>
  
  <pre><code>int sum(int a, int b) {
      return a + b;
  }
  </code></pre>
  
  <p>How could one go about supporting this method for all numeric boxed types without actually writing it six or seven times?</p>