title: Java static metaprogramming
body: <p>I'd like to implement annotation processor that will generate new class based on existing "prototype" class.</p>
  
  <pre><code>import java.util.List
  
  @MyAnnotation
  class MySuperClassPrototype {
      static MySuperClassPrototype createInstance() {
        return new MySuperClassPrototype();
      }
  }
  </code></pre>
  
  <p>As a result of code below. The following new source file (compilation unit) will be generated:</p>
  
  <pre><code>import java.util.List
  
  class MySuperClass {
      static MySuperClass createInstance() {
        return new MySuperClass();
      }
      public void specialAddedMethod() {
        /*...*/
      }
  }
  </code></pre>
  
  <p>I'd like to copy all top-level import statements and static members and not static members of prototype-class. I've moved pretty far with Compiler Tree API (com.sun.source.tree). I can print out Tree data-type while substituting new class name for old. But there are problems that seems pretty hard.</p>
  
  <p>If I get Tree.Kind.IDENTIFIER in the tree, how can I find what actual class it references. I need to replace all occurrences of MySuperClassPrototype identifier with MySuperClass identifier, and than print out whole tree.</p>
  
  <p>Is it feasible?</p>
  
  <p>Similarly I need to filter out @MyAnnotation annotation, and again it is represented with Tree.Kind.IDENTIFIER or Tree.Kind.MEMBER_SELECT.</p>
  
  <p>How can I find out actual annotation class that is referenced by this identifier?</p>
  
  <p>And another problem is printing out tree. If I use toString method I got decent result, but constructors are printed as methods with "&lt;init&gt;" name instead of methods with the same name as it's class, so I need to manually print every kind of Tree node.</p>
  
  <p>You can see <a href="https://gist.github.com/1433909" rel="noreferrer">code I've come with here</a></p>