title: Java code-generation : How to modify an existing java file method?
body: <p>I have a .java src file that looks like this:</p>
  
  <pre>
  class Test {
  
      public void foo() {
      }
  
  }
  </pre>
  
  <p>I would like to modify foo() programatically, in the sense, say add a sysout and make it look like this: </p>
  
  <pre>
  public void foo() {
      System.out.println("hello world");
  }
  </pre>
  
  <p>Are there any known ways to doing this by NOT directly editing the src file (RandomAccessFile)?</p>
  
  <p>A few posts on StackOverflow refer to <a href="https://codemodel.dev.java.net" rel="nofollow noreferrer">CodeModel</a> and Eclipse JDT's AST for code-generation purpose. I see that these will help in code-generation from scratch, and not modify existing code. Is there an API that would let you modify existing code and which has an API as simple as CodeModel/Eclipse JDT's AST? If not, what would be the best way to doing this? </p>