3 Replies Latest reply on Oct 29, 2005 11:14 AM by Shigeru Chiba

    cast errors

    Luca Ferrari Newbie

      Hi,
      I'm using javassist to merge two classes into a single one. To do this, I copy each member from the class B into the class A (I do not post the code to keep the message short, but I can post it if needed) and then instantiate the A object. Both the classes implement the Intf interface, and I use this interface as base for accepting object that must be merged.
      Everything seems to work, and if I perform reflection I can see the B's members into A. The problem is that if I try to invoke a method on A (thru reflection) I got an InvocationError, and the same if I try to convert (cast) A from its interface Intf to A itself (ClassCastException). It sounds like A has the right information but is not manipulated properly. Moreover, I've tried to write A on a file and reload it from file thru serialization and I got problems due to the serialization version number.

      Thanks,
      Luca

        • 1. Re: cast errors
          Luca Ferrari Newbie

          Hi,
          I haven't solved my problem yet, but here there's a piece of code that (I think) generates the troble:

          if(this.superclass!=null) {
          this.superclass.defrost();
          agentClass.setSuperclass(this.superclass);
          System.err.println("Superclass: "+agentClass.getSuperclass());
          }

          byte code[] = agentClass.toBytecode();
          return this.defineClass(agentName,code,0,code.length);


          being both superclass and agentClass a CtClass reference (obtained thru the class pool). Now, the superclass of my agentClass must be com.ibm.aglet.Aglet, and the output of the program shows:

          Superclasse rilevata: javassist.CtClassType@101ac93[public abstract class com.ibm.aglet.Aglet implements java.io.Serializable, .....]

          thus it seems ok. But, if after the define class I perform a newInstance on the class object, and then a cast to the Aglet class:

          Aglet a = (Aglet) myAgentClass.newInstance();

          I get a ClassCastException. In other words, it seems as the setSuperclass method is not working well. Any idea?

          Luca


          P.S.
          the same code with an older version of javassist, 2.0, worked.

          • 2. Re: cast errors
            Luca Ferrari Newbie

            I've found that, in the above code, after the define class, I'm not able to cast the class to itself too. The class I'm manipulating implements (from the beginning) an interface, like:

            public class TestAgent extends Aglet implement RolexAgent{..}

            and I access it and manipulate it as a RolexAgent. Thus, when I create the new instance and try to cast it to a TestAgent:

            TestAgent t = (TestAgent) this.defineClass(agentName,code,0,code.length).newInstance();

            I got a classcast exception. I've checked the classloader, and it's mine, thus it should work properly. Anybody as an idea of what is happening?

            Luca

            • 3. Re: cast errors
              Shigeru Chiba Expert

              I think that problem is due to your inappropriate
              use of class loaders.

              Section 3.2 of Javassist tutorial might help you.