title: How can I get the name of the defined class in a parent static method in Groovy
body: <p>Note: I found another answer that suggests that Java would redirect the static method call to it's own class even if it's called on a child class so I guess I need to find a Groovy work-around trick or it's just not going to be doable.</p>
  
  <p>Here's the problem: I created an abstract generic "Launcher" class with a "public static void main". The idea is that you extend it and in your child class you annotate methods like this:</p>
  
  <pre><code>@Command("Show an explorere shell")
  public dir() {
      "explorer".execute()    
  }
  </code></pre>
  
  <p>The parent of this class has a main that goes through, reflects for the @Command annotation and if the method name matches your parameter, executes it.</p>
  
  <p>The problem is that I can't figure out how to tell what the actual, instantiated class is within the parent's static main method.</p>
  
  <p>I'm pretty sure there is a trick somewhere--"this" won't work in statics, stack traces don't contain the actual class, just the parent class, and I can't find any meta-info in the class or MetaClass objects that helps.</p>
  
  <p>Currently I've gotten it to work by hard-coding the name of the child class into the parent's main like this:</p>
  
  <pre><code>public class QuickCli {
      public static void main(String[] args} {
          (new HardCodedChildClassName())."${args[0]}"()
      }
  }
  </code></pre>
  
  <p>I cut quite a bit out of that, but it's the general idea.  I'd like to replace </p>
  
  <pre><code>"new HardCodedChildClassName()"
  </code></pre>
  
  <p>with something that will work for any class that extends this class.</p>
  
  <p>Given the two code snips above, the command would be executed from the command line as:</p>
  
  <pre><code>groovy HardCodedChildClassName dir
  </code></pre>
  
  <p>Although I'd prefer not to make all the @Command methods static I could do so if I had to, but currently I'm not even convinced I could make that work.</p>