1 Reply Latest reply on Oct 21, 2004 11:54 AM by chiba

    Question on CtClass FieldAccess, and ExprEditor

    liam

      Greetings,
      I'm a new user of Javassist. My group is trying to use Javassist to instrument java class file for our research project.
      I have a few questions regarding Javassist:
      1. Methods such as getMethods, getMethod, getClassInitializer are not implemented functionable in CtClass. Are we expecting them to be implemented soon?
      2. In FieldAccess class, why isn't there a getOpcode() function? Since our project requires us to access the field depending on the opcode, it is necessary for use. but generally I think this will be a very useful function in FieldAccess.
      3. I read an article on IBM's website (http://www-106.ibm.com/developerworks/java/library/j-dyn0302.html) on how to use ExprEditor in javassist. However, this does not work anymore as the static method ClassPool.GetDefault() does not take Translator as parameter anymore in javassist 3. It works with javassist 2.6. How can I replace a field access with a function call using ExprEditor now? Is there any reference on it? CodeConverter would not work for use as we need to pass the field object as a parameter to the function being called up the access.

      Thanks for any help!
      Regards,
      Liam

        • 1. Re: Question on CtClass FieldAccess, and ExprEditor
          chiba

           

          1. Methods such as getMethods, getMethod, getClassInitializer are not implemented functionable in CtClass. Are we expecting them to be implemented soon?


          CtClass is an abstract class! Those methods have been implemented
          in CtClassType. The implementation in CtClass is mainly for primitive
          types.

          2. In FieldAccess class, why isn't there a getOpcode() function?


          Do you want to know which the opcode is, getstatic, getfield, putstatic,
          or putfield? If yes, you can instead use FieldAccess#isStatic(), #isReader(),
          #isWriter().

          However, this does not work anymore as the static method ClassPool.GetDefault() does not take Translator as parameter anymore in javassist 3.


          Yes, the article http://www-106.ibm.com/developerworks/java/library/j-dyn0302.html depends on 2.6.

          I showed a modified version of Listing 3 in the article above:

          public class Dissect
          {
           public static void main(String[] args) {
           if (args.length >= 1) {
           try {
           // set up class loader with translator
           Translator xlat = new DissectionTranslator();
           ClassPool pool = ClassPool.getDefault();
           Loader loader = new Loader();
           loader.addTranslator(pool, xlat);
          
           // invoke the "main" method of the application class
           String[] pargs = new String[args.length-1];
           System.arraycopy(args, 1, pargs, 0, pargs.length);
           loader.run(args[0], pargs);
           } catch (Throwable ex) {
           ex.printStackTrace();
           }
           } else {
           System.out.println
           ("Usage: Dissect main-class args...");
           }
           }
          
           public static class DissectionTranslator implements Translator
           {
           public void start(ClassPool pool) {}
           public void onLoad(ClassPool pool, String cname)
           throws NotFoundException, CannotCompileException {
           System.out.println("Dissecting class " + cname);
           CtClass clas = pool.get(cname);
           clas.instrument(new VerboseEditor());
           }
           }


          In 3.0, Translator must be passed to Loader instead of ClassPool.
          Translator#onWrite() was renamed into #onLoad because of this
          move. This is for clarifying the role of ClassPool (and also to make
          Javassist work better with JBoss AOP. :p)