4 Replies Latest reply on Aug 23, 2003 1:40 AM by chiba

    setSuperClass question

    huiben

      I try to alter the super class of a class, and this replace all occurance of the old super class, including reference to the old super class methods. But I don't want this, I want the reference to older super class method remain, while just change the super class name (i.e. change the 'extends')

      how can we do this?

      thanks
      ben

        • 1. Re: setSuperClass question
          chiba

          The reason why I don't add a method for doing that
          is that it lets the users wrongly change the super
          class to break the consistency. Why do you want
          to do that? I would like to know if there is a real
          example. (If yes, I want to add a method for doing
          that.)

          • 2. Re: setSuperClass question
            huiben

            consider the following 4 classes:

            public class A
            {
            // root class
            // some methods
            }

            public class B
            {
            public static void method(A obj)
            {
            // do something with object of class A
            }
            }

            public class C extends A
            {
            public void method2()
            {
            B.method(this); // param is super class A
            }
            }

            public class D extends A
            {
            }

            my situation is to make C extends D instead of A, i.e.

            public class C extends D
            {
            // and D extends A, so C also extends A
            }

            since D also extends A, so C is still a subclass of A, just that D is inserted in between. using Javassist setSuperClass method, this will break the invocation of B.method, because B.method parameter signature is class A, but setSuperClass change it to class D. as a result, I got method not found exception. i.e. it tries to look for B.method(D) instead of B.method(A).

            I think setSuperClass blindly replace all occurance of A with D is the root of this problem. It does not consider that there are external dependency with A (in my case, B.method() is depend on A, not D)

            that's why I need a different way to setSuperClass.

            what do you think chiba?

            thanks
            ben

            • 3. Re: setSuperClass question
              chiba

              OK, I understand this issue. Give me time.

              • 4. Re: setSuperClass question
                chiba

                Sorry for my too long silence.
                I think Ben is right so I made a decision to change
                the semantics of setSuperclass() so that
                setSuperclass() only changes the extend clause
                and constructors.

                Note: constructors may call the super's constructor.
                If the super class is changed, the constructor body
                must be instrumented.