-
1. Re: cast errors
cat4hire Oct 7, 2005 10:35 AM (in response to cat4hire)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
cat4hire Oct 12, 2005 12:18 PM (in response to cat4hire)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
chiba Oct 29, 2005 11:14 AM (in response to cat4hire)I think that problem is due to your inappropriate
use of class loaders.
Section 3.2 of Javassist tutorial might help you.