0 Replies Latest reply on Dec 7, 2009 1:18 AM by uaskme

    can not see new method after transformation!

    uaskme

      hi guys,
      i am adding new field and its getter and setter methods to an existing class.
      I can see those new changes in the bytecode, but unable to access those getter and setter. I know that the jvm which loaded the class is old, but how to load new byte code class
      here is my code

      ClassPool classPool = ClassPool.getDefault();
      CtClass ctClass = null;
      ctClass = classPool.get("com.cura.hibernate.annotations.Employ");
      ClassFile classFile = ctClass.getClassFile();

      ConstPool constPool = classFile.getConstPool();

      AnnotationsAttribute attr = new AnnotationsAttribute(constPool,
      AnnotationsAttribute.visibleTag);
      Annotation a = new Annotation("javax.persistence.Column", constPool);
      a.addMemberValue("name", new StringMemberValue("EmployAddress",
      constPool));
      // attr.setAnnotation(a);

      // classFile.addAttribute(attr);

      CtField make = CtField.make("private String employAddress;", ctClass);
      CtField ctField = new CtField(make, ctClass);

      // ctField.setAttribute("Author1", ctClass.toBytecode());

      JavassistUtils javassistUtils = new JavassistUtils();
      javassistUtils.addFieldAnnotation(ctField, a);

      ctClass.addField(ctField);

      CtMethod setter = CtNewMethod
      .make(
      "public void setEmployAddress(String employAddress) { this.employAddress = employAddress; }",
      ctClass);

      CtMethod getter = CtNewMethod.make(
      "public String getEmployAddress() { return employAddress; }",
      ctClass);

      ctClass.addMethod(getter);

      ctClass.addMethod(setter);
      ctClass.writeFile("bin");
      ctClass.rebuildClassFile();


      // Loader cl = new Loader(classPool);
      //
      // CtClass ct = classPool.get("com.cura.hibernate.annotations.Employ");
      //
      //
      // Class c = cl.loadClass("com.cura.hibernate.annotations.Employ");
      // Object rect = c.newInstance();



      // Class class1 = ctClass.toClass();


      byte[] byteCode = ctClass.toBytecode();
      HotSwapper hotSwapper = new HotSwapper(8000);// 8000 is a port number.
      hotSwapper.reload("com.cura.hibernate.annotations.Employ", byteCode);


      i have write with hotwapper and also with custom loader class to load .

      Please can anyone direct me in this situation.

      Thank You