title: What determines the return order of Class.getMethods()?
body: <p>This question is not looking for a solution to a problem but an explanation of why a problem may have arisen (or not!).</p>
  
  <p>The <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethods()" rel="nofollow">Javadocs</a> for <code>Class.getMethods()</code> say: </p>
  
  <blockquote>
    <p>The elements in the array returned are not sorted and are not in any particular order.</p>
  </blockquote>
  
  <p>The thing is, this:  we’ve been using a neat little Java templating library called <a href="https://github.com/DJCordhose/jmte" rel="nofollow">JMTE</a> for maybe a couple of years now, with no problems.  This uses JSTL-like syntax to inject model values into templates.  In particular, we’ve been using something like this to render a price:</p>
  
  <pre><code>${vendor.currency.symbol} ${order.amount}
  </code></pre>
  
  <p>The first of these translates to the the following:</p>
  
  <pre><code>vendor.getCurrency().getSymbol()
  </code></pre>
  
  <p>where <code>getCurrency()</code> returns a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html" rel="nofollow">java.util.Currency</a> object. Currency has two methods for getting the currency symbol - one that takes a specific Locale and one that uses the default.</p>
  
  <p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html#getSymbol()" rel="nofollow"><code>public String getSymbol()</code></a></p>
  
  <p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html#getSymbol(java.util.Locale)" rel="nofollow"><code>public String getSymbol(Locale locale)</code></a></p>
  
  <p>For the last 18 months or so, everything has been running fine, with currency codes/symbols appearing in emails. Then 5 days ago, we started getting a random <code>IllegalArgumentException</code> thrown when trying to substitute <code>${vendor.currency.symbol}</code></p>
  
  <p>After a bit of debugging I found the cause, deep inside JMTE:</p>
  
  <pre><code>for (Method method : declaredMethods) {
      if (Modifier.isPublic(method.getModifiers())
          &amp;&amp; (method.getName().equals("get" + suffix) ||   
              method.getName().equals("is" + suffix))) {
                  value = method.invoke(o, (Object[]) null);
                  ....
              }
  }
  </code></pre>
  
  <p>i.e., Whether <code>getSymbol()</code> or <code>getSymbol(Locale)</code> was called is completely at the mercy of the return order of <code>Class.getMethods()</code> (<em>not in any particular order</em>).  I added a test to ensure the method has 0 parameters and my problem was solved.</p>
  
  <p>Now, by a strange coincidence, someone else happened to submit an <a href="https://github.com/DJCordhose/jmte/pull/5" rel="nofollow">identical fix</a> on the very same day that we first observed this behaviour.</p>
  
  <p>What’s puzzling is that this code has been running for 18 months with no problems, then all of a sudden, this behaviour appears.</p>
  
  <p>When I created a test case it failed roughly 50% of the time, as one would expect (there are 2 matching methods, returned in no particular order), so I'm puzzled (amazed) it worked for 18 months and 10^5 executions. (It's conceivable, but unlikely, that it has failed but succeeded on subsequent retries).</p>
  
  <p><strong>Purely out of curiosity</strong>, I’m wondering if there’s anything in the Java runtime that might cause this latent behaviour to suddenly appear.  It’s even more puzzling that a fix for this behaviour should be provided by someone else on the same day for a project that is mature and stable - suggesting perhaps that the same latent behaviour suddenly realised itself elsewhere.</p>
  
  <p>So, the question is: does anyone know what factors might influence the order of the methods returned by <code>Class.getMethods()</code>?</p>