[jboss-user] [Javassist Users] - Javassist can't seem to instrument static method

ipolevoy do-not-reply at jboss.com
Fri Oct 30 20:19:13 EDT 2009


I stumbled upon a Javassist problem, when it fails to add code with insertBefore().

If someone can help, I would really appreciate this.



  | package test;
  | public class TheAddress extends TheModel {
  |     static{
  |         foo("The address!!!");
  |     }
  | }
  | 


  | package test;
  | public class TheModel {
  |     public static void foo(String param){
  |         System.out.println("Inside foo, param: " + param);
  |     }
  | }


  | package test;
  | import javassist.*;
  | public class JavassistTest {
  | 
  |     public static void main(String[] args) throws Exception {
  | 
  |         ClassPool pool = ClassPool.getDefault();
  |         CtClass clazz = pool.get("test.TheAddress");
  |         CtMethod foo = getMethod("foo", clazz);
  | 
  |         foo.insertBefore("{ System.out.println(\"inserted before foo!\");}");
  |         
  |         clazz.writeFile();
  |         clazz.toClass().newInstance();    
  |     }
  | 
  |     static CtMethod getMethod(String name, CtClass target){        
  |         for (CtMethod method : target.getMethods()) {
  |             if (name.equals(method.getName())) return method;
  |         }
  |         return null;
  |     }
  | }

cheers!

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4263278#4263278

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4263278



More information about the jboss-user mailing list