title: Declaring a Map of String to Class, for classes of a specific type
body: <p>I'm trying to do a command line wrapper class around some other other classes that all have a main method.</p>
  
  <p>I've declared a map of Strings to classes, but now I'd like to insure that all the classes implement a particular interface.</p>
  
  <p>The idea is that you'd give the a class alias or "command name" as the first argument, then I'd call the main of that class and pass the rest of the argv array to it.</p>
  
  <p>Initially I had this:</p>
  
  <pre><code>static final Map&lt;String, Class&gt; CLASSES = new LinkedHashMap&lt;String,Class&gt;() {{
      put( "command1", com.foobar.package_a.ClassOne.class   );
      put( "command2", com.foobar.package_a.ClassTwo.class   );
      put( "command3", com.foobar.package_b.ClassThree.class );
      ...
  }};
  </code></pre>
  
  <p>But I wanted to let users run the command with no args and list the classes and what they do.  And from previous work, most of the classes did have a description.</p>
  
  <p>I decided to formalize this in an interface:</p>
  
  <pre><code>public interface HasDescription {
      String getDescription();
  }
  </code></pre>
  
  <p>And then:</p>
  
  <pre><code>public class ClassOne implements HasDescription {
      // I'd prefer static, but that's a different concern
      public String getDescription() {
          return "check files for gophers";
      }
      ... rest of class ...
  }
  </code></pre>
  
  <p>But I'm not sure how to declare it, I've had many variations.  Here's one example of what <strong>doesn't</strong> work:</p>
  
  <pre><code>static final Map&lt;String, Class&lt;T implements HasDescription&gt;&gt; CLASSES = new LinkedHashMap&lt;String,Class&lt;T implements HasDescription&gt;&gt;() {{
      put( "command1", com.foobar.package_a.ClassOne.class   );
      put( "command2", com.foobar.package_a.ClassTwo.class   );
      put( "command3", com.foobar.package_b.ClassThree.class );
  }};
  </code></pre>
  
  <p>Variations / Lessons:</p>
  
  <ul>
  <li>I've tried with an without a leading letter inside the angle brackets</li>
  <li>I've tried both <code>implements</code> and <code>extends</code></li>
  <li>Even in Java 7 you can't just put <code>new LinkedHashMap&lt;&gt;()</code> when declaring it inline</li>
  </ul>
  
  <p>Questions:</p>
  
  <ul>
  <li>Ideally .getDescription() would be static.</li>
  <li>I assume that if I had the correct signature, then pulling it out of the hash with .get() I'd assign to a similarly declared variable (sans the top level Mop&lt; >)</li>
  <li>If a pull out a properly qualified class variable, is there a shortcut or special version of reflection that just lets me say something like ( (HasDescription)clazz ).getDescription() ?</li>
  <li>Wondering if I'm either reinventing the wheel, or that there's some reason this is a horribly bad idea</li>
  <li>Does specifying that all classes must implement a particular interface buy me anything in this usage scenario?  At a minimum, seems like it lets the compiler do some checking for me.  At runtime, I'm wondering if getting a qualified class reference gives me an special advantage over regular reflection.</li>
  </ul>
  
  <p>Why an I doing this?</p>
  
  <ul>
  <li>I'm having Maven produce a full, self contained .jar file</li>
  <li>I'd like folks to run it with just java -jar my_jar.jar ... so I need to choose one specific class; you'd run with <code>java -jar projname.jar command_name arg1 arg2 arg3</code></li>
  <li>If you run with just <code>java -jar projname.jar</code> you get a list of classes and what they do</li>
  <li>The alternative is to run each command as <code>java -cp projname.jar com.foobar.package_a.ClassOne arg1 arg2 arg3</code> but this is a bit ugly for folks to type in, and would require .sh and .bat for each class name.</li>
  </ul>