title: How can I place validating constraints on my method input parameters?
body: <p>Here is the typical way of accomplishing this goal:</p>
  
  <pre><code>public void myContractualMethod(final String x, final Set&lt;String&gt; y) {
      if ((x == null) || (x.isEmpty())) {
          throw new IllegalArgumentException("x cannot be null or empty");
      }
      if (y == null) {
          throw new IllegalArgumentException("y cannot be null");
      }
      // Now I can actually start writing purposeful 
      //    code to accomplish the goal of this method
  </code></pre>
  
  <p>I think this solution is ugly. Your methods quickly fill up with boilerplate code checking the valid input parameters contract, obscuring the heart of the method.</p>
  
  <p>Here's what I'd like to have:</p>
  
  <pre><code>public void myContractualMethod(@NotNull @NotEmpty final String x, @NotNull final Set&lt;String&gt; y) {
      // Now I have a clean method body that isn't obscured by
      //    contract checking
  </code></pre>
  
  <p>If those annotations look like JSR 303/Bean Validation Spec, it's because I borrowed them. Unfortunitely they don't seem to work this way; they are intended for annotating instance variables, then running the object through a validator.</p>
  
  <p>Which of the <a href="http://en.wikipedia.org/wiki/Design_by_contract#Languages%5Fwith%5Fthird-party%5Fsupport" rel="nofollow noreferrer">many Java design-by-contract frameworks</a> provide the closest functionality to my "like to have" example? The exceptions that get thrown should be runtime exceptions (like IllegalArgumentExceptions) so encapsulation isn't broken.</p>