2 Replies Latest reply on May 1, 2003 9:10 AM by Susumu YAMAZAKI

    Javassist sample

    popmace Newbie

      Hi,
      I would like to receive a sample pgm, that creates an object with an oject as attribute.

      Also after creating CtClass objects how to use that ?
      do i need to interospect every time to use it or can i use like other java objects.

      Thank You
      Pop
      popmace@yahoo.com

        • 1. Re: Javassist sample
          Susumu YAMAZAKI Newbie

          > I would like to receive a sample pgm, that creates an object with an oject as attribute.

          I think the following program list is what you want.

          ClassPool pool = ClassPool.getDefault();
          CtClass cc = pool.makeClass("someClass");
          CtClass ccObject = pool.get("java.lang.Object");
          CtField field = new CtField(ccObject, "someField", cc);
          cc.addField(field);

          > Also after creating CtClass objects how to use that ?

          See CtClass.toClass().

          Susumu YAMAZAKI <yamazaki@is.titech.ac.jp>

          • 2. Re: Javassist sample
            Susumu YAMAZAKI Newbie

            > do i need to interospect every time to use it or can i use like other java objects.

            I 'll give a supplementary explanation.

            If your created new class extends an existing class or implements existing interfaces, and if defined methods override methods defined in the super-class or the super-interfaces, you can use like other java objects via the super-class or the super-interfaces.

            If you add a new method that is not defined in the super-class and the super-interfaces, you need to use reflection (introspection) API.

            If you add a field, you need to use reflection API to access the field because it hides the existing field that has the same signature.

            Even if reflection API is needed, you can directly use the object created by javassist in an additional method created by javassist.

            Susumu YAMAZAKI <yamazaki@is.titech.ac.jp>