title: Using Javassist to instrument a private method of a parent class?
body: <p>I would like to use Javassist to instrument a Java class which source is out of my control (Java 1.6.0_26-b03, Javassist 3.16.1-GA).</p>
  
  <p>I'm relatively new to Javassist, but think I know the concepts behind it. I read a lot about it, but maybe I missed something. Please, see the following, really simple code:</p>
  
  <p>File ParentPrivateMethodInstrumentingTest.java:</p>
  
  <pre><code>package test.javassist;
  
  import javassist.*;
  
  public class ParentPrivateMethodInstrumentingTest
  {
      public ParentPrivateMethodInstrumentingTest() throws Exception
      {
          ClassPool classPool = ClassPool.getDefault();
  
          CtClass childCtClass = classPool.get( "test.javassist.Child" );
          CtMethod parentCtMethod = childCtClass.getMethod( "parent", "()V" );
  
          parentCtMethod.insertBefore( "{ System.err.println( \"-- before parent() --\" ); }" );
  
          Child child = (Child)childCtClass.toClass().newInstance();
          child.parent();
      }
  
      public static void main( String[] args ) throws Exception
      {
          new ParentPrivateMethodInstrumentingTest();
      }
  }
  </code></pre>
  
  <p>File Parent.java:</p>
  
  <pre><code>package test.javassist;
  
  public class Parent
  {
      private void privateParent()
      {
          System.out.println( "Parent.privateParent()" );
      }
  
      public void parent()
      {
          System.out.println( "Parent.parent()" );
          privateParent();
      }
  }
  </code></pre>
  
  <p>File Child.java:</p>
  
  <pre><code>package test.javassist;
  
  public class Child extends Parent
  {
  
  }
  </code></pre>
  
  <p>When I run this program, the output is:</p>
  
  <pre><code>Parent.parent()
  Parent.privateParent()
  </code></pre>
  
  <p>So my question is, why is there no line "-- before parent() --", which I thought has been inserted into the bytecode?</p>
  
  <p>Is it not possible to do code insertions the way I've tried on private methods of parent classes?</p>
  
  <p>Thanks a lot for your help!</p>