2 Replies Latest reply on Jun 9, 2004 10:49 AM by fabienbaligand

    Bytecode modifications at load time with Javassist Bytecode

    fabienbaligand Newbie

      Hi! I'm currently trying to perform some bytecode modifications on a class while loading this class in the JVM:

      Usually, Javassist users are likely to inject source code into a CtClass but my concern is to inject code from a compiled class A into compiled class B. Plus I'd like to do so while loading Class B into the JVM (Class A doesn't require to be loaded into the JVM) through a ClassLoader of mine.

      My first idea was to modify the findClass(..) method of my ClassLoader: I would intercept the moment class B is loaded and I could get its methods as CtMethods...Then I would use the Javassist Bytecode level API to obtain a ClassFile describing Class A. Thus I could use a CodeIterator to get some compiled code from Class A to finally inject it into some CtMethods of Class B. Eventually Class B would be loaded into the JVM and would behave with some code that belonged to A.

      Unfortunately I don't know how to inject the byte[] obtained from Class A bytecode into any CtMethod of Class B. Maybe we just can't?!

      I would really appreciate if you could tell me how to do such a thing and/or showing a piece of code would be great. If my approach is wrong, could you tell me how to proceed

      Thank you very much!

      -Fabien