0 Replies Latest reply on Apr 21, 2006 7:38 AM by dawidcha

    Proxying a class for O/R mapping framework

    dawidcha

      I'm in the process of building in EJB 3.0 persistence support for an open source persistence product (http://hydrate.sourceforge.net) that I support and was looking to use javassist to do the necessary bytecode manipulation of the user-supplied POJOs.

      What I'd like to do is to proxy these classes with code-generated framework classes, so that whenever the client code thinks it is creating a user class, it ends up with a proxy class that inherits from the original POJO.

      For example:

      -----------Original POJOs:-------------

      package userPackage;

      class Employee {
      //...
      }

      class Manager extends Employee {
      //...
      }


      ------------Code Generated by Hydrate O/R framework:-----------

      package userPackage;

      class Employee extends userPackage.proxied.Employee {
      //.... code generated class
      }

      class Manager extends userPackage.proxied.Manager {
      //.... code generated class
      }


      In order to make this work, I need to change the class name (and only the class name) of the classes to be proxied so that their package is changed to 'userPackage.proxied'.

      I started by using ClassPool.getAndRename(...) to do this and it worked fine, except that any references to the class from within itself also had their package names changed. This was not what I wanted.

      I had a bit of a root around in the code and it seems that what I want to do is not to rename the class because there is only one reference to the class in the constant pool and therefore I end up changing all references to that class within the class file. Instead what I need to do is create a new class reference in the constant pool and use that as the new proxied class name.

      I wrote the following code.

      package javassist.bytecode;

      public class ClassFileModifier {
      public static void proxyClassName(ClassFile cf, String name) {
      cf.thisClass = cf.getConstPool().addClassInfo(name);
      cf.thisclassname = name;
      }
      }

      Now calling proxyClassName with the user's POJO class file and the name 'userPackage.proxied', changes only the name of the class and not any self references.

      My question: does this make sense and is this a good way of achieving what I want to achieve. In order to make it work I need to add a class to the javassist.bytecode package so that I can access the class file 'thisClass' and 'thisclassname' fields, and this feels a bit wrong.

      Best Regards

      David.